Issue with array length property

by Nov 28, 2012

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:

function GetInstalledSoftware 
{
    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:templogs" 
        #$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-Module activedirectory 
        Import-Module psremoteregistry 
        $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.ArrayList

        Write-Host "In GetInstalledSoftware function. Entering while loop."
        while($compList.length -gt 0)
        {

            $u = 1 
            Write-Host "Calling VerifyConnectivity function"
            $Responded = VerifyConnectivity $compList 
            $message = "Done with VerifyConnectivity function. Now testing Responded.count which = $Responded"
            Write-Host $message

            if ($Responded.length -gt 0) #Why does this evaluate to less than 0?!?!
            {

                Write-Host "Processing responding computers: '$Responded'"
                foreach ($client in $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 each
                    foreach($key in $subkeys){

                        $thisKey = $UninstallKey + "\" + $key 

                        $thisSubKey = $reg.OpenSubKey($thisKey) 

                        $obj = New-Object PSObject

                        $obj | Add-Member -MemberType NoteProperty -Name "ObjectName" -Value $client

                        $obj | Add-Member -MemberType NoteProperty -Name "Registry_Key_Name" -Value $Key

                        $obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))

                        $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))

                        $obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))

                        $obj | Add-Member -MemberType NoteProperty -Name "VersionMinor" -Value $($thisSubKey.GetValue("VersionMinor"))

                        $obj | Add-Member -MemberType NoteProperty -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 -eq 0)
            {
                Write-Host "Responded = 0 "
            }
            elseif($Responded.Length -lt 0)
            {
                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($computer in $global:Dead)
            {
                #$compList = $global:Dead | SELECT Computer
                $tempCompList += $computer.Computer
                Write-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 = $tempCompList
            Write-Host "End of while loop"
            
            $global:softwareList | Export-Csv installedApps.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.