我有一个将主机名移到特定OU的脚本。
gc .\list.txt \ForEach-Object{ Get-ADComputer $_ \ move-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将是。
发布于 2021-04-14 10:10:15
与其使用只列出计算机名称的文本文件,我将创建一个带有两列的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 od 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 '$_'"
}
}https://stackoverflow.com/questions/67088861
复制相似问题