我有一個函式,由于某種原因,$Properties無論有多少用戶傳遞給腳本,它似乎只顯示一次。試圖僅將$user傳遞到腳本中的內容放入以顯示,但是,它也只顯示一次。不太確定為什么要這樣做,因為我已經用盡了它可能是什么的想法:
Function Set-DRAUserProperty {
Param (
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[string[]]$UserName,
$Company,
$Department,
$Name,
$O,
$PhysicalDeliveryOfficeName,
$TelephoneNumber,
$Title
)
DynamicParam
{
if ($UserName.Split(',').Count -le 1) {
$displayNameAttribute = New-Object System.Management.Automation.ParameterAttribute
$displayNameAttribute.Mandatory = $false
$attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($displayNameAttribute)
$displayNameParam = New-Object System.Management.Automation.RuntimeDefinedParameter('DisplayName', [string], $attributeCollection)
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('DisplayName', $displayNameParam)
$paramDictionary
}
}
Begin
{
$OrgBoxOu = "*OU=xxxxxxxxx"
$Parameters = $PSBoundParameters
$Properties = @{}
}
Process
{
foreach ($User in $UserName)
{
try {
$SelectedUser = Get-ADUser -Identity $User -Properties DisplayName
if ($SelectedUser) {
$Parameters.GetEnumerator() | Where-Object -FilterScript {$_.Key -ne 'UserName'} |
ForEach-Object -Process `
{
if ($_.Key -eq 'DisplayName' -and $SelectedUser.DistinguishedName -like $OrgBoxOu) {
if ('FirstNamePreferred' -in $Properties.Keys) {
Continue
}
else {
$Properties.Add($_.Key, $_.Value)
$Properties.Add('FirstNamePreferred', $_.Value)
}
}
else {
if ($_.Key -in $Properties.Keys) {
Continue
}
else {
$Properties.Add($_.Key, $_.Value)
}
}
}
$Properties.GetEnumerator()
#Set-DRAUser -Identifier $SelectedUser.DistinguishedName -Properties $Properties @DRA_Server_Splat -ErrorAction Stop
}
else {
Write-Host -Object "No valid member selected!"
}
}
catch {
Write-Host -Object "ERROR: $($_.Exception.Message)" -ForegroundColor Red -BackgroundColor Black
continue
}
}
}
End { }
}
運行該函式時,
Set-DRAUserProperty -UserName Abe,Abe -Company ssc
. . . 輸出只顯示一次:
Name Value
---- -----
Company ssc
我想要實作的是將值分配到另一個 cmdlet 上,但只要我的$Properties哈希表只顯示一次,它就無法作業。因此,預期的輸出將是顯示傳遞給函式的每個用戶的哈希表:
Name Value
---- -----
Company ssc
Name Value
---- -----
Company ssc
只是尋找一些可以為我指明正確方向和/或闡明可能發生的事情的新鮮眼睛(錯誤)。不確定是什么導致了問題,因此無法將其指向特定的代碼部分。
請教育我:)
uj5u.com熱心網友回復:
解決方案是hashtable在foreach回圈內部而不是在begin { ... }塊內部定義:
foreach ($User in $UserName)
{
$Properties = @{}
....
....
}
但是,如果您想了解為什么它以前不起作用?,這是你的提示:
if ($_.Key -in $Properties.Keys) {
'{0} is in $Properties.Keys :)' -f $_.Key
Continue
}
其中產生:
Name Value
---- -----
Company ssc
Company is in $Properties.Keys :)
Company is in $Properties.Keys :)
這只是因為$Properties在每次迭代中都是相同的(因為它是在begin { ... }塊中定義的)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363102.html
上一篇:Laravel父類屬性繼承問題
下一篇:用戶輸入路徑作為字串
