none
PowerShell Active Directory Command failed to execute RRS feed

  • Question

  • Greetings everyone,
    I am wondering why PowerShell is throwing error sometimes and sometimes running like a champ. I am using below command to get the desired output.
    Get-ADPrincipalGroupMembership '${username}' |  Get-ADGroup -Properties * | Select name,description

    The error is "Object reference not set to an instance of an object."

    Why this weird behavior ?

    Friday, February 2, 2018 6:25 AM

Answers

  • Hello Pawan,

    Please try below snippet.

    $groupMembership = Get-ADPrincipalGroupMembership '${username}'
    $data=$groupMembership |  Get-ADGroup -Properties * | Select name,description


    Friday, February 2, 2018 10:41 AM

All replies

  • Hello Pawan,

    Please try below snippet.

    $groupMembership = Get-ADPrincipalGroupMembership '${username}'
    $data=$groupMembership |  Get-ADGroup -Properties * | Select name,description


    Friday, February 2, 2018 10:41 AM
  • Hi Pawan,

    The error refers to the second part of your command (Get-ADGroup). The first part returned something that is not an object (possibly nothing at all). The second part (Get-ADGroup) then cannot select properties, because if it's not an object to begin with it has no properties. So it's a matter or input. This is also the reason why it works in some cases byt not all. You need to diagnose what the first command returns to figure out how to get rid of the errors on the second command.

    Incidentally, this ${Username} is not good writing of a variable. I get it to work reproducing it. But curly brackets are for enclosing blocks of code. Probably a better notation in your case is to simply rename the variable $UserName.

    Friday, February 2, 2018 10:51 AM
  • Thanks Deepak, now it is working like a champ :)
    Saturday, February 3, 2018 1:56 AM
  • Hi J.Couwenberg,

    My question is then why sometimes it works and sometimes not for the same user (every time before I ran script I used to clear all the variables by using Clear-Variable)?

    Regarding the writing variable in curly brackets, I'm sorry it was only for representational purpose.

    Saturday, February 3, 2018 2:02 AM