计算机是根据用户名,根据用户名移动计算机账号

上周客户提出了一个要求,将Computers里面的计算机账号移动到相应的OU下,由于数量比较大,采用脚本。

在客户环境中计算机账号名和用户名一致。这样问题处理就比较简单了,先获取此OU下面的用户名,然后和Computers计算机账号名一一匹配,如果一致,则移动此计算机账号到对应OU下。

自定义powershell命令SearchMove-ADComputer

使用方法:

将与“ou=test,dc=contoso,dc=com”下用户名一致的计算机账号移动到“ou=computers,ou=test,dc=contoso,dc=com” 下

SearchMove-ADComputer –SearchBase “ou=test,dc=contoso,dc=com” –TargetPath “ou=computers,ou=test,dc=contoso,dc=com”

1.将以下PS代码保存为.PS1文件

2.在Powershell Console中运行此文件

3.Import-Module ActiveDirectory

4.运行SearchMove-ADComputer指令

PS代码如下:

function SearchMove-ADComputer

([string] $SearchBase,

[string] $TargetPath)

{

$count = 0

$users = Get-ADUser -Filter * -SearchBase $SearchBase |Select-Object samaccountname

#SearchBase后面的参数需要改为自己环境的域名

$computers = Get-ADComputer -Filter * -SearchBase "cn=computers,dc=contoso,dc=com" |Select-Object DistinguishedName,name

$result = @()

for ($index = 0; $index -le ($computers.Length - 1); $index++)

{

foreach ($item in $users)

{

if($item.samaccountname -contains ($computers[$index].Name) -eq $true )

{

$ComputerName = New-Object -TypeName PSObject

$ComputerName | Add-Member NoteProperty ComputerName $computers[$index].Name

$ComputerName | Add-Member NoteProperty DN $computers[$index].DistinguishedName

$result += $ComputerName

}

}

}

Start-Sleep 2

if($result.count -eq 0)

{

Write-Host "没有查询出相关账号,请更改查询条件"

}

else

{

#移动到Computers OU下

foreach ($computerid in $result)

{

Move-ADObject -Identity $computerid.DN -TargetPath $TargetPath

$count++

}

Write-Host "共计移动"$count"个计算机账号" -ForegroundColor Green

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值