我正在嘗試動態構建一個包含字串的變數。然后我想檢查那個新變數,如果它不為空,請在前面加上“;” 到每個添加的字串。
基本上,我正在構建一個 Set-ADUser -Replace @{dynamicVarialble} 命令,并希望該 dynamicVariable 包含最多三個不同的值,但只有那些不為空或 null 的值。如果它是字串中的第一個值,則不要在前面加上 ';' 否則請添加它。所以結果會是這樣的:
Set-ADUser -identity $person -Replace @{mobilenumber=$number;phoneNUmer=$phoneNumber;pager=$pagerNumber}
但是這樣做是 -Replace @{$dynamicVariable} -下面將是我下面代碼中的 $ADUpdater 變數或陣列。
注意:如果這些輸入變數之一為空,請不要將其包含在“動態變數”或陣列中。如果它是添加的第一個變數,請不要在前面加上分號。
希望這是有道理的......所以如果輸入是:
$otherMobile = "555-222-1111"
$otherHomePhone ="" ##(empty or null)
$otherPager = "555-555-5555"
那么它將是:
Set-ADUser -identity $person -Replace @{otherMobile="555-222-111";otherPager="555-555-5555"}
$person =$userID
$number= $otherMobile
$homeNumber = $otherHomePhone
$pagerNumber = $otherPager
[String]$ADupdater = @()
if (![string]::IsNullOrEmpty($otherMobile)){
[string]$ADupdater = "otherMobile=$number"
}
if (![string]::IsNullOrEmpty($otherHomePhone)){
[string]$ADupdater = "otherHomePhone=$homeNumber"
}
if (![string]::IsNullOrEmpty($otherPager)){
[string]$ADupdater = "otherPager=$otherPager"
}
Foreach ($item in $ADupdater){
if ([string]::IsNullOrEmpty($item))
{
$ADupdater = $ADupdater
}
else {
$ADupdater = ';' $ADupdater
}
}
Write-host "this is ADUpdated: $ADupdater"
uj5u.com熱心網友回復:
這種方法行不通,因為-Replace引數實際上并不接受字串值。
我們來看看-Replace引數:
PS ~> $setADUserCommand = Get-Command Set-ADUser
PS ~> $setADUserCommand.Parameters['Replace']
Name : Replace
ParameterType : System.Collections.Hashtable
ParameterSets : {[Identity, System.Management.Automation.ParameterSetMetadata]}
IsDynamic : True
Aliases : {}
Attributes : {Identity, System.Management.Automation.ValidateNotNullOrEmptyAttribute, Microsoft.ActiveDirectory.Management.Commands.ValidateAttributeValueHashtableAttribute}
SwitchParameter : False
請注意該ParameterType值是如何列出的HashTable- 因此我們需要將哈希表作為引數傳遞給該引數。
好處是可以通過編程方式修改哈希表,因此我們仍然可以構造您想要的引數:
# Let's start by creating an empty hashtable
$ADupdater = @{}
$otherMobile = "555-222-1111"
$otherHomePhone ="" ##(empty or null)
$otherPager = "555-555-5555"
# now we just need to populate the hashtable instead of a string
if (![string]::IsNullOrEmpty($otherMobile)){
$ADupdater["otherMobile"] = $otherMobile
}
if (![string]::IsNullOrEmpty($otherHomePhone)){
$ADupdater["otherHomePhone"] = $otherHomePhone
}
if (![string]::IsNullOrEmpty($otherPager)){
$ADupdater["otherPager"] = $otherPager
}
# Now we simply pass the hashtable to Set-ADUser
Set-ADUser -Identity $person -Replace $ADupdater
您可能還希望通過將候選值和屬性名稱存盤在哈希表本身中來讓自己的生活更輕松 - 這將使代碼更簡單地添加新屬性(因為您不需要if(![string]::...){...}為每個新屬性單獨塊):
$ADupdater = @{}
$values = @{
otherMobile = "555-222-1111"
otherHomePhone ="" ##(empty or null)
otherPager = "555-555-5555"
}
foreach($entry in $values){
if(-not [string]::IsNullOrEmpty($entry.Value)){
$ADupdater[$entry.Name] = $entry.Value
}
}
uj5u.com熱心網友回復:
對于那些感興趣的人(感謝@Mathias R. Jessen)什么對我有用(盡管可能不干凈或高效)......
$ADupdater = @{}
$values = @{
otherMobile = $otherMobile
otherHomePhone = $otherHomePhone
otherPager = $otherPager
}
if (! [string]::IsNullOrEmpty($otherMobile)) {
$ADupdater.add('otherMobile',$otherMobile)
}
if (! [string]::IsNullOrEmpty($otherHomePhone)) {
$ADupdater.add('otherHomePhone',$otherHomePhone)
}
if (! [string]::IsNullOrEmpty($otherPager)) {
$ADupdater.add('otherPager',$otherPager)
}
Set-ADUser -Identity $($person) -Replace $ADupdater
由于我將 otherMobile、otehrHomePhone 和 otherPager 的值作為引數傳遞給我的腳本,因此我無法得到 Mathias 的以下建議:
foreach($entry in $values){
if(-not [string]::IsNullOrEmpty($entry.Value)){
$ADupdater[$entry.Name] = $entry.Value
}
}
干杯!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/416706.html
標籤:
