You must be careful when using the PowerShell accelerator [xml] and dynamic properties. What looks like a regular XML document object model is added to an ArrayList and other insincere objects.
Of course, when I load your XML into the DOM and call GetType() on the Service element, I get Object[] :
> $xml = [xml]'<QCSettings><Services><Service /><Service /></Services></QCSettings>' > $xml.QCSettings.Services.Service.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
Since you are using PowerShell 3, you can use the using scope to refer to an XML block in a remote script block:
$services = $xml.QCSettings.Services.Service Invoke-Command -ScriptBlock { $using:services | ForEach-Object { Set-Service $_.ServiceName -StartupType $_.StartupType } }
Since this code is transmitted in all objects of the service, you register only on each computer once, whereas earlier you registered once for the service.
If you cannot use the using scope, I will just live with an ArrayList and write your script block to accept a list of objects:
$scptModifyService = { param ( [Object[]] $service ); $service | ForEach-Object { Set-Service -Name $_.ServiceName -StartupType $_.StartupType } } $XML.QCSettings.Services.Service | ForEach-Object { Invoke-Command -ScriptBlock $scptModifyService -ComputerName $ComputerName -ArgumentList $_ }
source share