Impractical One-liner Challenge

Update

My thanks to Mike F. Robbins and others on reddit who pointed out that the command should be ‘Select -ExpandProperty DistinguishedName’, and not organizationalUnit.

The Challenge

A colleague and I got into a competition earlier today. How could we display an Out-Gridview of all of a companies OU’s, and then move a computer to the selected one in a single line of code.

We condensed our code down to the following two lines:

$destinationOU =  Get-ADObject -Filter 'ObjectClass -eq "organizationalUnit"' | Select -ExpandProperty DistinguishedName -Unique | Out-Gridview -passthru

Get-Content .\Computers.txt | Get-QADObject | Move-QADObject -NewParentContainer $destinationOU -whatif

The goal? Make it into a one-liner.

GUIDELINES:

  • Maintain the steps if possible.  Minimally acceptable solution:
    • Get user input for which OU
    • Get a list of computers and move them to the OU
  • NO CHEATING WITH Semicolons or backticks
    Doing the below doesn’t count

    $ou = Out-Gridview;gc Computers.txt | Move-Computer $ou

Nope, sorry, try harder.

  • Anything else goes!

Please comment here with your answers. I’ll post my own within a few days.  Also, if you have any ideas for a future impractical One-Liner Challenge, let me know here, Twitter, or Reddit!

Advertisements

6 thoughts on “Impractical One-liner Challenge

  1. Jonatan September 19, 2014 / 3:31 pm

    This was fun. I was able to come up with the following:

    (Get-ADOrganizationalUnit -Filter *).DistinguishedName | Out-GridView -PassThru -PipelineVariable ou| %{Import-Csv C:\computers.csv -PipelineVariable computers} | %{Move-ADObject -Identity $(Get-ADComputer $computers.computerName) -TargetPath $ou -WhatIf}

    • FoxDeploy September 19, 2014 / 3:49 pm

      Ohhhh, I love it Jonathan. At first I was puzzled by your use of the first ForEach, then I realized that you would only have one object, the OU you selected.

      Nice! I want to post my answer but I want to give more people a chance to reply ;D

  2. Mike F Robbins (@mikefrobbins) September 19, 2014 / 7:25 pm

    Looks like I ended up with a similar solution, although I didn’t see the previous one prior to creating mine 🙂

    Get-ADOrganizationalUnit -Filter * -Properties OU |
    Select-Object -Property @{label=’OU’;expression={$_.OU -replace ‘{|}’}},
    DistinguishedName |
    Out-GridView -OutputMode Single -Title ‘Select Destination OU’ |
    ForEach-Object {
    Get-Content -Path .\computers.txt |
    Get-ADComputer |
    Out-GridView -OutputMode Multiple -Title ‘Select Computers to Move’ -PipelineVariable info |
    Move-ADObject -TargetPath $_.DistinguishedName -PassThru |
    Select-Object -Property Name,
    @{label=’SourceOU’;expression={$info.DistinguishedName -replace ‘^(.*?)\,’}},
    @{label=’DestinationOU’;expression={$_.DistinguishedName -replace ‘^(.*?)\,’}} |
    Out-GridView -Title ‘Results’
    }

  3. christopherbarnettChris September 19, 2014 / 7:33 pm

    Here’s my go at it! 🙂

    Get-Content .\Computers.txt | Get-QADObject | Move-QADObject -NewParentContainer $(Get-ADObject -Filter ‘ObjectClass -eq “organizationalUnit”‘ | Select -ExpandProperty DistinguishedName -Unique | Out-Gridview -passthru) -whatif

  4. christopherbarnett September 19, 2014 / 7:37 pm

    I think this would be just fine as well, removing the select statement and just returning the .DistinguishedName of the $(Get-ADObject…)

    Get-Content .\Computers.txt | Get-QADObject | Move-QADObject -NewParentContainer $(Get-ADObject -Filter ‘ObjectClass -eq “organizationalUnit”‘ | Out-Gridview -passthru).DistinguishedName -whatif

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s