我想在所有远程服务器(大约100台服务器)上远程运行一个命令。此命令收集所有iis并保存在txt文件中。
(C:\Windows\System32\inetsrv\appcmd.exe list app >C:\Temp\list.txt)
自然地,我的问题是如何获取本地计算机中所有远程服务器的列表。我不想在远程服务器上收集每个列表。
$Servers = @("remotemachine1", "remotemachine2", "remotemachine3" )
foreach ($server in $Servers) {
$PSSession=New-PsSession -ComputerName $server
Invoke-command -Session $PSSession -ScriptBlock {
Write-Verbose -Message "Server: $env:ComputerName" -Verbose
C:\Windows\System32\inetsrv\appcmd.exe list app >C:\Temp\list.txt
}
}
Get-PSSession | Remove-PSSession
答案 0 :(得分:0)
您可以这样返回此文件的内容:
$Servers = @("remotemachine1", "remotemachine2", "remotemachine3" )
foreach ($server in $Servers) {
$PSSession=New-PsSession -ComputerName $server
$result = Invoke-command -Session $PSSession -ScriptBlock {
Write-Verbose -Message "Server: $env:ComputerName" -Verbose
return (C:\Windows\System32\inetsrv\appcmd.exe list app)
}
$result > C:\Temp\list.txt
}
Get-PSSession | Remove-PSSession
答案 1 :(得分:0)
C:\Temp\list.txt
使用Write-Output
而不是输出并将其保存到变量中-ComputerName
参数所以:
$Array = New-Object System.Collections.ArrayList
$Servers = @("remotemachine1", "remotemachine2", "remotemachine3" )
foreach ($server in $Servers) {
$Results = Invoke-command -ComputerName -ScriptBlock {
Write-Verbose -Message "Server: $env:ComputerName" -Verbose
Write-Output (C:\Windows\System32\inetsrv\appcmd.exe list app)
}
$Row = "" | Select Server,Results
$Row.Server = $Server
$Row.Results = $Results
[void]$Array.Add($Row)
}
最后,$Array
应该拥有所有内容。
答案 2 :(得分:0)
您还可以将invoke命令通过管道传输到文件中
invoke-command -session $pssession -scriptblock { commands on remote machine } | out-file -literalpath "c:\temp\$server-results.txt"