Get Patvhes Remote Machines with Authentication Required

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Jason Durnil Jason Durnil 1 year, 1 month ago.

  • Author
    Posts
  • #32617
    Profile photo of Jason Durnil
    Jason Durnil
    Participant

    This task is killing me 🙂 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.

  • #32624
    Profile photo of Ed Grant
    Ed Grant
    Participant

    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?

  • #32625
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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”
    }
    
  • #32628
    Profile photo of Ed Grant
    Ed Grant
    Participant

    Curtis,

    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
    
         foreach
            ($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.

  • #32641
    Profile photo of Jason Durnil
    Jason Durnil
    Participant

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

  • #32643
    Profile photo of Ed Grant
    Ed Grant
    Participant

    Jason,

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

  • #32656
    Profile photo of Jason Durnil
    Jason Durnil
    Participant

    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 🙂

You must be logged in to reply to this topic.