问题
I have the following powershell command that reads and gives me the status of my .bat scripts depending on the content of .log files :
Get-ChildItem *.log | ForEach-Object {
[pscustomobject] @{
Name = $_.Name
Status =
('FAILED', 'SUCCESS')[(Select-String -Quiet 'Finished with NO errors' $_.FullName)]
}
}
This command runs perfectly, when running on the same server as the one where the .log files are stored (ServerA), however when running outside of it - no value is returned. Is there a way to make it run from another server within the same network?
- Server : ServerA
- Directory : c:\Temp\
回答1:
As an example of implementing @AdminOfThings suggestion, with some comments:
$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
来源:https://stackoverflow.com/questions/65308261/run-powershell-command-from-different-server