locked
Remove characters from array output RRS feed

  • Question

  • Hi I am fairly new to Powershell and I'm trying to get some automation going, I have built the below array to capture all software installed in the environment, which will be converted to HTML to upload to a confluence page. The capture itself works fine but always happy to hear better ways of doing this. However, because of certain characters, the automation bit to confluence fails does and I cant for the life of me understand how to remove these characters from the output of the below. "," "()" , "+" Please note I have taken bits from others code to achieve the below so far. The properties that contain the characters are display name and publisher.

        
    $computers = Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' | Select-Object name
    
    $array = @()
    
    foreach($pc in $computers){
    
        $computername=$pc.name
    
        #Define the variable to hold the location of Currently Installed Programs
    
        $UninstallKey=”SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall” 
    
        #Create an instance of the Registry Object and open the HKLM base key
    
        $reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey(‘LocalMachine’,$computername) 
    
        #Drill down into the Uninstall key using the OpenSubKey Method
    
        $regkey=$reg.OpenSubKey($UninstallKey) 
    
        #Retrieve an array of string that contain all the subkey names
    
        $subkeys=$regkey.GetSubKeyNames() 
    
        #Open each Subkey and use GetValue Method to return the required values for each
    
        foreach($key in $subkeys){
    
            $thisKey=$UninstallKey+”\\”+$key 
    
            $thisSubKey=$reg.OpenSubKey($thisKey) 
    
            $obj = New-Object PSObject
    
            $obj | Add-Member -MemberType NoteProperty -Name “ComputerName” -Value $computername
    
            $obj | Add-Member -MemberType NoteProperty -Name “DisplayName” -Value $($thisSubKey.GetValue(“DisplayName”)) -remo
    
            $obj | Add-Member -MemberType NoteProperty -Name “DisplayVersion” -Value $($thisSubKey.GetValue(“DisplayVersion”))
    
            #$obj | Add-Member -MemberType NoteProperty -Name “InstallLocation” -Value $($thisSubKey.GetValue(“InstallLocation”))
    
            $obj | Add-Member -MemberType NoteProperty -Name “Publisher” -Value $($thisSubKey.GetValue(“Publisher”))
    
            $array += $obj
    
        } 
    
    }
    

    $array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion, Publisher
    Thursday, May 14, 2020 9:49 AM

Answers

  • This should work:

    $computers = Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' | Select-Object name
    $array = 
    foreach ($pc in $computers) {
        $computername = $pc.name
        $UninstallKey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'
        $reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine', $computername) 
        $regkey = $reg.OpenSubKey($UninstallKey) 
        $subkeys = $regkey.GetSubKeyNames() 
        foreach ($key in $subkeys) {
            $thisKey = $UninstallKey + '\\' + $key 
            $thisSubKey = $reg.OpenSubKey($thisKey) 
    
            [PSCustomObject]@{
                ComputerName   = $ComputerName
                DisplayName    = $thisSubKey.GetValue('DisplayName')
                DisplayVersion = ($thisSubKey.GetValue('DisplayVersion')) -replace ',|\(|\)|\+'
                Publisher      = $thisSubKey.GetValue('Publisher')
            }
        } 
    }
    $array


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by GWynne Thursday, May 14, 2020 1:33 PM
    Thursday, May 14, 2020 10:42 AM

All replies

  • This should work:

    $computers = Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' | Select-Object name
    $array = 
    foreach ($pc in $computers) {
        $computername = $pc.name
        $UninstallKey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'
        $reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine', $computername) 
        $regkey = $reg.OpenSubKey($UninstallKey) 
        $subkeys = $regkey.GetSubKeyNames() 
        foreach ($key in $subkeys) {
            $thisKey = $UninstallKey + '\\' + $key 
            $thisSubKey = $reg.OpenSubKey($thisKey) 
    
            [PSCustomObject]@{
                ComputerName   = $ComputerName
                DisplayName    = $thisSubKey.GetValue('DisplayName')
                DisplayVersion = ($thisSubKey.GetValue('DisplayVersion')) -replace ',|\(|\)|\+'
                Publisher      = $thisSubKey.GetValue('Publisher')
            }
        } 
    }
    $array


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by GWynne Thursday, May 14, 2020 1:33 PM
    Thursday, May 14, 2020 10:42 AM
  • Thank you that worked perfectly
    Thursday, May 14, 2020 1:33 PM
  • You can shorten (and make more understandable) the regex by using a character class and avoid all that escaping:

           "A,B(C)D+" -replace '[,()+]'
          
    ABCD


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by BOfH-666 Thursday, May 14, 2020 6:47 PM
    Thursday, May 14, 2020 3:05 PM