Get Patvhes Remote Machines with Authentication Required

This task is killing me :slight_smile: Ive spent so much time with no success. Ive found many great syntax to get the patches form computers in the same domain but I cant figure out how to provide authentication against a different domain without a trust.

The same as running the command against a non-domain system.

This what I have that works great on a local machine.

Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName $env:computername |select Description,HotFixID,InstalledBy,InstalledOn| export-csv “C:\Users\jdurnil-admin\Documents\Collection\Patches$env:computername Hotfixes.csv”

Something that Ive tried without success.

foreach ($computers in $i)
Invoke-Command -ComputerName $i -ScriptBlock { Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName $env:computername |select Description,HotFixID,InstalledBy,InstalledOn| export-csv “.$env:computername.csv”} -credential $cred

I want this file $env:computername.csv to write to my local machine

Thank you so much for the help.

Try adding the -Credential parameter.

Invoke-Command -Credential Domain\User -ComputerName $i -ScriptBlock {Stuff}

If that doesn’t work then there is a slightly different approach I can share.

And to clarify, you want the file to be output to each server locally, or to the machine you are running the script on?

I personally have not used a lot of invoke-command, but it looks like your export is in the wrong place if you want the export to be on the local machine. Right now it’s in the script block, which executes on the remote machine. It would reason that you need to move it out so it executes on the pipeline data invoke-command returns on the local machine. (The below is conceptual, I have not tested it)

foreach ($computers in $i)
Invoke-Command -ComputerName $i -ScriptBlock {
                                              Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName $env:computername |
                                              select Description,HotFixID,InstalledBy,InstalledOn
                                             } -credential $cred |
export-csv “.\$env:computername.csv”


I have had issues with Invoke-Command in loops as well. I found that actually creating a New-PSSession caused everything to run a lot smoother.

$cred = Get-Credential domain\user
$TargetSession = Get-Content C:\file.txt

        ($Computer in $TargetSession)
          #Pings each computer before trying to execture the script. If unreachable, nothing happens
          if (Test-Connection -ComputerName $Computer -BufferSize 16 -Count 1 -Quiet)
                #Creates a new remote PowerShell Session
                $Session = New-PSSession $Computer -Credential $cred
                Invoke-Command -Session $Session -ScriptBlock {Your Code Here}
                Remove-PSSession -Session $Session

Jason, this approach may work for you as well.

Thanks guys… I was struggling with this one and that got me to where I need to be.


Good to hear! What changes did you end up making that eventually got everything working?

The New-PSSession was the trick to solving the loop issue. I also moved the export.csv tu just after the close bracket. Im a happy man :slight_smile: