Remote query for certain software and get CSV output?

Welcome Forums General PowerShell Q&A Remote query for certain software and get CSV output?

Viewing 1 reply thread
  • Author
    Posts
    • #248878
      Participant
      Topics: 1
      Replies: 0
      Points: 12
      Rank: Member

      I mostly been playing with command line and just started to figure out PowerShell. So far I have managed to get this script working with the help of google, but I am unable to figure out how to get the output into an CSV file instead of a text file. It would make things easier for me if I could get it into a CSV and maybe convert into an excel format afterwards. I had seen some commands for that while looking over google results. For starters, how would I get the CSV file working instead of a normal text file?

      • This topic was modified 1 month, 2 weeks ago by kcraptor8256.
    • #249116
      Participant
      Topics: 15
      Replies: 1776
      Points: 3,218
      Helping Hand
      Rank: Community Hero

      In order to get results to a CSV, or really to do anything as far as analysis or export, Powershell typically works with a PSObject. If you create a common object schema, then everything has the same properties. This is untested, but this should be closer to what you are looking for:

      <br />#The computername should be passed to the function as a parameter, not referenced as an external variable<br />Function Get-InstalledApps {<br />    param (<br />        $ComputerName<br />    )</p><p>    Invoke-Command -ComputerName $ComputerName -ScriptBlock {<br />        if ([IntPtr]::Size -eq 4) {<br />            $Regpath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'<br />        }<br />        else {<br />            $Regpath = @(<br />                'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',<br />                'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'<br />            )<br />        }<br />        Get-ItemProperty $Regpath |<br />        .{Process{If($_.DisplayName -and $_.UninstallString) { $_ } }} |<br />        Select DisplayName, Publisher, InstallDate, DisplayVersion, UninstallString |<br />        Sort DisplayName<br />    }<br />}</p><p>$Computers = Get-Content 'MachineList.txt'<br />$ProgramList = Get-Content 'ProgramList.txt'<br />$Date = Get-Date -Format MMM-dd-yyyy:HH.mm</p><p>$results = foreach ( $Computer in $Computers ){<br />    if (!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {<br />        [pscustomobject]@{<br />            Computername    = $Computer<br />            Filter          = $null<br />            DisplayName     = 'OFFLINE'<br />            Publisher       = $null<br />            InstallDate     = $null<br />            DisplayVersion  = $null<br />            UninstallString = $null<br />            QueryDate       = $Date<br />        }<br />    }<br />    else {</p><p>        .\Set-WinRM.ps1 -Mode enable -ComputerName $Computer<br />        #This should be outside the loop as the function is returning ALL software, so you just need to do it once<br />        $apps = Get-InstalledApps -ComputerName $Computer</p><p>        #Loop through the apps to see if they exist in the results returned from query<br />        foreach ( $Program in $ProgramList ){<br />            $appQuery = $apps | Where {$_.DisplayName -like "*$Program*"}</p><p>            if ($appQuery) {<br />                [pscustomobject]@{<br />                    Computername    = $Computer<br />                    Filter          = $Program<br />                    DisplayName     = $appQuery.DisplayName<br />                    Publisher       = $appQuery.Pubilisher<br />                    InstallDate     = $appQuery.InstallDate<br />                    DisplayVersion  = $appQuery.DisplayVersion<br />                    UninstallString = $appQuery.UninstallString<br />                    QueryDate       = $Date<br />                }<br />            }<br />            else {<br />                [pscustomobject]@{<br />                    Computername    = $Computer<br />                    Filter          = $Program<br />                    DisplayName     = 'NOT_INSTALLED'<br />                    Publisher       = $null<br />                    InstallDate     = $null<br />                    DisplayVersion  = $null<br />                    UninstallString = $null<br />                    QueryDate       = $Date<br />                }<br />            }<br />        }</p><p>        .\Set-WinRM.ps1 -Mode disable -ComputerName $Computer<br />    }<br />}<br />

      This allows you to filter to determine outcomes with DisplayName.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.