我有以下powershell命令,该命令根据.log文件的内容读取并提供.bat脚本的状态:
Get-ChildItem *.log | ForEach-Object {
[pscustomobject] @{
Name = $_.Name
Status =
('FAILED', 'SUCCESS')[(Select-String -Quiet 'Finished with NO errors' $_.FullName)]
}
}
当在与存储.log文件的服务器(ServerA(相同的服务器上运行时,此命令可以完美运行,但当在服务器之外运行时,不会返回任何值。有没有办法让它在同一网络中的另一台服务器上运行?
- 服务器:ServerA
- 目录:c:\Temp\
作为实现@AdminOfThings建议的示例,并附上一些注释:
$servers = "server1", "server2"
ForEach ($server in $servers){
Invoke-Command -ComputerName $server -ScriptBlock {
Get-ChildItem c:temp*.log | ForEach-Object {
[pscustomobject] @{
Name = $($env:COMPUTERNAME)
Status = ('FAILED', 'SUCCESS')[(Select-String -Quiet 'Finished with NO errors' $_.FullName)]
}
}#EndOfForEachLogFile
}#EndOf Invoke-Command
}#EndOf ForEach