PowerShell Remote and OutFile Locally with If Statement

Welcome Forums General PowerShell Q&A PowerShell Remote and OutFile Locally with If Statement

Viewing 2 reply threads
  • Author
    • #187090
      Topics: 1
      Replies: 0
      Points: 16
      Rank: Member

      Hi all,

      I am a bit stumped here. I am trying to write what I thought would be a pretty easy script to search for a .ini file on an array of servers. If the .ini file contains the text i'm looking for, It will report back to a .txt file on my local machine.

      This is what I have without the array.. This is just checking one server.


      Invoke-Command -ComputerName test-server.domain.com -ScriptBlock {If (get-content C:\Windows\SMSCFG.ini | select-string -Pattern "Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36" -SimpleMatch){
      Out-file "$env:ComputerName" -Filepath c:\temp\MachinesWithGUID.txt}}

      I have also ——

      Invoke-Command -ComputerName test-server.domain.com -FilePath C:\temp\GUIDChecker.ps1 | Write-output C:\Temp\MachinesWithGUID.txt

      The GUIDChecker.ps1 basically contains¬† — get-content C:\Windows\SMSCFG.ini | select-string -Pattern "Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36" -SimpleMatch

      This returns just a blank MachinesWithGUID.txt, but what I'm looking for is if the GUID exists on the .ini, it will write back to my MachinesWithGUID.txt with the Hostname of the server. If it doesn't contain the GUID in the .ini, then nothing.


      Any help would be greatly appreciated!





    • #187246
      Topics: 6
      Replies: 108
      Points: 304
      Helping Hand
      Rank: Contributor

      By doing this, you're writing to a file local to the test-server.domain.com machine.

      Invoke-Command -ComputerName test-server.domain.com -ScriptBlock {If (get-content C:\Windows\SMSCFG.ini | select-string -Pattern "Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36" -SimpleMatch){
      Out-file "$env:ComputerName" -Filepath c:\temp\MachinesWithGUID.txt}}

      And if the line above is what is in your GUIDChecker.ps1 script, then you've got the same thing happening there.

      You need to write your ScriptBlock so that it outputs to the console and then pipe that to a file like you are doing in your second example. Something like:

      Invoke-Command -ComputerName test-server.domain.com -ScriptBlock {
          If (get-content C:\Windows\SMSCFG.ini | select-string -Pattern "Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36" -SimpleMatch)
      } |
      Out-file -Filepath c:\temp\MachinesWithGUID.txt
    • #187252
      Topics: 27
      Replies: 740
      Points: 2,021
      Helping Hand
      Rank: Community Hero

      Does this work?

      invoke-command test-server.domain.com { get-content C:\Windows\SMSCFG.ini | 
        select-string Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36 }

      Actually that comes out blank for me too. For some reason, the default custom format of the returned psobject is blank. This will get you both the line and the pscomputername. You can pipe to select line locally too.

      invoke-command test-server.domain.com { select-string Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36 C:\Windows\SMSCFG.ini | select line }
      Line                                                 PSComputerName         RunspaceId
      ----                                                 --------------         ----------
      Identifier=GUID:DA56AFAD-E7AA-4A71-9A02-174D94EF4E36 test-server.domain.com 1bb11911-42b6-4736-92bd-8365e8f6c205
Viewing 2 reply threads
  • You must be logged in to reply to this topic.