这是我用来运行远程服务器上exe文件的代码。我在Web服务器中部署了带有以下代码的控制台应用程序,以调用远程服务器上的exe。但是我收到错误消息“ psexec%1不是有效的Win32应用程序”。我在本地计算机上测试了此代码,它可以正常工作...但是在服务器上不起作用。
<webLimits>
......
<sites>
.....
<site name="[YourProjectName]" id="[YourProjectID]">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="[Your Project Path]" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:50569:[Remove localhost from here]" />
</bindings>
</site>
.....
</sites>
......
<webLimits />
服务器详细信息-
内核版本:Windows Server 2012 R2 Standard,多处理器免费 产品类型:标准版 产品版本:6.3 服务包:0 内核内部版本号:9600 注册机构: 注册所有者:Windows用户 IE版本:9.000 系统根目录:C:\ Windows 处理器:4 处理器速度:2.5 GHz 处理器类型:AMD Opteron(tm)处理器6380 物理内存:2 MB 视频驱动程序:VMware SVGA 3D
此软件包中的PsTools版本:2.45
答案 0 :(得分:0)
Install Power Shell in the Remote PC
Enable Power Shell Remoting to execute Powershell Commnads on the remote server by using the command "Enable-PSRemoting"
Add the server from which you are making the call as a Trusted one by using the command-
set-item wsman:\localhost\Client\TrustedHosts -value 172.16.0.0
注意:确保在两台计算机上都运行上述命令
Now use the below code in your project to execute exe on remote server
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
ps.Commands.AddScript("$User = \"domain\\username\"");
ps.Commands.AddScript("$PWord = ConvertTo-SecureString -String \"yourpassword\" -
AsPlainText -Force");
ps.Commands.AddScript("$Credential = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList $User, $PWord");
ps.Commands.AddScript("Invoke-Command -ComputerName REMOTECOMPUTERIP -ScriptBlock {
D:\\TestApp.exe } -cred $Credential");
Collection<PSObject> resultst = ps.Invoke();
Collection<ErrorRecord> errors = ps.Streams.Error.ReadAll();
StringBuilder errorDetails = new StringBuilder();
if (errors != null && errors.Count > 0)
{
foreach (ErrorRecord er in errors)
{
errorDetails.AppendLine(er.Exception.ToString());
}
throw new Exception(errorDetails.ToString());
}
Console.WriteLine(errorDetails.ToString());
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in resultst)
{
stringBuilder.AppendLine(obj.ToString());
}`enter code here`
Console.WriteLine(stringBuilder.ToString());
runspace.Close();