如何在远程计算机上运行Get-ChildItem命令

时间:2016-09-08 15:42:55

标签: powershell

我需要运行以下内容:

Get-ChildItem Cert:\LocalMachine -Recurse

在远程计算机上。例如,我有一个循环:

foreach ($server in $serverList) {
   if((Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
   {
        $certs = Get-ChildItem Cert:\LocalMachine -Recurse | Select Thumbnail, subject
        .....
   }
}

如何确保它在远程计算机上运行?

1 个答案:

答案 0 :(得分:0)

您可以使用Invoke-Command

foreach ($server in $serverList) {
   if((Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
   {
        $certs = Invoke-Command -Cn $server {
            Get-ChildItem Cert:\LocalMachine -Recurse | Select Thumbnail, subject
        }
        .....
   }
}