Cannot index into a null array error

Welcome Forums General PowerShell Q&A Cannot index into a null array error

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
2 years, 1 month ago.

  • Author
    Posts
  • #24336

    Participant
    Points: 0
    Rank: Member

    Hi there,

    I am getting Cannot index into a null array error. I run the script below.

    ******************************************************
    Function check-even ($num) {[bool]!($num%2)}

    foreach ($file in (Get-ChildItem -Path c:\Users\rsimmers\desktop\*.ini)) {
    "Processing file {0}" -f $file
    $newfilename = $file.fullname -replace ".ini", ".txt"
    $newContent = foreach ($line in (Get-Content $file.fullname)){
    $printer = $line -match '\| (.*?)"'
    $printerName = $Matches[1]
    $printerNum = $printerName.Split("-")[2]

    if((check-even $printerNum) -eq $true) {
    #even
    $line.Replace('Host="ACFILE01"','Host="VJJJRT02"').Replace('Host="effile01"','Host="VJJJRT02"')
    }
    else {
    #odd
    $line.Replace('Host="ACFILE01"','Host="VSHHPRT01"').Replace('Host="ACFILE01"','Host="VSHHPRT01"')
    }
    }
    }

    $newContent
    $newContent | Out-File $newfilename

    ******************************************************************

    Processing file \\vfs04\Users$\kaendm\Desktop\Test00E8E627F9C.ini
    Cannot index into a null array.
    At C:\Powershell\TReplaceIniValues.ps1:12 char:9
    + $printerName = $Matches[1]
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

    You cannot call a method on a null-valued expression.
    At C:\Powershell\TReplaceIniValues.ps1:13 char:9
    + $printerNum = $printerName.Split("-")[2]
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

  • #24337

    Keymaster
    Points: 1
    Rank: Member

    That is because $matches doesn't contain anything. That means this:

    $printer = $line -match '\| (.*?)"'
    

    Didn't produce any matches. I'd consider adding some verbose output, so you can see what variables like $printer and $line and $matches actually contain as your script runs.

  • #24345

    Participant
    Points: 0
    Rank: Member

    Don,

    I have added the following Write-Verbose ($Matches | Out-String) -Verbose to the script. And I am getting the attached messages. See attach

    Function check-even ($num) {[bool]!($num%2)}

    foreach ($file in (Get-ChildItem -Path "\\vfs04\Users$\kaendm\Desktop\Test\*ini")) {
    "Processing file {0}" -f $file
    $newfilename = $file.fullname -replace ".ini", ".txt"
    $newContent = foreach ($line in (Get-Content $file.fullname)){
    $printer = $line -match '\| (.*?)"'

    Write-Verbose ($Matches | Out-String) -Verbose

    $printerName = $Matches[1]
    $printerNum = $printerName.Split("-")[2]

    if((check-even $printerNum) -eq $true) {
    #even
    $line.Replace('Host="ACFILE01"','Host="VJJJRT02"').Replace('Host="effile01"','Host="VJJJRT02"')
    }
    else {
    #odd
    $line.Replace('Host="ACFILE01"','Host="VSHHPRT01"').Replace('Host="ACFILE01"','Host="VSHHPRT01"')
    }
    }
    }

    $newContent
    $newContent | Out-File $newfilename

  • #24347

    Keymaster
    Points: 1
    Rank: Member

    It's as I said. This line:

    $printer = $line -match '\| (.*?)"'

    Is not producing any matches. I don't know what $line contains, so I can't tell you why it isn't producing any matches, but it isn't. You might try working with your file content and your regular expression interactively in the shell to see what's going wrong.

  • #53151

    Participant
    Points: 0
    Rank: Member

    Hello Don... Need your help and expertise to remove the null array error.

    Below script will check for DHCP Server option 252 in all the scope of server DHCPServer1 & DHCPServer2. Each server has 20 scopes and not all the scopes have option 252 configured. how to add "No Option Configured" in the exported file.

    Please help.

    ##################Error################
    Cannot index into a null array.
    At M:\Get-DHCPScopeOption.ps1:43 char:17
    + $row.ScopeProxy = $ScopeDNSList[0]
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
    ##################Script################
    import-module DHCPServer
    #Get all Authorized DCs from AD configuration
    $DHCPs = 'DHCPServer1','DHCPServer2'
    # $DHCPs = Get-DhcpServerInDC
    $filename = "M:\DHCPScopes_Proxy_$(get-date -Uformat "%Y%m%d-%H%M%S").csv"

    $Report = @()
    $k = $null
    write-host -foregroundcolor Green "`n`n`n`n`n`n`n`n`n"
    foreach ($dhcp in $DHCPs) {
    $k++
    Write-Progress -activity "Getting DHCP scopes:" -status "Percent Done: " `
    -PercentComplete (($k / $DHCPs.Count) * 100) -CurrentOperation "Now processing $($dhcp)"
    $scopes = $null
    $scopes = (Get-DhcpServerv4Scope -ComputerName $dhcp -ErrorAction:SilentlyContinue)
    If ($scopes -ne $null) {
    #getting global Proxy settings, in case scopes are configured to inherit these settings
    $GlobalProxyList = $null
    $GlobalProxyList = (Get-DhcpServerv4OptionValue -OptionId 252 -ComputerName $dhcp -ErrorAction:SilentlyContinue).Value
    $scopes | % {
    $row = "" | select Hostname,ScopeID,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,ScopeProxy,GlobalProxy
    $row.Hostname = $dhcp
    $row.ScopeID = $_.ScopeID
    $row.SubnetMask = $_.SubnetMask
    $row.Name = $_.Name
    $row.State = $_.State
    $row.StartRange = $_.StartRange
    $row.EndRange = $_.EndRange
    $row.LeaseDuration = $_.LeaseDuration
    $row.Description = $_.Description
    $ScopeProxyList = $null
    $ScopeProxyList = (Get-DhcpServerv4OptionValue -OptionId 252 -ScopeID $_.ScopeId -ComputerName $dhcp -ErrorAction:SilentlyContinue).Value
    #write-host "Q: Use global scopes?: A: $(($$ScopeProxyList -eq $null) -and ($$GlobalProxyList -ne $null))"
    If (($$ScopeProxyList -eq $null) -and ($$GlobalProxyList -ne $null)) {
    $row.GlobalProxy = $GlobalProxyList[0]
    $row.ScopeProxy = $GlobalProxyList[0]
    }
    Else {
    $row.ScopeProxy = $ScopeProxyList[0]
    }
    $Report += $row
    }
    }
    Else {
    write-host -foregroundcolor Yellow """$($dhcp)"" is either running Windows 2003, or is somehow not responding to querries. Adding to report as blank"
    $row = "" | select Hostname,ScopeID,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,ScopeProxy,GlobalProxy
    $row.Hostname = $dhcp
    $Report += $row
    }
    write-host -foregroundcolor Green "Done Processing ""$($dhcp)"""
    }

    $Report | Export-csv -NoTypeInformation -UseCulture $filename
    ######################################

  • #53157

    Participant
    Points: 0
    Rank: Member

    and where is string from error line in your script ?

    Cannot index into a null array.
     At M:\Get-DHCPScopeOption.ps1:43 char:17
     + $row.ScopeProxy = $ScopeDNSList[0]
    

    where $ScopeDNSList variable defined ?

  • #53389

    Participant
    Points: 0
    Rank: Member

    Hello Max,

    Please ignore the error in above post. Below is the error.

    #################################################
    Cannot index into a null array.
    At M:\Get-DHCPScopeOption.ps1:43 char:17
    + $row.ScopeProxy = $ScopeProxyList[0]
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
    #################################################

  • #53445

    Participant
    Points: 0
    Rank: Member

    Avijit Dutta, you should have created your own post to get help instead of using someone else's thread. Also, when you paste code, be sure to enclose your code with the pre tags so it formats it nicely.

    I don't have the DHCPServer module, so haven't been able to test anything, so hope I got it right first go:

    import-module DHCPServer
    
    #Get all Authorized DCs from AD configuration
    $DHCPs = 'DHCPServer1','DHCPServer2'
    
    # $DHCPs = Get-DhcpServerInDC
    $filename = "M:\DHCPScopes_Proxy_$(get-date -Uformat "%Y%m%d-%H%M%S").csv"
    $Report = @()
    $k = $null
    
    write-host -foregroundcolor Green "`n`n`n`n`n`n`n`n`n"
    
    foreach ($dhcp in $DHCPs) {
    
    	$k++
    	Write-Progress -activity "Getting DHCP scopes:" -status "Percent Done: " `
    		-PercentComplete (($k / $DHCPs.Count) * 100) -CurrentOperation "Now processing $($dhcp)"
    		
    	$scopes = $null
    	$scopes = (Get-DhcpServerv4Scope -ComputerName $dhcp -ErrorAction:SilentlyContinue)
    	
    	If ($scopes -ne $null) {
    	
    		#getting global Proxy settings, in case scopes are configured to inherit these settings
    		$GlobalProxyList = $null
    		$GlobalProxyList = (Get-DhcpServerv4OptionValue -OptionId 252 -ComputerName $dhcp -ErrorAction:SilentlyContinue).Value
    		
    		$scopes | % {
    			$row = "" | select Hostname,ScopeID,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,ScopeProxy,GlobalProxy
    			$row.Hostname = $dhcp
    			$row.ScopeID = $_.ScopeID
    			$row.SubnetMask = $_.SubnetMask
    			$row.Name = $_.Name
    			$row.State = $_.State
    			$row.StartRange = $_.StartRange
    			$row.EndRange = $_.EndRange
    			$row.LeaseDuration = $_.LeaseDuration
    			$row.Description = $_.Description
    			
    			$row.GlobalProxy = 'No Option Configured'
    			$row.ScopeProxy = 'No Option Configured'
    			
    			$ScopeProxyList = $null
    			$ScopeProxyList = (Get-DhcpServerv4OptionValue -OptionId 252 -ScopeID $_.ScopeId -ComputerName $dhcp -ErrorAction:SilentlyContinue).Value
    			
    			#write-host "Q: Use global scopes?: A: $(($$ScopeProxyList -eq $null) -and ($$GlobalProxyList -ne $null))"
    
    			If($ScopeProxyList){
    			
    				$row.ScopeProxy = $ScopeProxyList[0]
    				
    				If($GlobalProxyList){
    					$row.GlobalProxy = $GlobalProxyList[0]
    				}
    
    			} Else {
    				If($GlobalProxyList){
    					$row.GlobalProxy = $GlobalProxyList[0]
    					$row.ScopeProxy = $GlobalProxyList[0]
    				}
    			}
    			
    			$Report += $row
    		}
    	} Else {
    		write-host -foregroundcolor Yellow """$($dhcp)"" is either running Windows 2003, or is somehow not responding to querries. Adding to report as blank"
    		
    		$row = "" | select Hostname,ScopeID,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,ScopeProxy,GlobalProxy
    		$row.Hostname = $dhcp
    		$Report += $row
    	}
    	
    	write-host -foregroundcolor Green "Done Processing ""$($dhcp)"""
    }
    
    $Report | Export-csv -NoTypeInformation -UseCulture $filename
    ######################################
    

The topic ‘Cannot index into a null array error’ is closed to new replies.