Invoke-Command Get-AdOrganizationalUnit

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

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #172751

    Participant
    Topics: 49
    Replies: 51
    Points: 268
    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: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    What error message you are getting ?

  • #172820

    Participant
    Topics: 49
    Replies: 51
    Points: 268
    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: 15
      Points: 61
      Helping Hand
      Rank: Member

      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: 49
    Replies: 51
    Points: 268
    Rank: Contributor

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

  • #172862

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

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

  • #172877

    Participant
    Topics: 10
    Replies: 59
    Points: 142
    Helping Hand
    Rank: Participant

    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.

You must be logged in to reply to this topic.