1

I am trying to verify that a specific service exists from a .txt list of server names and then output to a file. In this case I need to also add credentials so I need to use the Invoke-Command. What am I doing wrong here?

clear

start-transcript -path .\Log.txt

$servers = Get-Content .\Resources\Lab.txt
$cred = get-credential domain\Username
$name = Read-Host -Prompt 'Input your service name'


function Getinfo() {foreach($server in $servers) 
    {    
         Get-Service | Where-Object { $_.Name -eq $name}-and {$_.Status -eq "Running"}| Format-Table -AutoSize
    }
}

Invoke-Command -credential $cred -ComputerName $servers -ScriptBlock ${function:Getinfo} 

Stop-Transcript

1 Answers1

1

I think you mean this?

$servers = Get-Content .\Resources\Lab.txt
$cred = Get-Credential domain\Username
$name = Read-Host -Prompt 'Input your service name'

Invoke-Command -Credential $cred -ComputerName $servers -ScriptBlock {
    param($name)
    Get-Service | Where-Object { $_.Name -eq $name -and $_.Status -eq "Running"}
} -ArgumentList $name | Format-Table -AutoSize
marsze
  • 15,079
  • 5
  • 45
  • 61