将主机名批量移动到所需的 OU
Bulk hostname movement to desired OU
我有一个用于将主机名移动到特定 OU 的脚本。
gc .\list.txt |ForEach-Object { 获取 ADComputer $_ |移动 adobject -targetpath "" }
在上面的脚本中实际上有 150 个主机名,需要移动不同的不同 ou。我们如何做到这一点。
示例。
masuadfl01 -- move to masu/branches/laptops
masufgd002 -- move to masu/branches/desktops
abdufghd001 -- move to abdu/branches/desktops
像上面一样,一切都应该自动移动正确的 OU。我们如何创建这样的脚本。
如果是桌面最后一个,数字D 就会出现。对于笔记本电脑 L 将是。
我不会使用仅列出计算机名的文本文件,而是为此创建一个包含两列的 CSV 文件。
第一列包含计算机名称,第二列包含要将计算机移动到的 OU 的 DistinguishedName。
"ComputerName","Destination"
"masuadfl01","OU=laptops,OU=branches,OU=masu,DC=Contoso,DC=com"
"masufgd002","OU=desktops,OU=branches,OU=masu,DC=Contoso,DC=com"
"abdufghd001","OU=desktops,OU=branches,OU=abdu,DC=Contoso,DC=com"
那么你的代码可能是这样的:
Import-Csv -Path 'Path\To\The\File.csv' | ForEach-Object {
Get-ADComputer $_.ComputerName | Move-ADObject -TargetPath $_.Destination
}
编辑
如果计算机名称中涉及某种常量策略,那么您可以利用它来确定目标 OU,而不必创建 CSV 文件。
例如:如果所有计算机名称都以 masu
或 abdu
开头,并且这是其中一个子 OU 的名称,您可以将其用作变量。
至于台式机或笔记本电脑,每个名称都有一个 l
或 d
字符作为最终数字,如果确实表明它是笔记本电脑或台式电脑,我们可以使用它来制作 OU完整的专有名称:
# create a template OU distinguished name string
$targetOU = "OU={0},OU=branches,OU={1},DC=Contoso,DC=com"
Get-Content -Path 'Path\To\The\ComputerNamesFile.txt' | ForEach-Object {
$subOU = $_.Substring(0,4)
if ($_ -match '([ld])\d+$') {
$type = switch ($matches[1]) {
'l' { 'laptops'; break }
'd' { 'desktops' }
}
$moveTo = $targetOU -f $type, $subOU
Get-ADComputer $_ | Move-ADObject -TargetPath $moveTo
}
else {
Write-Warning "Cannot determine the computertype for '$_'"
}
}
这里的开销有点大,但是您可以为此使用正则表达式
首先,设置正则表达式
# will match a d,l,D or L character before the digit
[regex]$regex = "[dlDL](?=\d)"
然后您可以遍历您的主机名列表
Get-Content -Path .\list.txt | ForEach-Object {
# check if the hostname matches the regex
$match = $regex.Match($_)
# get the desired OU, first 4 characters
$desiredOU = $_.Substring(0,4)
If($match.Success){
# if client is L -> move to laptops
If($match.Value.ToUpper() -eq "L"){
Write-Host "$_ is a laptop" -ForegroundColor Green
Get-ADComputer $_ | Move-ADObject -TargetPath "OU=laptops,OU=branches,OU=$desiredOU,DC=Contoso,DC=com"
# if client is D -> move to desktops
}Elseif($match.Value.ToUpper() -eq "D"){
Write-Host "$_ is a desktop" -ForegroundColor Green
Get-ADComputer $_ | Move-ADObject -TargetPath "OU=desktops,OU=branches,OU=$desiredOU,DC=Contoso,DC=com"
}Else{
Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
}
}Else{
Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
}
}
我有一个用于将主机名移动到特定 OU 的脚本。
gc .\list.txt |ForEach-Object { 获取 ADComputer $_ |移动 adobject -targetpath "" }
在上面的脚本中实际上有 150 个主机名,需要移动不同的不同 ou。我们如何做到这一点。
示例。
masuadfl01 -- move to masu/branches/laptops
masufgd002 -- move to masu/branches/desktops
abdufghd001 -- move to abdu/branches/desktops
像上面一样,一切都应该自动移动正确的 OU。我们如何创建这样的脚本。
如果是桌面最后一个,数字D 就会出现。对于笔记本电脑 L 将是。
我不会使用仅列出计算机名的文本文件,而是为此创建一个包含两列的 CSV 文件。
第一列包含计算机名称,第二列包含要将计算机移动到的 OU 的 DistinguishedName。
"ComputerName","Destination"
"masuadfl01","OU=laptops,OU=branches,OU=masu,DC=Contoso,DC=com"
"masufgd002","OU=desktops,OU=branches,OU=masu,DC=Contoso,DC=com"
"abdufghd001","OU=desktops,OU=branches,OU=abdu,DC=Contoso,DC=com"
那么你的代码可能是这样的:
Import-Csv -Path 'Path\To\The\File.csv' | ForEach-Object {
Get-ADComputer $_.ComputerName | Move-ADObject -TargetPath $_.Destination
}
编辑
如果计算机名称中涉及某种常量策略,那么您可以利用它来确定目标 OU,而不必创建 CSV 文件。
例如:如果所有计算机名称都以 masu
或 abdu
开头,并且这是其中一个子 OU 的名称,您可以将其用作变量。
至于台式机或笔记本电脑,每个名称都有一个 l
或 d
字符作为最终数字,如果确实表明它是笔记本电脑或台式电脑,我们可以使用它来制作 OU完整的专有名称:
# create a template OU distinguished name string
$targetOU = "OU={0},OU=branches,OU={1},DC=Contoso,DC=com"
Get-Content -Path 'Path\To\The\ComputerNamesFile.txt' | ForEach-Object {
$subOU = $_.Substring(0,4)
if ($_ -match '([ld])\d+$') {
$type = switch ($matches[1]) {
'l' { 'laptops'; break }
'd' { 'desktops' }
}
$moveTo = $targetOU -f $type, $subOU
Get-ADComputer $_ | Move-ADObject -TargetPath $moveTo
}
else {
Write-Warning "Cannot determine the computertype for '$_'"
}
}
这里的开销有点大,但是您可以为此使用正则表达式
首先,设置正则表达式
# will match a d,l,D or L character before the digit
[regex]$regex = "[dlDL](?=\d)"
然后您可以遍历您的主机名列表
Get-Content -Path .\list.txt | ForEach-Object {
# check if the hostname matches the regex
$match = $regex.Match($_)
# get the desired OU, first 4 characters
$desiredOU = $_.Substring(0,4)
If($match.Success){
# if client is L -> move to laptops
If($match.Value.ToUpper() -eq "L"){
Write-Host "$_ is a laptop" -ForegroundColor Green
Get-ADComputer $_ | Move-ADObject -TargetPath "OU=laptops,OU=branches,OU=$desiredOU,DC=Contoso,DC=com"
# if client is D -> move to desktops
}Elseif($match.Value.ToUpper() -eq "D"){
Write-Host "$_ is a desktop" -ForegroundColor Green
Get-ADComputer $_ | Move-ADObject -TargetPath "OU=desktops,OU=branches,OU=$desiredOU,DC=Contoso,DC=com"
}Else{
Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
}
}Else{
Write-Host "$_ not a laptop nor desktop" -ForegroundColor Red
}
}