0

嗨,第一次在这里发帖和 PS 的新手,在我们的 AD 中,我们有一个名为 Disabled 的 OU 和 12 个按月命名的子 OU,我目前有一个脚本来禁用用户删除 o365 许可证,转换为共享邮箱并移动到禁用的 OU。我想做的是将禁用的用户移动到当月的 OU 禁用操作发生。

#edit here
$NewHire = "Some.Guy"
$mobph = "123-555-1212"
$stree = "155 any ST"
$cit = "Orlando"
$sta = "Florida"
$zip = "55555"
#end Edit
$user = Get-ADUser $NewHire -Properties 

mail,ProxyAddresses,Office,Company,Country,OfficePhone,streetaddress,city,state,PostalCode,MobilePhone
$user.ProxyAddresses = "SMTP:$NewHire@somedomain.com"
$user.mail = "$NewHire@somedomain.com"
$user.Office = "Remote"
$user.Company = "Company name"
$user.Country = "US"
$user.streetaddress = "$stree"
$user.city = "$cit"
$user.state = "$sta"
$user.PostalCode = "$zip"
$user.MobilePhone = "$mobph"
Set-ADUser -instance $user
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 
https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic - 
AllowRedirection
Import-PSSession $Session
Connect-MsolService
Get-Date -Format G
Start-Sleep -s 270
Disable-ADAccount -Identity "$NewHire"
Get-ADPrincipalGroupMembership "$NewHire" | Select Name | Export-CSV -path C:\MemberOf\$NewHire.csv - 
NoTypeInformation
Get-ADUser "$NewHire" -Properties MailNickName | Set-ADUser -Replace @{MailNickName = "$NewHire"}
Get-ADUser -Identity "$NewHire" -Properties MemberOf | ForEach-Object {
$_.MemberOf | Remove-ADGroupMember -Members $_.DistinguishedName -Confirm:$false
}
Get-ADUser "$newhire" | Move-ADObject -TargetPath "OU=Disabled Accounts,OU=North 
America,DC=123,DC=local"
Set-ADUser $NewHire -Replace @{msExchHideFromAddressLists=$true}

Set-MsolUserLicense -UserPrincipalName "$NewHire@somedomain.com" -RemoveLicenses "somecorp:SPE_E3"
4

1 回答 1

0

如果我理解正确,您似乎只需要更改将用户移动到标记为月份的子 OU 的 OU。

您可以让当前月份执行以下操作。您可以将“yyyy-MM”部分更改为您想要的格式。

$month = [datetime]::Today.ToString("yyyy-MM")

然后换行

Get-ADUser "$newhire" | Move-ADObject -TargetPath "OU=Disabled Accounts,OU=North America,DC=123,DC=local"

Get-ADUser "$newhire" | Move-ADObject -TargetPath "OU=$month,OU=Disabled Accounts,OU=North 
America,DC=123,DC=local"

请确保先创建 OU,然后再要求脚本将用户移至该 OU。

于 2021-01-30T08:00:23.233 回答