使用PowerShell从本地服务器向多个远程服务器执行批处理文件

时间:2016-07-04 17:39:50

标签: powershell batch-file

我想从本地服务器执行以下批处理文件(使用PowerShell),这将从多个远程服务器获取结果,我希望在C:\temp folder中获得这些结果。

@echo off
cd "C:\Program Files\Tivoli\TSM\baclient"
dsmc.exe q mgmtclass > C:\temp\TSMmgmtclass.txt

之后,想要使用PowerShell脚本获得那些输出结果,如下所述。

Get-Content -Path 'C:\Program Files\tivoli\tsm\baclient\dsmerror.log' | select-object -last 15

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法可能是使用invoke-command,并完全删除.bat文件。它会看起来像这样

$scriptBlock = {
  & "C:\Program Files\Tivoli\TSM\baclient\dsmc.exe" q mgmtclass
  return $return
}

invoke-command -computername $computernames -scriptblock $scriptBlock | out-file $logfile

在这种情况下,$ computernames将是一个数组,其中包含要对其运行命令的每台计算机的名称,$ logfile只是您希望内容输出的路径。