locked
Getting error "Object reference not set to an instance of an object." RRS feed

  • Question

  • Hi Folks,

    I am running a script to find top 5 largest files remotely.

    Invoke-Command -computername (Read-Host -Prompt 'Enter Hostname') -Credential (Get-Credential) -Authentication Negotiate -ScriptBlock {Get-ChildItem -Path (Read-Host -Prompt "Path") -recurse -ErrorAction "SilentlyContinue"  | where-Object {$_.Length -gt 1024MB} | sort-Object -property length -Descending | Select-Object -first 5  Name, @{Name="Size (GB)";Expression={[math]::truncate($_.Length /1GB)}},@{Name="Path";Expression={$_.directory}} | Select-Object -Property * -ExcludeProperty PSComputerName,RunspaceID | Format-table -AutoSize}

    Above is my code, this works fins if i execute this from Windows machine.

    While running this code from a Linux machine i get below error.

    Object reference not set to an instance of an object.
        + CategoryInfo          : OperationStopped: (srv-****-****:String) [], PSRemotingTransportException
        + FullyQualifiedErrorId : JobFailure
        + PSComputerName        : srv-****-****

    Some one help me with this.

    Regards,

    NaveenRaj

    Tuesday, October 10, 2017 11:42 PM

All replies

  • It would be helpful if you posted your code correctly.  Use the code posting tool and format the lines correctly.

    Please!


    \_(ツ)_/

    Tuesday, October 10, 2017 11:49 PM
  • Invoke-Command -computername (Read-Host -Prompt 'Enter Hostname') -Credential (Get-Credential) -Authentication Negotiate -ScriptBlock {Get-ChildItem -Path (Read-Host -Prompt "Path") -recurse -ErrorAction "SilentlyContinue"  | where-Object {$_.Length -gt 1024MB} | sort-Object -property length -Descending | Select-Object -first 5  Name, @{Name="Size (GB)";Expression={[math]::truncate($_.Length /1GB)}},@{Name="Path";Expression={$_.directory}} | Select-Object -Property * -ExcludeProperty PSComputerName,RunspaceID | Format-table -AutoSize}

    Tuesday, October 10, 2017 11:58 PM
  • Since you are new I will fix the post for you and show you how to use remoting so it  works easier and returns correct information.

     $sb = {
        Get-ChildItem -Path (Read-Host -Prompt Path) -recurse -ErrorAction SilentlyContinue  | 
            where-Object {$_.Length -gt 1Gb}
     }
     $computername =Read-Host -Prompt 'Enter Hostname'
     Invoke-Command -ScriptBlock $sb -computername  -Credential (Get-Credential) |
        sort-Object -property length -Descending | 
        Select-Object Name, 
                      @{m='Size (GB)';e={[math]::truncate($_.Length /1GB)}},
                      @{Name='Path';Expression={$_.directory}}  -first 5  |
        Format-table -AutoSize

    Now it is readable and shouldn't give you null object errors


    \_(ツ)_/

    Tuesday, October 10, 2017 11:59 PM
  • Invoke-Command -computername (Read-Host -Prompt 'Enter Hostname') -Credential (Get-Credential) -Authentication Negotiate -ScriptBlock {Get-ChildItem -Path (Read-Host -Prompt "Path") -recurse -ErrorAction "SilentlyContinue"  | where-Object {$_.Length -gt 1024MB} | sort-Object -property length -Descending | Select-Object -first 5  Name, @{Name="Size (GB)";Expression={[math]::truncate($_.Length /1GB)}},@{Name="Path";Expression={$_.directory}} | Select-Object -Property * -ExcludeProperty PSComputerName,RunspaceID | Format-table -AutoSize}

    That is not correctly formatted code and it is still unreadable.


    \_(ツ)_/

    Wednesday, October 11, 2017 12:01 AM
  • It still works good in windows but not sure when i execute this script from a Linux powershell it gives me this error 

    Object reference not set to an instance of an object.
        + CategoryInfo          : OperationStopped: (srv-****-****:String) [], PSRemotingTransportException
        + FullyQualifiedErrorId : JobFailure
        + PSComputerName        : srv-****-****

    Wednesday, October 11, 2017 12:13 AM
  • Linux PowerShell does not support much of the Windows components.

    Try doing only the invoke;

    Just run this to be sure the Linux components are good.

    $sb = {
    	Get-ChildItem -Path c:\ -recurse 
    }
    $computername = Read-Host -Prompt 'Enter Hostname'
    Invoke-Command -ScriptBlock $sb -computername -Credential (Get-Credential)


    \_(ツ)_/

    Wednesday, October 11, 2017 12:24 AM
  • Yes the above command works good also if i specify drive name as "

    Get-ChildItem -Path "C:\"

    C:\" on to my original script, it works as well.

    Only when i ask the script to prompt for drive name to enter its throwing error do we not have any other options to over come this error?

    Get-ChildItem -Path (Read-Host -Prompt 'Path')
    Wednesday, October 11, 2017 12:35 AM
  • Linux remoting does not support remote prompting as it is based on PS V2 and Net V2 so you need to prompt outside of the invoke.  That is why I moved it out.


    \_(ツ)_/

    Wednesday, October 11, 2017 12:44 AM
  • Thanks a lot for the Great support, i will try some more options, if i stuck then will come back here :).
    Wednesday, October 11, 2017 12:58 AM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, October 13, 2017 9:22 AM