Welcome Forums General PowerShell Q&A Invoke-Command Get-AdOrganizationalUnit

Viewing 5 reply threads
  • Author
    Posts
    • #172751
      Participant
      Topics: 53
      Replies: 54
      Points: 329
      Rank: Contributor

      I’ve written a script that will take User input of an OU name and the initial part of the Script will ‘Validate’ the input but checking to see if the OU exists.

      I need to run the Get-ADOrganizationUnit cmdlet with the scriptblock of the Invoke-command while the user supplies it with the correct credentials.

      I am having problems running it since the $OU variable will need to be passed to the Invoke-Command as a string.  I think I am getting hung up on when to use double quotes and when to use curly brackets and how to pass a variable to the ‘filter’ parameter of Get-ADOrganizationalUnit

      This is essentially what I am trying to do.

      #user Input
      $OUName = "NSSS"
      
      $ScriptBlockContent = {Get-ADOrganizationalUnit -Filter "Name -eq $Using:OU"}
      
      Invoke-Command -ComputerName ADServer -ScriptBlock $ScriptBlockContent -Credential domain\user

      This of course fails. I have tried removing the curly brackets from the $ScriptContent variable and creating a string. Can’t seem to find my error.

    • #172772
      Senior Moderator
      Topics: 8
      Replies: 1160
      Points: 4,056
      Helping Hand
      Rank: Community Hero

      What error message you are getting ?

    • #172820
      Participant
      Topics: 53
      Replies: 54
      Points: 329
      Rank: Contributor
      Invoke-Command : The value of the using variable '$using:OU' cannot be retrieved because it has not been set in the local session.
      At line:6 char:1
      + Invoke-Command -ComputerName 10.221.21.3 -ScriptBlock $ScriptBlockCon ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [Invoke-Command], RuntimeException
      + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand
      • #172829
        Participant
        Topics: 0
        Replies: 28
        Points: 142
        Helping Hand
        Rank: Participant

        Can you try this?

        $OUName = “NSSS”
        Invoke-Command -ComputerName ADServer -ScriptBlock {Get-ADOrganizationalUnit -Filter “Name -eq $($args[0])”} -Credential domain\user -ArgumentList $OUName
        
    • #172832
      Participant
      Topics: 53
      Replies: 54
      Points: 329
      Rank: Contributor

      Result
      Error parsing query: ‘Name -eq NSSS’ Error Message: ‘syntax error’ at position: ’10’.
      + CategoryInfo          : ParserError: (:) [Get-ADOrganizationalUnit], ADFilterParsingException
      + FullyQualifiedErrorId : ActiveDirectoryCmdlet:<wbr />Microsoft.ActiveDirectory.<wbr />Management.<wbr />ADFilterParsingException,<wbr />Microsoft.ActiveDirectory.<wbr />Managemen
      t.Commands.<wbr />GetADOrganizationalUnit
      + PSComputerName        : 10.221.21.3<

    • #172862
      Senior Moderator
      Topics: 8
      Replies: 1160
      Points: 4,056
      Helping Hand
      Rank: Community Hero

      your variable name is OUName, hence you should use $Using:OUName

    • #172877
      Participant
      Topics: 10
      Replies: 119
      Points: 535
      Helping Hand
      Rank: Major Contributor

      Try this:

      $ouName = 'NSSS'
      
      Invoke-Command -ComputerName ADServer -Credential domain\user -ArgumentList $ouName -ScriptBlock {
          param($ouName = $ouName)
      
          Get-ADOrganizationalUnit -Filter "Name -eq '$ouName'"
      }

       

      Edit: You do not have use the param() block; just refer to the argument using $($args[0]) as per the previous poster. This was just a ‘fuller’ approach.

Viewing 5 reply threads
  • The topic ‘Invoke-Command Get-AdOrganizationalUnit’ is closed to new replies.