delete/clear AD 属性值,如果属性包含类似 val 的值*
delete/clear AD attribute value if attribute contains value like val*
美好的一天!
我想清除名为 aaccountroles 的 AD 属性的特定值
像这样的概念:
如果此属性“aaccountroles”包含以“S4P any”开头的值,这意味着 S4P*,它应该删除这样的值
AD中属性的画面
导入-csv-路径.\test.csv | foreach{Set-ADUser $_.sAMAccountName -Clear aaccountroles}
此脚本清除所有值,但我想清除特定值
我的 test.csv 文件
我试着创建了这张纸条:
导入-csv-路径.\test.csv | foreach{Set-ADUser $_.sAMAccountName -清除@{aaccountroles="S4P*"}
但是报错
Set-ADUser : 指定的目录服务属性或值不存在
参数名称:System.Collections.Hashtable
在 line:1 char:39
- ... v | foreach{Set-ADUser $_.sAMAccountName -清除@{aaccountroles="S ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo:InvalidArgument:(test.user1:ADUser)[Set-ADUser],ArgumentException
- FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Set-ADUser
的 -Clear
参数需要一个 字符串 或 字符串数组 ,而不是哈希表。
您需要首先找到具有 'S4P*' 等属性的用户,如果发现清楚 属性:
Import-Csv -Path .\test.csv | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.sAMAccountName)' -and aaccountroles -like 'S4P*'" -Properties aaccountroles -ErrorAction SilentlyContinue
if ($user) {
Write-Host "Clearing the aaccountroles attribute for user $($user.Name)"
$user | Set-ADUser -Clear aaccountroles
}
}
使用 -Remove
、-Add
、-Replace
或 -Clear
和 Set-ADUser,您必须使用 LDAP 显示名称。我更喜欢使用正确的 LDAP 大小写,所以如果 属性 被称为 aaccountRoles
(大写 R),它也可以帮助在这里做到这一点
我不清楚你想完全清空属性(如上所示)只是取出以 ''S4P'..
开头的项目
假设 aaccountroles
是一个 数组 :
Import-Csv -Path .\test.csv | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.sAMAccountName)' -and aaccountroles -like 'S4P*'" -Properties aaccountroles -ErrorAction SilentlyContinue
if ($user) {
$roles = @($user.aaccountroles | Where-Object { $_ -match '\S' -and $_ -notlike 'S4P*' })
if ($roles.Count) {
Write-Host "Removing the 'S4P*' aaccountroles items for user $($user.Name)"
$user | Set-ADUser -Replace @{aaccountroles = [string[]]$roles}
}
else {
Write-Host "Clearing the aaccountroles attribute for user $($user.Name)"
$user | Set-ADUser -Clear aaccountroles
}
}
}
假设属性是多行字符串,而不是数组,你可以这样做:
Import-Csv -Path .\test.csv | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.sAMAccountName)' -and aaccountroles -like 'S4P*'" -Properties aaccountroles -ErrorAction SilentlyContinue
if ($user) {
# split the multiline string into an array and filter out the items that do not start with 'S4P'
$roles = @($user.aaccountroles -split '\r?\n' | Where-Object { $_ -match '\S' -and $_ -notlike 'S4P*' })
if ($roles.Count) {
Write-Host "Removing the 'S4P*' aaccountroles items for user $($user.Name)"
# rejoin the left over array items into a multiline string
$newRoles = $roles -join [environment]::NewLine
$user | Set-ADUser -Replace @{aaccountroles = $newRoles}
}
else {
Write-Host "Clearing the aaccountroles attribute for user $($user.Name)"
$user | Set-ADUser -Clear aaccountroles
}
}
}
美好的一天!
我想清除名为 aaccountroles 的 AD 属性的特定值 像这样的概念: 如果此属性“aaccountroles”包含以“S4P any”开头的值,这意味着 S4P*,它应该删除这样的值
AD中属性的画面
导入-csv-路径.\test.csv | foreach{Set-ADUser $_.sAMAccountName -Clear aaccountroles}
此脚本清除所有值,但我想清除特定值
我的 test.csv 文件
我试着创建了这张纸条:
导入-csv-路径.\test.csv | foreach{Set-ADUser $_.sAMAccountName -清除@{aaccountroles="S4P*"}
但是报错
Set-ADUser : 指定的目录服务属性或值不存在 参数名称:System.Collections.Hashtable 在 line:1 char:39
- ... v | foreach{Set-ADUser $_.sAMAccountName -清除@{aaccountroles="S ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo:InvalidArgument:(test.user1:ADUser)[Set-ADUser],ArgumentException
- FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Set-ADUser
的 -Clear
参数需要一个 字符串 或 字符串数组 ,而不是哈希表。
您需要首先找到具有 'S4P*' 等属性的用户,如果发现清楚 属性:
Import-Csv -Path .\test.csv | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.sAMAccountName)' -and aaccountroles -like 'S4P*'" -Properties aaccountroles -ErrorAction SilentlyContinue
if ($user) {
Write-Host "Clearing the aaccountroles attribute for user $($user.Name)"
$user | Set-ADUser -Clear aaccountroles
}
}
使用 -Remove
、-Add
、-Replace
或 -Clear
和 Set-ADUser,您必须使用 LDAP 显示名称。我更喜欢使用正确的 LDAP 大小写,所以如果 属性 被称为 aaccountRoles
(大写 R),它也可以帮助在这里做到这一点
我不清楚你想完全清空属性(如上所示)只是取出以 ''S4P'..
开头的项目假设 aaccountroles
是一个 数组 :
Import-Csv -Path .\test.csv | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.sAMAccountName)' -and aaccountroles -like 'S4P*'" -Properties aaccountroles -ErrorAction SilentlyContinue
if ($user) {
$roles = @($user.aaccountroles | Where-Object { $_ -match '\S' -and $_ -notlike 'S4P*' })
if ($roles.Count) {
Write-Host "Removing the 'S4P*' aaccountroles items for user $($user.Name)"
$user | Set-ADUser -Replace @{aaccountroles = [string[]]$roles}
}
else {
Write-Host "Clearing the aaccountroles attribute for user $($user.Name)"
$user | Set-ADUser -Clear aaccountroles
}
}
}
假设属性是多行字符串,而不是数组,你可以这样做:
Import-Csv -Path .\test.csv | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$($_.sAMAccountName)' -and aaccountroles -like 'S4P*'" -Properties aaccountroles -ErrorAction SilentlyContinue
if ($user) {
# split the multiline string into an array and filter out the items that do not start with 'S4P'
$roles = @($user.aaccountroles -split '\r?\n' | Where-Object { $_ -match '\S' -and $_ -notlike 'S4P*' })
if ($roles.Count) {
Write-Host "Removing the 'S4P*' aaccountroles items for user $($user.Name)"
# rejoin the left over array items into a multiline string
$newRoles = $roles -join [environment]::NewLine
$user | Set-ADUser -Replace @{aaccountroles = $newRoles}
}
else {
Write-Host "Clearing the aaccountroles attribute for user $($user.Name)"
$user | Set-ADUser -Clear aaccountroles
}
}
}