0

I want to sometimes check some info from our servers in domains. In this example, I am trying to remotely get windows versions (just one server, currently without loop):

$cr=Get-Credential "domain\adm_user"
$computer="serverA"
Invoke-Command { Get-WmiObject -Class Win32_OperatingSystem -Namespace root/cimv2 -Computer $computer -Credential $cr | Format-List -Property Name, OSArchitecture, SerialNumber} -AsJob -ComputerName .
Get-Job | Wait-Job
Get-Job | Receive-Job

Output:

Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

Seems, that scriptblock {} cannot see variable $computer. When I replace variable -computer $computer to name of server, it is working.

Jerry1
  • 372
  • 2
  • 6
  • 17

1 Answers1

1

For variable expansion inside Invoke-Command use a param block and specify arguments:

$computer = 'localhost'
Invoke-Command {param($computer) Get-WmiObject Win32_OperatingSystem -Computername $computer } -ArgumentList $computer

Working on PS v5.

Jelphy
  • 961
  • 2
  • 11
  • 28