Can not get variables populated when in script

I see this problem repeatedly in my scripts, I’m trying to output a line of powershell to a variable. When I do it from console the variable populates fine however. Once I put that same line in a script the variable does not populate. Poshoholic pointed out in a prior post that I should be using -passthru in some cases, but I don’t see the -passthru parameter available in allot of these cmdlets I’m trying to use.

Here is the script I am currently working on. I’m trying to pull data from the $Gwmi variable, but its never populated with the pipeline output.

$Prod=Get-ADComputer -SearchBase "OU=PROD,OU=Servers,DC=domain,DC=com" -Filter {name -like "prod*"}

foreach ($Server in $Prod){
$Gwmi = Get-WmiObject -Class Win32_Service -ComputerName $ -Credential $Creds |where {$ -like "Netbackup Client*"}|Select-Object @{label="ComputerName";expression={$}}, name, state
$ToOutput += $Gwmi
$ToOutput |Export-Csv c:\scriptexports\20130625BUservicesstillrunning.csv

Any thoughts on what I’m doing wrong?

I think it’s because you’re filtering in your Where-Object clause using Name instead of DisplayName. Since nothing is returned, you never get any results assigned to $Gwmi.

Also worth pointing out, you are doing client-side filtering in your Get-WmiObject call, when you should be using server-side filtering to get results faster by only pulling back the service you care about.

I haven’t run this exact script, but I would probably remove some of the variables and implement it something like this:

$Prod=Get-ADComputer -SearchBase "OU=PROD,OU=Servers,DC=domain,DC=com" -Filter {name -like "prod*"}

$(foreach ($Server in $Prod){
    Get-WmiObject -Class Win32_Service -ComputerName $ -Credential $Creds -Filter 'Name LIKE "Netbackup Client%" OR DisplayName LIKE "Netbackup Client%"' | Select-Object @{label="ComputerName";expression={$}}, name, state
}) | Export-Csv c:\scriptexports\20130625BUservicesstillrunning.csv

Thanks Posho
I will try your method and reply with results.
Still I find it odd that the same line in the shell does add content to the $gwmi content. But when done in a script it doesn’t.

I would suggest you confirm that by doing the following:

  1. Copy the exact script into the clipboard.
  2. Open a new PowerShell console.
  3. Invoke a second PowerShell host with -NoProfile (powershell -noprofile).
  4. Paste in your script to see if it works as you expect.

The trouble with comparing console to script often is that the console has some state/configuration due to interactive use that the script does not.

Now, there are scripts that perform differently in the console than in a script file, so that is actually possible, but there are specific use cases where this occurs and from what I see in your script, this is not one of those use cases.

It worked!
Turning the foreach cmdlet into a variable and using it in the pipeline has opened so many possibilities for me.
Thanks for the creative solution Poshoholic.

Excellent, I’m glad it resolved your issue Matthew.