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!
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}
LikeLike
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
LikeLike
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’
}
LikeLike
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
LikeLike
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
LikeLike