我正在尝试编写远程Web服务器上安装程序的执行脚本。 有问题的安装程序也是托pipeNServiceBus的Windows服务。 如果RDP进入服务器,则通过以下命令安装该应用程序:
&“$ theInstaller”/ install / serviceName:TheServiceName
安装程序将输出关于其注册服务的进度以及连接到数据库的stdout到stdout等等。
这从RDP会话正常工作,但是当我通过PS远程执行它时,如果直接执行它或通过Invoke-Command -computername $theRemoteServer执行它,我会得到一个不能通过networking传递的消息:
System.IO.FileLoadException:无法加载文件或程序集'file:// \\ theRemoteServer \ c $ \ thePath \ AutoMapper.dll'或其某个依赖项。 操作不受支持。 (来自HRESULT的exception:0x80131515)—> System.NotSupportedException:尝试从networking位置加载程序集,该程序集会导致程序集在以前版本的.NET Framework中被沙盒化。 此版本的.NET Framework默认情况下不启用CAS策略,因此此加载可能很危险。 如果此加载不是为了assembly沙盒,请启用loadFromRemoteSources开关。 有关更多信息,请参阅http://go.microsoft.com/fwlink/?LinkId=155569 。
(注意:为了使它在本网站的预览中正确显示,我在第一行的path中添加了一个额外的“\”。)
这个和其他DLL被服务加载,服务的执行上下文显然不能被重新定义。
我也尝试使用Invoke-WmiMethod ,它做了一些事情 ,但不清楚什么,并从安装程序的输出丢失:
Invoke-WMIMethod win32_process create'“$ theInstaller”/ install / serviceName:TheServiceName' – 计算机名$服务器(在intaller引用之前有和没有cmd.exe / k):
__GENUS:2
__CLASS:__PARAMETERS
__SUPERCLASS:
__DYNASTY:__PARAMETERS
__RELPATH:
__PROPERTY_COUNT:2
__指令:{}
__SERVER:
__NAMESPACE:
__PATH:
ProcessId:
返回值:9
如何远程执行这样的EXE 并捕获输出?
在本地远程执行它…我的意思是,在远程服务器上远程执行远程本地机器上的远程服务器。 那么,也许这只是更less的混淆阅读…
听起来你最好的select就是使用PSExec打开远程服务器上执行的远程PowerShell控制台或命令行。
所以,例如:
psexec \\ remoteserver cmd.exe
要么
psexec \\ remoteserver powershell.exe
您最终会在远程服务器上运行PowerShell或cmd的实例,您将在屏幕上进行操作。
好的…这个作品:
invoke-command $remoteServer { param($installer) cmd /c "$installer" /install /serviceName:TheServiceName } -ArgumentList $installer
其中$remoteServer是远程服务器的名称, $installer是该远程服务器上$installer的UNCpath,而TheServiceName是服务名称。
不过,我还必须提供特殊的configuration,才能通过networking调用exe,如下所示: http : //through-the-interface.typepad.com/through_the_interface/2011/07/loading-blocked-and -networking托pipe组件,与净4.html
所以这个技巧是将服务configuration为可远程的,然后你可以invoke-command它。
另一种破解螺母的方法,无需更改部署中的应用程序configuration:
# increase memory available to WsMan (run from elevated PS instance on remote machine) set-item wsman:localhost\Shell\MaxMemoryPerShellMB 2048 # for PowerShell v3 or higher this line is also necessary Set-Item WSMan:\localhost\Plugin\Microsoft.PowerShell\Quotas\MaxMemoryPerShellMB 2048 # add remote work to script on remote server, and then call that script from the local script invoke-command $remoteServer { param($remoteScript, $arg1, $arg2, $arg3) powershell $script $arg1 $arg2 $arg3 } -ArgumentList $remoteScript, $arg1, $arg2, $arg3