Powershell performing 2 action to an object with 1 line?

Discussion in 'Programming & Software Development' started by Multiplexer, Jun 22, 2020.

  1. Multiplexer

    Multiplexer Member

    Joined:
    Feb 26, 2002
    Messages:
    2,121
    Location:
    Home
    I need to apply a Set-ADObject and Move-ADObject to a computer object. How can I perform the 2 change with 1 line? I tried piping and did not work. Sequence is important if you have a look at what it does.

    Get-ADComputer -Identity $computer | Set-ADObject -ProtectedFromAccidentalDeletion:$false
    Get-ADComputer -Identity $computer | Move-ADObject -TargetPath $destinationOU
     
  2. Hookimus

    Hookimus Member

    Joined:
    Jan 11, 2016
    Messages:
    65
    Location:
    Sydney
    There's a couple of different ways you can do it:

    1. Wrap up the two lines in to a function:
      Function Move-ADComputerCustom ($Computer) {
      $a = Get-ADComputer -identity $Computer
      Set-ADObject -identity $a -ProtectedFromAccidentalDeletion:$false
      Move-ADObject -identity $a -TargetPath $destinationOU
      }

      Once you've initialized the function then you can use:
      Move-ADComputerCustom $Computer

    2. Use ; to indicate separate commands in the one line or inside of { }
      $a = Get-ADComputer -Identity $computer ; Set-ADObject -identity $a -ProtectedFromAccidentalDeletion:$false ; Move-ADObject -identity $a -TargetPath $destinationOU
     
  3. OP
    OP
    Multiplexer

    Multiplexer Member

    Joined:
    Feb 26, 2002
    Messages:
    2,121
    Location:
    Home
    I have figure it out. Put -passthru at the end of Set-ADObject. Now it is one line
     

Share This Page

Advertisement: