So I am very new to Powershell and I am almost certain that what I am doing is not the most efficient way of going about it. But I really want to understand why what I am doing is not working.
I am trying to trigger Configuration Manager client actions using Powershell by running the following code:
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Invoke-WmiMethod -Namespace "Root\CCM" -Class SMS_Client -Name
TriggerSchedule -ArgumentList "{00000000-0000-0000-0000-000000000042}"
}
This runs fine. But I wanted to be able to call a variable or something where all of those long codes are instead of having to put those in each time I want to change the client action. So here is where I started playing around and was unable to get anything to work.
$ApplicationDeployment = '"{00000000-0000-0000-0000000000000042}"'
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Invoke-WmiMethod -Namespace "Root\CCM" -Class SMS_Client -Name
TriggerSchedule -ArgumentList $ApplicationDeployment
}
This gives me an error. I also tried the following:
$hash = @{"ApplicationDeployment" = "{00000000-0000-0000-0000-000000000042}"}
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Invoke-WmiMethod -Namespace "Root\CCM" -Class SMS_Client -Name
TriggerSchedule -ArgumentList $hash.'ApplicationDeployment'
}
and finally
$Object = @{ApplicationDeployment = '{00000000-0000-0000-0000-000000000042}'}
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Invoke-WmiMethod -Namespace "Root\CCM" -Class SMS_Client -Name
TriggerSchedule -ArgumentList $Object.ApplicationDeployment
}
I have also tried
$($ApplicationDeployment).ArgumentList
But this gives the same error as everything else.
I would really appreciate an explanation as to why this isn't working... Thank you in advance.
Your issue is that remote machine doesn't have your variable initialized locally. You need to pass it to remote machine when executing script. To do this, replace $Object.ApplicationDeployment with $Using:Object.ApplicationDeployment as in code below:
$Object = @{ApplicationDeployment = '{00000000-0000-0000-0000-000000000042}'}
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
Invoke-WmiMethod -Namespace "Root\CCM" -Class SMS_Client -Name TriggerSchedule -ArgumentList $Using:Object.ApplicationDeployment
}