Need a little help with this conundrum.

I have a script that get installed software from multiple computers at time and stores the data into custom objects for later use. This script is by no means finalized and there may be some weird looking code. This is just an excerpt from it in order to keep it a small post. Sorry can’t post to pastebin. The real question deals with the portion of code highlighted in with what should be bold markup. When querying a computer’s registry for software and no results come back that usually means that the key doesn’t exist(aka “not installed”). When I formed the if statement without ($Output.ComputerName -notcontains $Computer) I get results back that say a software is installed and also “not installed”. I, by accident, meant to include “-or” in between the criteria but didn’t, and somehow received the results that I wanted. This is my question: how does the “-” only give me the results I want. It doesn’t make logical sense to me.

NOTE: $ComputerON and $ComputerOFF are lists created by a test-connection for a list of computers. Also sorry for not including the whole script. Hope what I said makes sense. First time posting.

Foreach($Computer in $ComputerON){ $Parameters = @{ Computer = $Computer RegKeys = $RegKeys SoftwareName = $SoftwareName SoftwareVersion = $SoftwareVersion } $job = [PowerShell]::Create().AddScript({ Param($Computer,$RegKeys,$SoftwareName,$SoftwareVersion) $Output = New-Object System.Collections.ArrayList Try{ $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$Computer) foreach($RegKey in $RegKeys){ Try{ $UninstallKey = $Reg.OpenSubKey($RegKey).GetSubKeyNames() } Catch [System.Management.Automation.RuntimeException]{ Continue } foreach($subkey in $UninstallKey){ foreach($soft in $SoftwareName){ $value = $Reg.OpenSubKey("$RegKey\$subkey") if( ($value.getvalue("DisplayName") -ne $null) -and ($value.getvalue("DisplayName") -match $soft) -and ($value.getvalue("DisplayVersion") -match $SoftwareVersion) -and ($value.getvalue("ParentKeyName") -eq $null) -and ($value.getvalue("ReleaseType") -eq $null) -and (($value.getvalue("UninstallString") -or $value.getvalue("NoRemove")) -ne $null) ){ if($value.getvalue('InstallDate') -ne $null){ $InstallDate = ([Datetime]::ParseExact($value.getvalue('InstallDate'),'yyyyMMdd',$null).ToShortDateString())} else{ $InstallDate = $value.getvalue('InstallDate') } $list = New-Object PSObject $list | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $Computer $list | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $value.getvalue("DisplayName") $list | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $value.getvalue("DisplayVersion") $list | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $value.getvalue("Publisher") $list | Add-Member -MemberType NoteProperty -Name "InstallDate" -Value $InstallDate $list | Add-Member -MemberType NoteProperty -Name "UninstallString" -Value $value.getvalue("UninstallString") if($list -ne $null){$Output.Add($list) | Out-Null} } } } } **foreach($soft in $SoftwareName){ if(($Output.DisplayName -notlike "*$soft*") - ($Output.ComputerName -notcontains $Computer)){ [void]$Output.Add([pscustomobject]@{ ComputerName = $Computer DisplayName = "$soft Not Installed" DisplayVersion = "$soft Not Installed" Publisher = "$soft Not Installed" InstallDate = "$soft Not Installed" UninstallString = "$soft Not Installed" }) } }** } Catch [System.Management.Automation.MethodInvocationException]{ $list = New-Object PSObject $list | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $Computer $list | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value "Access Denied" $list | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value "Access Denied" $list | Add-Member -MemberType NoteProperty -Name "Publisher" -Value "Access Denied" $list | Add-Member -MemberType NoteProperty -Name "InstallDate" -Value "Access Denied" $list | Add-Member -MemberType NoteProperty -Name "UninstallString" -Value "Access Denied" if($list -ne $null){$Output.Add($list) | Out-Null} Continue } Catch{ } $Output 

submitted by /u/ForNoPurpose
[link] [comments]

Leave a Reply