通过psexec远程调用Powershell命令

时间:2017-10-31 14:09:55

标签: powershell remote-server psexec

我尝试使用psexec在远程服务器上执行Get-ChildItem(以查找最新的dot net版本),但它在本地系统上执行: 以下是我的代码:

$server = "x.x.x.x"
Invoke-Expression ".\psexec \\$($server) powershell (Get-ChildItem -Path 
$Env:windir\Microsoft.NET\Framework |Sort-Object -Property Name -Descending | 
Select-Object -First 1).Name"

输出:

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com
The term v4.0.30319 is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
At line:1 char:11
+ v4.0.30319 <<<<
+ CategoryInfo          : ObjectNotFound: (v4.0.30319:String) [], CommandN
otFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

这意味着脚本能够在本地系统上运行并找到v4.0.30319版本。 如何让它在预期的服务器上运行而不会出现任何错误并且不会突然受到阻碍? (现在脚本执行和haults而不退出)

注1。我已尝试使用其他连接方法,例如PSSessionInvoke-Command scriptblock方法,但所有抛出:

WinRM not started

注2。我使用psexec,因为Get-ChildItem没有-computername。其次,我成功地能够使用psexec以下命令:

Invoke-Expression ".\PsExec.exe \\$($server) route add 10.38.5.0 mask 255.255.254.0 $($gate) -p"

注3。我已通过psexec分别使用以下命令测试与服务器的连接:\psexec.exe \\10.150.19.108 hostname

并返回该服务器的名称,证明psexec连接已建立。

只是想知道,如何通过psexec运行powershell命令?如果有人可以提供帮助,该解决方案将减少我项目中的一半工作量!

0 个答案:

没有答案
相关问题