Powershell script

Sara 401 Reputation points
2021-05-05T13:42:21.253+00:00

can someone help me on the below script on how to get the below script working to get the status of multiple services on multiple servers.

$servers = 'a','b'
$services = 'a', 'b'

for each($server in $servers)
{
$status = Invoke-Command -Session $remotesession -ScriptBlock { (Get-Service -Name $services).status }

I can get this working when I pass it on like this,

$status = (Get-Service -Name $services -ComputerName $Server).status

But considering the performance of using remote connection every time vs using a pssession might be worth a shot .

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,273 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 30,826 Reputation points
    2021-05-05T13:52:01.143+00:00

    You don't say what error or what result you are getting. It look like you just need to use $using so that your scriptblock can reference the $service variable.

    https://www.pdq.com/blog/invoke-command-and-remote-variables/

    $servers = 'a','b'
    $services = 'a', 'b'
    
    for each($server in $servers)
    {
    $status = Invoke-Command -Session $remotesession -ScriptBlock { (Get-Service -Name $using:services).status }
    

0 additional answers

Sort by: Most helpful