Reputation: 109
My computerlist is a CSV file with 3 columns (Machinename, New_AU, and New_State).
I'm trying to move each machine in my computer list to it's TargetOU. The TargetOU for each machine is based on the New_AU and New_State. I'm not sure how to get this information from the CSV file. Here is what I have so far. I appreciate any and all help on this matter.
Import-Module ActiveDirectory
$ComputerList = Import-Csv -Path "C:\Temp\Scripts\AUchange\Computerlist.csv"
$TargetOU = "OU=$New_AU,OU=$New_State,OU=ST,OU=EN,DC=en,DC=wb,DC=bk,DC=cp"
foreach ($Computer in $ComputerList) {
Get-ADComputer $Computer |
Move-ADObject -Server AD-server.en.wb.bk.cp -TargetPath $TargetOU
}
Upvotes: 0
Views: 47
Reputation: 6860
OK lets go over whats wrong.
The variables $New_AU and $New_State are not defined in script so right now there Null.
$ComputerList = Import-Csv -Path C:\Temp\Scripts\AUchange\Computerlist.csv
$TargetOU = "OU=$New_AU,OU=$New_State,OU=ST,OU=EN,DC=en,DC=wb,DC=bk,DC=cp"
Solution : I am going off the fact you said that your $ComputerList should be an Array of objects that contain multiable objects with the properties New_AU, New_State and MachineName
If thats the case you could use :
Import-Module ActiveDirectory
Import-Csv -Path C:\Temp\Scripts\AUchange\Computerlist.csv | %{
$TargetOU = "OU=$($_.New_AU),OU=$($_.New_State),OU=ST,OU=EN,DC=en,DC=wb,DC=bk,DC=cp"
Get-ADComputer $_.Machinename | Move-ADObject -Server AD-server.en.wb.bk.cp -TargetPath $TargetOU
}
What is happening
In powershell | means Pipe. This will send the data from the previous command to the next one in the pipe | and the variable for that data will be $_
So we Import-CSV which turns the file into a Powershell Object. We then Pipe | that object to a %{} which is shorthand for ForEach-Object
We set the $TargetOU with a string using $() which means expression (It means run this section as script not as string. Then we call the object data passed from Import-CSV using $_ and then call each property needed $_.PropertyName.
We then Get-AdComputer using the piped | data $_ with property MachineName that we got from Import-csv. We pipe that Get-AdComputer Object to Move-ADObject where we set the -TargetPath parameter to $TargetOU
Upvotes: 1