Created
January 25, 2017 13:14
-
-
Save gindox/f71e91b6b5b77e628a643a05a6f70b63 to your computer and use it in GitHub Desktop.
Power
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-RemoteProgram { | |
| [CmdletBinding(SupportsShouldProcess=$true)] | |
| param( | |
| [Parameter(ValueFromPipeline =$true, | |
| ValueFromPipelineByPropertyName=$true, | |
| Position=0 | |
| )] | |
| [string[]] | |
| $ComputerName = $env:COMPUTERNAME, | |
| [Parameter(Position=0)] | |
| [string[]] | |
| $Property, | |
| [switch] | |
| $ExcludeSimilar, | |
| [int] | |
| $SimilarWord | |
| ) | |
| begin { | |
| $RegistryLocation = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\', | |
| 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\' | |
| $HashProperty = @{} | |
| $SelectProperty = @('ProgramName','ComputerName') | |
| if ($Property) { | |
| $SelectProperty += $Property | |
| } | |
| } | |
| process { | |
| foreach ($Computer in $ComputerName) { | |
| $RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer) | |
| $RegistryLocation | ForEach-Object { | |
| $CurrentReg = $_ | |
| if ($RegBase) { | |
| $CurrentRegKey = $RegBase.OpenSubKey($CurrentReg) | |
| if ($CurrentRegKey) { | |
| $CurrentRegKey.GetSubKeyNames() | ForEach-Object { | |
| if ($Property) { | |
| foreach ($CurrentProperty in $Property) { | |
| $HashProperty.$CurrentProperty = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue($CurrentProperty) | |
| } | |
| } | |
| $HashProperty.ComputerName = $Computer | |
| $HashProperty.ProgramName = ($DisplayName = ($RegBase.OpenSubKey("$CurrentReg$_")).GetValue('DisplayName')) | |
| if ($DisplayName) { | |
| New-Object -TypeName PSCustomObject -Property $HashProperty | | |
| Select-Object -Property $SelectProperty | |
| } | |
| } | |
| } | |
| } | |
| } | ForEach-Object -Begin { | |
| if ($SimilarWord) { | |
| $Regex = [regex]"(^(.+?\s){$SimilarWord}).*$|(.*)" | |
| } else { | |
| $Regex = [regex]"(^(.+?\s){3}).*$|(.*)" | |
| } | |
| [System.Collections.ArrayList]$Array = @() | |
| } -Process { | |
| if ($ExcludeSimilar) { | |
| $null = $Array.Add($_) | |
| } else { | |
| $_ | |
| } | |
| } -End { | |
| if ($ExcludeSimilar) { | |
| $Array | Select-Object -Property *,@{ | |
| name = 'GroupedName' | |
| expression = { | |
| ($_.ProgramName -split $Regex)[1] | |
| } | |
| } | | |
| Group-Object -Property 'GroupedName' | ForEach-Object { | |
| $_.Group[0] | Select-Object -Property * -ExcludeProperty GroupedName | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment