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.

2
  • The correct parameter is -ComputerName - you are using -Computer? Commented May 31, 2017 at 10:28
  • Computer and ComputerName is alias. I tried long name and output (error) is same. Commented May 31, 2017 at 10:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.