我有以下代码...迭代中跳过了一些奇怪的原因
Function PrintArrayAsGrid
{
param([string[]]$Array,[ValidateRange(1,24)][int]$ColumnCount)
$GridSplat = @{
InputObject = $Array|ForEach-Object {
New-Object psobject -Property @{'Value' = $_}
}
Property = 'Value'
}
if(-not $PSBoundParameters.ContainsKey('ColumnCount'))
{
$GridSplat['AutoSize'] = $true
}
else
{
$GridSplat['Column'] = $ColumnCount
}
Format-Wide @GridSplat
}
Function UserInputAdSearchPropertyName
{
$userInputNotYetValidated = $true
$userInput = Read-Host "
Enter AD property name you wish to search with (e.g.: employeeNumber)"
$userInput = $userInput.Trim()
Write-Host "you input $userInput"
Write-Host ""
[String[]]$validAdProperties = @('SamAccountName', 'msRTCSIP-UserEnabled', 'msRTCSIP-OptionFlags', 'msRTCSIP-PrimaryUserAddress', 'msRTCSIP-PrimaryHomeServer',
'mail', 'msExchMasterAccountSid', 'homeMDB', 'proxyaddresses', 'legacyExchangeDN',
'lastLogonTimestamp', 'logonCount', 'lastLogoff', 'lastLogon', 'pwdLastSet', 'userAccountControl', 'whenCreated', 'whenChanged', 'accountExpires',
'sn', 'givenName', 'displayName', 'distinguishedName', 'initials', 'l', 'st', 'street', 'title', 'description', 'postalCode', 'physicalDeliveryOfficeName', 'telephoneNumber', 'facsimileTelephoneNumber', 'info', 'memberOf', 'co', 'department', 'company', 'streetAddress', 'employeeNumber', 'employeeType', 'objectGUID', 'employeeID', 'homeDirectory', 'homeDrive', 'scriptPath', 'objectSid', 'userPrincipalName', 'url', 'msDS-SourceObjectDN', 'manager', 'extensionattribute8')
while ($userInputNotYetValidated)
{
If ($validAdProperties -notcontains $userInput)
{
Write-Error "Invalid AD Property Name: $userInput"
PrintArrayAsGrid $validAdProperties 4
$userInput = Read-Host " Enter one property name from list above to search with"
Write-Host "you input $userInput"
Write-Host ""
} Else {
$userInputNotYetValidated = $false
}
}
Write-Output $userInput
}
# Ask user to enter property name in AD to search with
$searchAdPropertyName = UserInputAdSearchPropertyName
输出:
Enter AD property name you wish to search with (e.g.: employeeNumber): asdf
you input asdf
UserInputAdSearchPropertyName : Invalid AD Property Name: asdf
At C:\Scripts\Tests\temp2.ps1:59 char:26
+ $searchAdPropertyName = UserInputAdSearchPropertyName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,UserInputAdSearchPropertyName
Enter one property name from list above to search with:
这里的问题是它没有打印特性的列表,从而跳过迭代,它调用PrintArrayAsGrid $validAdProperties 4
这里是如果我添加以下行会发生什么...
Write-Host 'Found'
PrintArrayAsGrid $validAdProperties 4
Write-Host 'Found'
输出:
Enter AD property name you wish to search with (e.g.: employeeNumber): asdf
you input asdf
UserInputAdSearchPropertyName : Invalid AD Property Name: asdf
At C:\Scripts\Tests\temp2.ps1:60 char:26
+ $searchAdPropertyName = UserInputAdSearchPropertyName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,UserInputAdSearchPropertyName
Found
Found
Enter one property name from list above to search with:
而现在,我竟然得到了表打印出来,如果我在退出添加如下...
Write-Host 'Found'
PrintArrayAsGrid $validAdProperties 4
Write-Host 'Found'
Exit
输出:
PS C:\Tests> .\test1.ps1
Enter AD property name you wish to search with (e.g.: employeeNumber): asdf
you input asdf
UserInputAdSearchPropertyName : Invalid AD Property Name: asdf
At C:\Tests\test1.ps1:61 char:26
+ $searchAdPropertyName = UserInputAdSearchPropertyName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,UserInputAdSearchPropertyName
Found
Found
SamAccountName msRTCSIP-UserEnabled msRTCSIP-OptionFlags msRTCSIP-PrimaryUserAddress
msRTCSIP-PrimaryHomeServer mail msExchMasterAccountSid homeMDB
proxyaddresses legacyExchangeDN lastLogonTimestamp logonCount
lastLogoff lastLogon pwdLastSet userAccountControl
whenCreated whenChanged accountExpires sn
givenName displayName distinguishedName initials
l st street title
description postalCode physicalDeliveryOfficeName telephoneNumber
facsimileTelephoneNumber info memberOf co
department company streetAddress employeeNumber
employeeType objectGUID employeeID homeDirectory
homeDrive scriptPath objectSid userPrincipalName
url msDS-SourceObjectDN manager extensionattribute8
PS C:\Tests>
所需的输出:
PS C:\Tests> .\test1.ps1
Enter AD property name you wish to search with (e.g.: employeeNumber): asdf
you input asdf
UserInputAdSearchPropertyName : Invalid AD Property Name: asdf
At C:\Tests\test1.ps1:61 char:26
+ $searchAdPropertyName = UserInputAdSearchPropertyName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,UserInputAdSearchPropertyName
SamAccountName msRTCSIP-UserEnabled msRTCSIP-OptionFlags msRTCSIP-PrimaryUserAddress
msRTCSIP-PrimaryHomeServer mail msExchMasterAccountSid homeMDB
proxyaddresses legacyExchangeDN lastLogonTimestamp logonCount
lastLogoff lastLogon pwdLastSet userAccountControl
whenCreated whenChanged accountExpires sn
givenName displayName distinguishedName initials
l st street title
description postalCode physicalDeliveryOfficeName telephoneNumber
facsimileTelephoneNumber info memberOf co
department company streetAddress employeeNumber
employeeType objectGUID employeeID homeDirectory
homeDrive scriptPath objectSid userPrincipalName
url msDS-SourceObjectDN manager extensionattribute8
Enter one property name from list above to search with:
任何人都知道为什么发生这种情况?
你确定你的'格式,宽@ GridSplat'线产生的任何输出?你为什么认为它跳过任何东西? – n0rd
你也可以在你的代码中混合使用'Write-Output'和'Write-Host',这是故意的吗? – n0rd
写入输出将分配稍后将用于多个事物的变量。写输出只是写入窗口,让用户知道发生了什么。 –