locked
Invoke-command issue RRS feed

  • Question

  • Hello Powesrhell GURUSSS!!!, i have a problem with my below code, don't know why it is showing me the error "Cannot index into a null array" I don't have any array on it and i've checked all the variables and they are not empty. this error only appears when I try to invoke it remotelly, if i run the code inside the server it works perfectly.

    Here is the code

    Invoke-Command -ComputerName "servername1"-ScriptBlock{
    Param (
    [parameter(ValueFromPipeline=$True)]
    [string[]]$ComputerName)
    Begin
    {
    $ComputerName = hostname
    }
    Process
    {   #---------------------------------------------------------------------
        # Process each ComputerName
        #---------------------------------------------------------------------
        if (!($PSCmdlet.MyInvocation.BoundParameters[“Verbose”].IsPresent))
        {
            Write-Host "Processing $ComputerName"
        }
        Write-Verbose "=====> Processing $ComputerName <====="
    $htmlreport = @()
        $htmlbody = @()
        $htmlfile = "ServerReport.html"
        $spacer = "<br />"
        #---------------------------------------------------------------------
        # Do 10 pings and calculate the fastest response time
        # Not using the response time in the report yet so it might be
        # removed later.
        #---------------------------------------------------------------------
        try
        {
            $bestping = (Test-Connection -ComputerName $ComputerName -Count 10 -ErrorAction STOP | Sort ResponseTime)[0].ResponseTime
        }
        catch
        {
            Write-Warning $_.Exception.Message
            $bestping = "Unable to connect"
        }
        if ($bestping -eq "Unable to connect")
        {
            if (!($PSCmdlet.MyInvocation.BoundParameters[“Verbose”].IsPresent))
            {
                Write-Host "Unable to connect to $ComputerName"
            }
            "Unable to connect to $ComputerName"
        }
        else
        {
            #---------------------------------------------------------------------
            # Collect computer system information and convert to HTML fragment
            #---------------------------------------------------------------------
            Write-Verbose "Collecting computer system information"
            $subhead = "<h3>Computer System Information</h3>"
            $htmlbody += $subhead
            try
            {
                $csinfo = Get-WmiObject Win32_ComputerSystem -ComputerName $ComputerName -ErrorAction STOP |
                    Select-Object Name,Manufacturer,Model,
                                @{Name='Physical Processors';Expression={$_.NumberOfProcessors}},
                                @{Name='Logical Processors';Expression={$_.NumberOfLogicalProcessors}},
                                @{Name='Total Physical Memory (Gb)';Expression={
                                    $tpm = $_.TotalPhysicalMemory/1GB;
                                    "{0:F0}" -f $tpm
                                }},
                                DnsHostName,Domain
           
                $htmlbody += $csinfo | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect operating system information and convert to HTML fragment
            #---------------------------------------------------------------------   
            Write-Verbose "Collecting operating system information"
            $subhead = "<h3>Operating System Information</h3>"
            $htmlbody += $subhead
            try
            {
                $osinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName -ErrorAction STOP | 
                    Select-Object @{Name='Operating System';Expression={$_.Caption}},
                                @{Name='Architecture';Expression={$_.OSArchitecture}},
                                Version,Organization,
                                @{Name='Install Date';Expression={
                                    $installdate = [datetime]::ParseExact($_.InstallDate.SubString(0,8),"yyyyMMdd",$null);
                                    $installdate.ToShortDateString()
                                }},
                                WindowsDirectory
                $htmlbody += $osinfo | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect physical memory information and convert to HTML fragment
            #---------------------------------------------------------------------
            Write-Verbose "Collecting physical memory information"
            $subhead = "<h3>Physical Memory Information</h3>"
            $htmlbody += $subhead
            try
            {
                $memorybanks = @()
                $physicalmemoryinfo = @(Get-WmiObject Win32_PhysicalMemory -ComputerName $ComputerName -ErrorAction STOP |
                    Select-Object DeviceLocator,Manufacturer,Speed,Capacity)

                foreach ($bank in $physicalmemoryinfo)
                {
                    $memObject = New-Object PSObject
                    $memObject | Add-Member NoteProperty -Name "Device Locator" -Value $bank.DeviceLocator
                    $memObject | Add-Member NoteProperty -Name "Manufacturer" -Value $bank.Manufacturer
                    $memObject | Add-Member NoteProperty -Name "Speed" -Value $bank.Speed
                    $memObject | Add-Member NoteProperty -Name "Capacity (GB)" -Value ("{0:F0}" -f $bank.Capacity/1GB)
                    $memorybanks += $memObject
                }
                $htmlbody += $memorybanks | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect pagefile information and convert to HTML fragment
            #---------------------------------------------------------------------
            $subhead = "<h3>PageFile Information</h3>"
            $htmlbody += $subhead
            Write-Verbose "Collecting pagefile information"
            try
            {
                $pagefileinfo = Get-WmiObject Win32_PageFileUsage -ComputerName $ComputerName -ErrorAction STOP |
                    Select-Object @{Name='Pagefile Name';Expression={$_.Name}},
                                @{Name='Allocated Size (Mb)';Expression={$_.AllocatedBaseSize}}

                $htmlbody += $pagefileinfo | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect BIOS information and convert to HTML fragment
            #---------------------------------------------------------------------
            $subhead = "<h3>BIOS Information</h3>"
            $htmlbody += $subhead
            Write-Verbose "Collecting BIOS information"
            try
            {
                $biosinfo = Get-WmiObject Win32_Bios -ComputerName $ComputerName -ErrorAction STOP |
                    Select-Object Status,Version,Manufacturer,
                                @{Name='Release Date';Expression={
                                    $releasedate = [datetime]::ParseExact($_.ReleaseDate.SubString(0,8),"yyyyMMdd",$null);
                                    $releasedate.ToShortDateString()
                                }},
                                @{Name='Serial Number';Expression={$_.SerialNumber}}
                $htmlbody += $biosinfo | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect logical disk information and convert to HTML fragment
            #---------------------------------------------------------------------
            $subhead = "<h3>Logical Disk Information</h3>"
            $htmlbody += $subhead
            Write-Verbose "Collecting logical disk information"
            try
            {
                $diskinfo = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName -ErrorAction STOP | 
                    Select-Object DeviceID,FileSystem,VolumeName,
                    @{Expression={$_.Size /1Gb -as [int]};Label="Total Size (GB)"},
                    @{Expression={$_.Freespace / 1Gb -as [int]};Label="Free Space (GB)"}
                $htmlbody += $diskinfo | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
    #---------------------------------------------------------------------
            # Collect volume information and convert to HTML fragment
            #---------------------------------------------------------------------
            $subhead = "<h3>Volume Information</h3>"
            $htmlbody += $subhead
            Write-Verbose "Collecting volume information"
            try
            {
                $volinfo = Get-WmiObject Win32_Volume -ComputerName $ComputerName -ErrorAction STOP | 
                    Select-Object Label,Name,DeviceID,SystemVolume,
                    @{Expression={$_.Capacity /1Gb -as [int]};Label="Total Size (GB)"},
                    @{Expression={$_.Freespace / 1Gb -as [int]};Label="Free Space (GB)"}
                $htmlbody += $volinfo | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect network interface information and convert to HTML fragment
            #---------------------------------------------------------------------    
            $subhead = "<h3>Network Interface Information</h3>"
            $htmlbody += $subhead
            Write-Verbose "Collecting network interface information"
            try
            {
                $nics = @()
                $nicinfo = @(Get-WmiObject Win32_NetworkAdapter -ComputerName $ComputerName -ErrorAction STOP | Where {$_.PhysicalAdapter} |
                    Select-Object Name,AdapterType,MACAddress,
                    @{Name='ConnectionName';Expression={$_.NetConnectionID}},
                    @{Name='Enabled';Expression={$_.NetEnabled}},
                    @{Name='Speed';Expression={$_.Speed/1000000}})
                $nwinfo = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -ErrorAction STOP |
                    Select-Object Description, DHCPServer,  
                    @{Name='IpAddress';Expression={$_.IpAddress -join '; '}},  
                    @{Name='IpSubnet';Expression={$_.IpSubnet -join '; '}},  
                    @{Name='DefaultIPgateway';Expression={$_.DefaultIPgateway -join '; '}},  
                    @{Name='DNSServerSearchOrder';Expression={$_.DNSServerSearchOrder -join '; '}}
                foreach ($nic in $nicinfo)
                {
                    $nicObject = New-Object PSObject
                    $nicObject | Add-Member NoteProperty -Name "Connection Name" -Value $nic.connectionname
                    $nicObject | Add-Member NoteProperty -Name "Adapter Name" -Value $nic.Name
                    $nicObject | Add-Member NoteProperty -Name "Type" -Value $nic.AdapterType
                    $nicObject | Add-Member NoteProperty -Name "MAC" -Value $nic.MACAddress
                    $nicObject | Add-Member NoteProperty -Name "Enabled" -Value $nic.Enabled
                    $nicObject | Add-Member NoteProperty -Name "Speed (Mbps)" -Value $nic.Speed        
                    $ipaddress = ($nwinfo | Where {$_.Description -eq $nic.Name}).IpAddress
                    $nicObject | Add-Member NoteProperty -Name "IPAddress" -Value $ipaddress
                    $nics += $nicObject
                }
                $htmlbody += $nics | ConvertTo-Html -Fragment
                $htmlbody += $spacer
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Collect software information and convert to HTML fragment
            #---------------------------------------------------------------------
            $subhead = "<h3>Software Information</h3>"
            $htmlbody += $subhead 
            Write-Verbose "Collecting software information"     
            try
            {
                $software = Get-WmiObject Win32_Product -ComputerName $ComputerName -ErrorAction STOP | Select-Object Vendor,Name,Version | Sort-Object Vendor,Name        
                $htmlbody += $software | ConvertTo-Html -Fragment
                $htmlbody += $spacer         
            }
            catch
            {
                Write-Warning $_.Exception.Message
                $htmlbody += "<p>An error was encountered. $($_.Exception.Message)</p>"
                $htmlbody += $spacer
            }
            #---------------------------------------------------------------------
            # Generate the HTML report and output to file
            #---------------------------------------------------------------------
            Write-Verbose "Producing HTML report"
            $reportime = Get-Date
            #Common HTML head and styles
        $htmlhead="<html>
        <style>
        BODY{font-family: Arial; font-size: 8pt;}
        H1{font-size: 20px;}
        H2{font-size: 18px;}
        H3{font-size: 16px;}
        TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}
        TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}
        TD{border: 1px solid black; padding: 5px; }
        td.pass{background: #7FFF00;}
        td.warn{background: #FFE600;}
        td.fail{background: #FF0000; color: #ffffff;}
        td.info{background: #85D4FF;}
        </style>
        <body>
        <h1 align=""center"">Server Info: $ComputerName</h1>
        <h3 align=""center"">Generated: $reportime</h3>"

            $htmltail = "</body>
        </html>"

            $htmlreport = $htmlhead + $htmlbody + $htmltail
            $htmlreport | Out-File c:\windows\temp\$htmlfile -Encoding Utf8
                
    }
    }
    End
    {

    This is the error

    Cannot index into a null array.
        + CategoryInfo          : InvalidOperation: (Verbose:String) [], RuntimeException
        + FullyQualifiedErrorId : NullArray
        + PSComputerName        : servername1

    PS: PSremote is available on the server that i am testing this, i've also test it with test-wsman and it is working, i've invoked a get-service and also works.

    PS C:\Windows\system32> Test-WSMan -ComputerName server

    wsmid           : http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd
    ProtocolVersion : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
    ProductVendor   : Microsoft Corporation
    ProductVersion  : OS: 0.0.0 SP: 0.0 Stack: 2.0

    Thanks, everyone for your help!.

     
    Thursday, October 19, 2017 2:53 PM

Answers

  • Please always post the complete error and post the lines the error is indicating.  We will not go through 100 lines of code to try and guess what is wrong.

    Also note that there are dozens of arrays in your code.

    Where did you find this script?


    \_(ツ)_/

    • Marked as answer by D.M.L Thursday, October 19, 2017 4:13 PM
    • Unmarked as answer by jrv Thursday, October 19, 2017 4:20 PM
    • Marked as answer by jrv Thursday, October 19, 2017 4:20 PM
    Thursday, October 19, 2017 3:01 PM

All replies

  • Please always post the complete error and post the lines the error is indicating.  We will not go through 100 lines of code to try and guess what is wrong.

    Also note that there are dozens of arrays in your code.

    Where did you find this script?


    \_(ツ)_/

    • Marked as answer by D.M.L Thursday, October 19, 2017 4:13 PM
    • Unmarked as answer by jrv Thursday, October 19, 2017 4:20 PM
    • Marked as answer by jrv Thursday, October 19, 2017 4:20 PM
    Thursday, October 19, 2017 3:01 PM
  • This is the full error and it only appears when i use invoke-command.

    Cannot index into a null array.
        + CategoryInfo          : InvalidOperation: (Verbose:String) [], RuntimeException
        + FullyQualifiedErrorId : NullArray
        + PSComputerName        : servername1

    • Marked as answer by D.M.L Thursday, October 19, 2017 4:12 PM
    • Unmarked as answer by D.M.L Thursday, October 19, 2017 4:14 PM
    • Marked as answer by D.M.L Thursday, October 19, 2017 4:14 PM
    • Unmarked as answer by jrv Thursday, October 19, 2017 4:19 PM
    Thursday, October 19, 2017 4:06 PM
  • Thanks, I don't pretend to check for the entire code, i don't know why it is failing when i run this remotely,  but it ok, i will set it as aswered, i will try to use psxcec :P, thanks again
    Thursday, October 19, 2017 4:14 PM
  • Sorry but we don't rewrite code found on the Internet.  You would do best to take some time to learn PowerShell.

    \_(ツ)_/

    Thursday, October 19, 2017 4:19 PM