简体   繁体   中英

Substring ForEach-Object into Variable

Sorry if this is a really basic question, like many people I am learning PowerShell as I go along with specific needs/projects. Right now, I need to move a bunch of Computers from one OU to another OU in AD, based on a string in the msDS-AuthenticatedAtDC attribute.

Right now I'm just trying to get a substring from that attribute, and set it as a variable. I have started out with this:

$Computers = Get-ADComputer -Properties * -SearchBase 
"OU=NG,OU=Workstations,OU=Production,DC=MyDC,DC=MYDC2,DC=MyDC3" |Select msDS-AuthenticatedAtDC 

Now for each of those objects, I need to get the 5th to 10th characters in a variable. So something like a

ForEach-Object {$CountryCode = $_.Substring(4,5)}

But that's not working of course. Does anyone have a suggestion? Once I get that part of the puzzle I can work on the Move-ADObject, because the variable will make up part of my target path.

Thank you!

Not sure why the above didn't work, but I ended up getting what I want with this:

foreach($Object in $Computers){ 
$Auth = $Object.'msDS-AuthenticatedAtDC' 
$CC = $Auth.Substring(4,2) 
$PC = $Auth.Substring(4,5) 
$CN = $Object.CN 
Move-ADObject -Identity "CN=$CN,OU=Production,DC=MyDC,DC=MyDC2,DC=MyDC3" - 
TargetPath "OU=$PC,OU=$CC,OU=Workstations,OU=Production,DC=MyDC,DC=MyDC2,DC=MyDC3"}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM