Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
jsclmedave

Administrator
Registered:
Posts: 500
Reply with quote  #1 
Using CimSession's to pull the applications listed in a Windows Servers Registry.

Thanks goes out to Jeff Hicks, Mike F Robbins, Richard Siddaway, Adam Bertram and everyone else that worked with me on this...






Function Get-AllApps {
<#
.SYNOPSIS
    Creates CimSessions to remote computer(s), automatically determining if the WSMAN
    or Dcom protocol should be used. !!  THIS IS CURRENT BROKEN SINCE WSMAN IS NOT WORKING AT THIS TIME !!
.DESCRIPTION
    Get-AllApps is a TEST function that is designed to create a CimSession to one (or more
    Computers one CimSession at a time) automatically determining if the default WSMAN protocol or the backwards
    compatible Dcom protocol should be used.  The function will then query the Target Servers REGSITRY and 
    display the version of the BMC RSCD Agent installed on that Target Server.
    PowerShell version 3 is required on the computer that this function is being run on, but PowerShell does not need to be
    installed at all on the remote computer.
.PARAMETER ComputerName
    The Friendly Name (Example: BigDogUtil01 ) of the remote computer(s). This parameter accepts pipeline input. The local
    computer is the default.
.PARAMETER Credential
    If needed, this specifies a user account that has permission to perform this action. The default is
    the current user. NOTE! At the time this was created my account has Servers access so this was not used.
.EXAMPLE
     Get-AllApps -ComputerName Server01, Server02
.EXAMPLE
     Get-AllApps -ComputerName Server01, Server02 -Credential (Get-Credential)
.EXAMPLE
     Get-Content -Path C:\Servers.txt | Get-AllApps
.INPUTS
    String
.OUTPUTS
    Microsoft.Management.Infrastructure.CimSession
.NOTES
    Original Authors:  
    
    Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins
    PowerShell Function to Create CimSessions to Remote Computers with Fallback to Dcom
    http://mikefrobbins.com/2014/08/28/powershell-function-to-create-cimsessions-to-remote-computers-with-fallback-to-dcom/

    Jeff Hicks
    Website: http://jdhitsolutions.com/blog/ 
    Twitter: @JeffHicks
    PowerShell Problem Solver: Finding Installed Software Using CIM Cmdlets
    https://www.petri.com/powershell-problem-solver-finding-installed-software-part-4

    Richard Siddaway
    Website: https://richardspowershellblog.wordpress.com/
    Twitter: @RSiddaway
    Registry Cmdlets: Working with the Registry
    http://blogs.technet.com/b/heyscriptingguy/archive/2015/02/02/registry-cmdlets-manage-the-registry.aspx
    Hey, Dude! Where Are My Methods?
    http://blogs.technet.com/b/heyscriptingguy/archive/2013/09/20/hey-dude-where-are-my-methods.aspx
    
    Current Version Authors:
    
    Tim Bolton Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ''
    Website: http://timbolton.net/
    Twitter: @jsclmedave
    TechNet
    https://social.technet.microsoft.com/Search/en-US?query=Tim%20Bolton&rn=Hey,%20Scripting%20Guy!%20Blog&rq=site:blogs.technet.com/b/heyscriptingguy/&beta=0&ac=4
    
Don Walker Email: [string](0..22|%{[char][int](32+('8488146879781487657675698232717765737614677977').substring(($_*2),2))}) -replace ''
    Website:
    Twitter: @EngineeringDon
    Weekend Scripter: Understanding Quotation Marks in PowerShell
    http://blogs.technet.com/b/heyscriptingguy/archive/2015/06/20/weekend-scripter-understanding-quotation-marks-in-powershell.aspx

#>
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)]
        [ValidateNotNullorEmpty()]
        [string[]]$ComputerName = $env:COMPUTERNAME,
 
        [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
    )
 
    BEGIN {
        $Opt = New-CimSessionOption -Protocol Dcom
        $SessionParams = @{
            ErrorAction = 'Stop'
        }
 
        If ($PSBoundParameters['Credential']) {
            $SessionParams.Credential = $Credential
        }
    } # End Begin
#######################################
 PROCESS {
            $Report = @()
            Foreach ($Computer in $ComputerName) 
            {
            "`r`n"
# Checking connection to Server If cannot connect drop this server and go to the next one.
        Try {
            $Check1 = Test-Connection $Computer -Count 1 -ErrorAction Stop | Select-Object Address,IPV4Address
            write-Host 'Test-Connection successful.  Now testing scanning access of '  $Check1.Address  $Check1.IPV4Address -ForegroundColor Yellow
            "`r`n" 
            }
            Catch [System.Net.NetworkInformation.PingException] {$Computer + ' is offline or not accessible. Check computer name.'
            Continue
            }
# Set From Above
            $SessionParams.ComputerName  = $Computer
            if ((Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue).productversion -match 'Stack: ([3-9]|[1-9][0-9]+)\.[0-9]+') {
                try {
                    Write-Verbose -Message "Attempting to connect to $Computer using the WSMAN protocol."
                    $CS = New-CimSession @SessionParams
                }
                catch {
                    Write-Warning -Message "Unable to connect to $Computer using the WSMAN protocol. Verify your credentials and try again."
                }
            }
 
            else {
                $SessionParams.SessionOption = $Opt
                try {
                    Write-Verbose -Message "Attempting to connect to $Computer using the DCOM protocol."
                    $CS = New-CimSession @SessionParams
                }
                catch {
                    Write-Warning -Message "Unable to connect to $Computer using the WSMAN or DCOM protocol. Verify $Computer is online and try again."
                }
                $SessionParams.Remove('SessionOption')
            }            
        

#########################
    $HKEY_USERS=2147483651 
    $HKLM=2147483650
    $rpaths = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
    "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
#initialize $data
$data = @()
 
$regcim = Get-CimClass -Namespace root\default -class StdRegProv -CimSession $CS
 
 
foreach ($rpath in $rpaths) {
    Write-Verbose "Querying $rpath"
    
    $enumArgs = @{hDefKey=$HKLM;sSubKeyName=$rpath}
 
    $paramHash = @{
     cimclass = $regcim
     CimSession = $cs
     Name = "EnumKey"
     Arguments = $enumArgs
    }
 
    $mySnames = Invoke-CimMethod @paramHash | select -expand snames | where {$_ -notmatch '(\.)?KB\d+'} 
    $i=0
    $data += foreach ($item in $MySnames) {
 
    $i++
    
    $keyPath = "$rpath\$item"
    
    #revise paramhash
    $paramHash.Name = "EnumValues"
    $paramHash.Arguments = @{hDefKey=$HKLM;sSubKeyName=$keyPath}
    Invoke-CimMethod @paramHash | foreach {
       #get value data
       $hash = [ordered]@{Path = $KeyPath}
 
       #add a list of known properties
       "Displayname","DisplayVersion","Publisher",
       "InstallDate","InstallLocation","Comments","UninstallString" | foreach { 
   
       $paramHash.Name = "GetStringValue"
       $paramhash.Arguments = @{hDefKey = $HKLM ;sSubKeyName=$KeyPath;sValueName=$_}
       $value = Invoke-CimMethod @paramhash 
       $hash.Add($_,$($value.sValue))
      } #foreach property name
 
       #write a custom object to the pipeline
       [pscustomobject]$hash
    } #foreach subkey name
 
    } #foreach sname
 
} #foreach rpath
 
#write results to pipeline
$data | Out-Gridview -Title "Registry Listed Apps For: $Computer"
Write-Host 'Removeing The Current CIM-Session.' -ForegroundColor Yellow
    Get-CimSession | Remove-CimSession
         } # End Foreach Computer in ComputerName
    } # End Process
} # End Function




AllApps.gif


__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
MichaelB

Master of all Exchange
Registered:
Posts: 15
Reply with quote  #2 
Does this work if Set-StrictMode is enabled?

Regards,
Michael B.
0
jsclmedave

Administrator
Registered:
Posts: 500
Reply with quote  #3 
Michael,

I have not tried that...  I "do" have Admin rights to the Windows servers that I am targeting.  

This "just always works" in my current environment which is why I did not add any error logging.

__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.