Hello. I am new to PS but a novice programmer, mainly in Java. I am modding a script I found on this site to collect the relevant data that I need and am confused by an array length. The code returns an array that has one object in it but when evaluating the length, it the array evaluates to less than 0. What am I missing?
The output of this section of code is as follows:
Done with VerifyConnectivity function. Now testing Responded.count which = @{Computer=Frobozz01}
Responded < 0
compList = 'Frobozz01 DummyComputer01 DummyComputer02' \nAssigning global:Dead to compList
tempCompList is now == to: 'DummyComputer01'
computer = '(@{Computer=DummyComputer01; IPAddress=; Responding=False} | select computer)'
tempCompList is now == to: 'DummyComputer01 DummyComputer02'
computer = '(@{Computer=DummyComputer02; IPAddress=; Responding=False} | select computer)'
Final tempCompList is: 'DummyComputer01 DummyComputer02'
Now assigning tempCompList to compList
End of while loop
The code is as follows:
functionGetInstalledSoftware {param ( [parameter(ValueFromPipeline=$true)] $compList ) BEGIN {$d=Get-Date$strDate=$d.ToString() $month=$d.Month$day=$d.Day$year=$d.Year$cDate="$month-$day-$year"$global:logFilePath="C:\temp\logs\"#$NoPrtMapLog = $logFilePath + "NoMappedPrinters-" + $cDate + ".log" #$WmiErrorLog = $logFilePath + "WmiError-" + $cDate + ".log" #$MappedPrinters = $logFilePath + "MappedPrinters-" + $cDate + ".csv" #$NoUsrLoggedIn = $logFilePath + "NoUsrLoggedIn-" + $cDate + ".log" $RemoteRegNotRunning=$logFilePath+"RemoteRegNotRunning-"+$cDate+".log"$ErrorActionPreference='SilentlyContinue'Import-ModuleactivedirectoryImport-Modulepsremoteregistry$global:wmiErrors=@() $global:NoUserLoggedIn=@() #$CompUserInfo = @() #$arrCompLogonInfo = @() $arrRemoteRegSvcStopped=@() #$arrNoMappedPrinters = @() #$arrMappedPrinters = @() #$statusMSG = "Getting Logged on User Information" #$statusMSG2 = "Getting User SID from Active Directory" #$statusMSG3 = "Collecting Mapped Printer Information" #Define the variable to hold the location of Currently Installed Programs$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"$global:softwareList=@()$global:deadComputers=@() } PROCESS {#$arrCompList = New-Object System.Collections.ArrayListWrite-Host"In GetInstalledSoftware function. Entering while loop."while($compList.length-gt0) {$u=1Write-Host"Calling VerifyConnectivity function"$Responded=VerifyConnectivity$compList$message="Done with VerifyConnectivity function. Now testing Responded.count which = $Responded"Write-Host$messageif ($Responded.length-gt0)#Why does this evaluate to less than 0?!?! {Write-Host"Processing responding computers: '$Responded'"foreach ($clientin$Responded) {Write-Host"Processing '$client'"#Create an instance of the Registry Object and open the HKLM base key$reg= [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$client) #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 eachforeach($keyin$subkeys){$thisKey=$UninstallKey+"\\"+$key$thisSubKey=$reg.OpenSubKey($thisKey) $obj=New-ObjectPSObject$obj|Add-Member-MemberTypeNoteProperty-Name"ObjectName"-Value$client$obj|Add-Member-MemberTypeNoteProperty-Name"Registry_Key_Name"-Value$Key$obj|Add-Member-MemberTypeNoteProperty-Name"DisplayVersion"-Value$($thisSubKey.GetValue("DisplayVersion"))$obj|Add-Member-MemberTypeNoteProperty-Name"DisplayName"-Value$($thisSubKey.GetValue("DisplayName"))$obj|Add-Member-MemberTypeNoteProperty-Name"Publisher"-Value$($thisSubKey.GetValue("Publisher"))$obj|Add-Member-MemberTypeNoteProperty-Name"VersionMinor"-Value$($thisSubKey.GetValue("VersionMinor"))$obj|Add-Member-MemberTypeNoteProperty-Name"VersionMajor"-Value$($thisSubKey.GetValue("VersionMajor"))#$obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))$global:softwareList+=$obj } Write-Host"Done Processing '$client'" } }elseif($Responded.Length-eq0) {Write-Host"Responded = 0 " }elseif($Responded.Length-lt0) {Write-Host"Responded < 0 " }else {Write-Host"I don't know what the *** is going on!" }Write-Host"compList = '$compList' \nAssigning global:Dead to compList"$tempCompList=@()foreach($computerin$global:Dead) {#$compList = $global:Dead | SELECT Computer$tempCompList+=$computer.ComputerWrite-Host"tempCompList is now == to: '$tempCompList'"Write-Host"computer = '($computer | select computer)'" }Write-Host"Final tempCompList is: '$tempCompList'"Write-Host"Now assigning tempCompList to compList"$compList=$tempCompListWrite-Host"End of while loop"$global:softwareList|Export-CsvinstalledApps.txt-notypeinformation-Delimiter`t-Append$global:softwareList=@() } } END {#$arrMappedPrinters | Export-Csv -Path $MappedPrinters #Add-Content $NoPrtMapLog $arrNoMappedPrinters #Add-Content $WmiErrorLog $wmiErrors #Add-Content $NoUsrLoggedIn $global:NoUserLoggedIn #Add-Content $RemoteRegNotRunning $arrRemoteRegSvcStopped } }
Thanks for any help.