Need Help with PowerShell Script

Welcome Forums General PowerShell Q&A Need Help with PowerShell Script

Viewing 2 reply threads
  • Author
    Posts
    • #192988
      Participant
      Topics: 1
      Replies: 1
      Points: -4
      Rank: Member

      New to PowerShell script so thought to ask for directions here. Learn as I go so forgive my noob sense.

      I wrote a script that does exactly what I needed it done. Nothing fancy… just getting the job done. here goes the description…

      The script calls a list of workstations. It performs a test-connection and then output the result into two files 1) AbleToConnect.txt and 2) UnableToConnect.txt. Then the second part of the script is calling the AbleToConnect.txt and checking to see if each machine has Office 365 ProPlus installed or not. If installed, it export the confirmation to a CSV file I named ConfirmedOPPInstalled.csv. I got everything to work except for where if it can’t find MS Office 365 ProPlus on the machine it then export the list of the workstations from the AbleToConnect.txt to a text file called NoOPPInstalled.txt.

       

      Below is the 2nd part of the script.

      foreach ($workstation in (Get-Content “.\result\AbleToConnect.txt”))
      {
      write-verbose “Working on $workstation…” -Verbose
      Invoke-Command “$workstation” -ScriptBlock {
      Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365ProPlus* | Select-Object DisplayName, DisplayVersion, Publisher
      } | export-csv “.\result\ConfirmedOPPInstalled.csv” -NoTypeInformation -Append -Force

       

      Thanks in advance.

    • #193090
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      Consider simplifying your approach a bit. Rather than trying to save different outcomes to separate files, try to collect that information in a single object\output. Here is an example and how you would query the different aspects:

      $computers = Get-Content computers.txt
      
      $results = foreach ( $computer in $computers ) {
          if (Test-Connection -ComputerName $computer -Count 2 -Quiet) {
              $o365 = Invoke-Command -ComputerName $computer -ScriptBlock {
                  Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\O365ProPlus* | Select-Object DisplayName, DisplayVersion, Publisher
              } 
              
              if ( $o365 ) {
                  $o365 | Select *, @{Name='ComputerName';Expression={$computer}}
              }
              else {
                   [pscustomobject]@{
                      ComputerName   = $computer
                      DisplayName    = $null
                      DisplayVersion = '0.0.0.0'
                      Publisher      = $null
                  }           
              }
      
              
          }
          else {
              [pscustomobject]@{
                  ComputerName   = $computer
                  DisplayName    = $null
                  DisplayVersion = $null
                  Publisher      = $null
              }
              
          }
      }
      
      
      #Test connection failed
      $results | Where {!$_.DisplayVersion}
      
      #Test connection success, but registry issue
      $results | Where {$_.DisplayVersion -eq '0.0.0.0'}
      
      ##Test connection and registry success
      $results | Where {$_.DisplayName}
      
      
    • #193207
      Participant
      Topics: 1
      Replies: 1
      Points: -4
      Rank: Member

      Thank you, Mr. Rob Simmers. I will review and learn each as I go through your approach you’d provided.

Viewing 2 reply threads
  • The topic ‘Need Help with PowerShell Script’ is closed to new replies.