Powershell Output does not display when run on remote PC

I wrote a simple script to display installed programs. It works on the local machine just fine - grabs the programs, lists them alphabetically, great. When I try to run it on a remote PC, the script finishes but there is no output. No error, but nothing displayed. I tried putting Write-Host in various places, but no joy. I would like to be able to retrieve this information without having to write it to a file on the local machine, and then open the file to read the info. Any hints? Also not sure if the invoke-command is needed.


$Computername = Read-Host -prompt ("Which PC would you like to Inventory installed software on?")
$Credential = Read-Host -prompt ("What domain and username would you like to Authenticate with?")
$s = New-PSSession -ComputerName $Computername -Credential $Credential
Invoke-Command -Session $s -ScriptBlock {Get-WMIObject -Class Win32_Product -Computername $Computername | Sort name | Format-Wide -Column 1 -passthru}
Matthew EilersI.T. SpecialistAsked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
As a rule of thumb, Format-* needs to be the last command in a pipe. This includes Invoke-Command output.
Using Invoke-Command -ComputerName together with Get-WMIObject -Computername is doing the same thing twice ;-). I would just do the WMI query:
$Computername = Read-Host -prompt ("Which PC would you like to Inventory installed software on?")
$Credential = Read-Host -prompt ("What domain and username would you like to Authenticate with?")
Get-WMIObject Win32_Product -Computername $Computername -Credential $Credential | Sort name | Format-Wide -Column 1 -passthru

Open in new window

will probably work much better.
0
 
footechCommented:
To add to Qlemo's post...
If you have firewall restrictions such that you can't do a direct query via Get-WmiObject, then you could either use Get-WmiObject (omitting the computername parameter) within the Invoke-Command scriptblock, or you could substitute Get-CimInstance for Get-WmiObject in the code Qlemo posted.

Caveat - I feel I have to post this almost every time I see use of Win32_Product.
https://gregramsey.net/2012/02/20/win32_product-is-evil/
People should be aware of the potential downside before using.
0
 
Matthew EilersI.T. SpecialistAuthor Commented:
Thank you for you input guys. Unfortunately -passthru is not being recognized as a usable parameter in this version.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Matthew EilersI.T. SpecialistAuthor Commented:
Hah! However, removing the -passthru actually DOES return the programs I was hoping for in my terminal. Cheers!
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Correct, I should have removed that switch ...
0
 
PberSolutions ArchitectCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Qlemo (https:#a42429319)
-- footech (https:#a42429416)
-- Matthew Eilers (https:#a42430579)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.