Powershell Output does not display when run on remote PC

Matthew Eilers
Matthew Eilers used Ask the Experts™
on
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}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
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.
Top Expert 2014
Commented:
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.
Matthew EilersI.T. Specialist

Author

Commented:
Thank you for you input guys. Unfortunately -passthru is not being recognized as a usable parameter in this version.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Matthew EilersI.T. Specialist
Commented:
Hah! However, removing the -passthru actually DOES return the programs I was hoping for in my terminal. Cheers!
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Correct, I should have removed that switch ...
PberSolutions Architect

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial