Run Powershell command from different server

霸气de小男生 提交于 2021-01-07 01:43:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!