这里是Powershell新手,我的第一个脚本。
我有一个用户对象,它的AD自定义属性名为tvCode,值为123456、6787682或983736等。
我想编写一些脚本,从user对象中获取tvCode值
When:
123456 = Sony
6787682 = Samsung
9837343 = LG将"Sony“、"Samsung”或"LG“的值写入用户对象的"City”属性。
看起来我可能需要使用一个哈希表。
如果可能,对特定OU执行此操作
希望这是有意义的
谢谢
发布于 2017-04-29 12:48:19
function Convert-TVCode {
Param
(
[parameter(Mandatory=$true,Position=0,ValueFromPipeline=$true)]
[String[]]
$Code
)
Process {
foreach ($C in $Code) {
switch ($C) {
"123456" {$brand = "Sony"}
"6787682" {$brand = "Samsung"}
"9837343" {$brand = "LG"}
default {
$brand = $null
Write-Warning "$C not included in switch statement. Returning"
return
}
}
if ($brand) {
Write-Verbose "Code '$C' matched to Brand '$brand' -- searching for users to update"
Get-ADUser -Filter "tvCode -eq '$C'" | Set-ADUser -Replace @{tvCode=$brand}
}
}
}
}此函数将允许您更新任何将其tvCode属性设置为目标数值之一的用户。你也可以让它同时命中多个代码。
示例:
Convert-TVCode -Code 123456
Convert-TVCode -Code 123456,6787682
Convert-TVCode -Code 123456,6787682,9837343 -Verbose更新函数中的switch语句,将其自定义为您的实际值,如果您有任何问题,请告诉我!
https://stackoverflow.com/questions/43686012
复制相似问题