Author Posts

June 25, 2013 at 9:10 pm

All,
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*"}
$creds=Get-Credential

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

Any thoughts on what I'm doing wrong?
Thanks!

June 26, 2013 at 4:20 am

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*"}
$creds=Get-Credential

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

June 26, 2013 at 6:50 am

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.

June 26, 2013 at 6:54 am

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.

June 26, 2013 at 7:24 am

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.

June 26, 2013 at 7:52 am

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