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

  • Question

  • I got error "Object reference not set to an instance of an object" error with the following (slim) PS scripts -

    ==========================

    Import-Module WebAdministration

    $webSites = Get-ChildItem IIS:\Sites

    for ($i=0;$i -le $webSites.Count-1;$i++)
    {
        func1 ($webSites[$i].Name)
    }

    function func1 ([String]$site_name)

    {

        func2 -siteName $site_name -para2 "mystring"

    }

    function func2

    {

    Param

    (

    [Parameter(Mandatory=$True, Position=0)]

    [String]$siteName,

    [Parameter(Mandatory=$True, Position=1)]

    [String]$para2

    )

    #do things with $siteName and $para2....

    }

    =============================

    The error is from the bold line, and it seems from the usage of '$site_name'.

    Can anyone help point out what's wrong here and how to fix it?

    Thanks in advance


    Tuesday, June 9, 2020 1:13 AM

All replies

  • When posting in a technical forum please format code correctly and post according to the forum rules.

    The following will give you the instructions on how to post:

    Plese post the full exception also.  We need all of the information.


    \_(ツ)_/


    • Edited by jrv Tuesday, June 9, 2020 1:51 AM
    Tuesday, June 9, 2020 1:49 AM
  • Note also that you cannot call a function before you have defined it.

    I recommend starting by learning basic PowerShell,

    Here is a good resource.  I recommend studying it before continuing with PowerShell.


    \_(ツ)_/

    Tuesday, June 9, 2020 1:53 AM
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

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

    Friday, July 3, 2020 3:33 AM