Home > Uncategorized > No string/variable expansion in foreach loops when using Invoke-Command? Try expansion by declaring a script block object.

No string/variable expansion in foreach loops when using Invoke-Command? Try expansion by declaring a script block object.

An administrative assistant said that they saw much higher levels of color printing on some Canon printers this past month, and we were required to pay pretty high levels of overages on our lease. She asked me to take a look at several machines to check who is set to print in color. As the Default printing preferences do reflect Black/White, each user would have to manually set their printing preferences to color.

Guessing that the printer preferences settings are stored in the registry, I used one of my favorites tool, regshot, to take before and after snapshots to obtain the registry data value that was changed. Unfortunately, it was a reg_binary, which may (like a dword, etc) have a difficult to understand bit pattern for settings. Makes sense if you’re the dev, doesn’t make sense to easily pull out explicit meaning from the value for the layman. However, in this one case, there was a data value that only exists if you’ve ever modified the Default printing preferences locally. So, instead of going around to a bunch of machines and snailing it through print preferences, I figured that I could check if the registry data value existed, and maybe perform further investigation to see if the bitwise calculation can reliably be decompiled.

The issue I came across is an old and good one. When using `foreach` with `Invoke-Command`, the variable can not be passed into the script block. The solution is quite simple. You declare the scriptblock object before calling `Invoke-Command`:

$contents = "a","string","list","of","SIDs"

foreach ($sid in $contents) {

  $invokable = "write-output ""checking sid $sid""; New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS | out-null; write-output ""$env:COMPUTERNAME iamaprintserver,Canon C7055\7065 (Front) modified:"";(get-itemproperty -path ""HKU:$sid\Printers\Connections\,,iamaprintserver,Canon C7055\7065 (Front)"" -name ""devmode"" | measure-object).count; write-output ""$env:COMPUTERNAME iamaprintserver,Canon C7055\7065 (Front) DBL sided modified:""; (get-itemproperty -path ""HKU:$sid\Printers\Connections\,,iamaprintserver,Canon C7055/7065 (Front) DBL Sided"" -name ""devmode""  | measure-object).count"
  $scriptbblock = $ExecutionContext.InvokeCommand.NewScriptBlock($invokable)
  Invoke-Command -scriptblock $scriptbblock -computername ( get-adcomputer -filter "objectCategory -eq 'computer'" -SearchBase "OU=Workstations,OU=New York,DC=contoso,DC=corp" | select-object -expand name ) 2>$null | tee -append -filepath grabregistryvalues.log
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: