【问题标题】:how to run powershell on remote windows get remote windows result instead of local如何在远程 Windows 上运行 powershell 获取远程 Windows 结果而不是本地
【发布时间】:2018-08-09 18:38:37
【问题描述】:

我的本​​地计算机名称是 LOCAL-MACHINE, 远程计算机名称为 REMOTE-MACHINE。

我希望在本地计算机上发出 powershell cmdlet, 通过 Enter-PSSession 连接到远程计算机, 并在远程计算机的 pssession 上执行 hostname.exe, 然后退出。

我在本地计算机 powershell ISE 上发出这些命令。

$password = ConvertTo-SecureString "Mypassword" -AsPlainText -Force

$cred=new-object System.Management.Automation.PSCredential("domain\user",$password)

Enter-PSSession -ComputerName REMOTE-MACHINE -Port 5986  -Credential  $cred -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck)

hostname.exe

Exit-PSSession

我希望得到结果:

REMOTE-MACHINE

但是我得到了这个结果(本地计算机名):

 LOCAL-MACHINE

我能做什么?

【问题讨论】:

  • 您是否真的看到控制台提示更改为 [REMOTE-MACHINE]: PS C:\.. ?如果不是,你没有连接......你也是一次运行它吗?还是一行一行?
  • 等到 Enter-PSSession 执行完毕,再执行hostname.exe

标签: powershell


【解决方案1】:

这是因为 Enter-PSSession 在脚本中不起作用。请改用Invoke-Command

$password = ConvertTo-SecureString "Mypassword" -AsPlainText -Force
$cred=new-object System.Management.Automation.PSCredential("domain\user",$password)

Invoke-Command -ComputerName REMOTE-MACHINE -Port 5986  -Credential  $cred -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck) -scriptblock {

    hostname.exe
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多