Ran*_*Dom 3 powershell write-host
我查询 Office365 以获取与Chris的 displayName 匹配的所有用户的列表。
我想提示用户他们想要选择哪个克里斯。这样做时,我有以下 for..each 代码
$tmpUserOffice = Get-MsolUser -SearchString "*Chris*"
if ($tmpUserOffice -is [array])
{
if ($tmpUserOffice.Count -lt 50) {
Write-Host "Many matching users in MSOL. Choose which one you want to save"
for ($i = 0; $i -lt $tmpUserOffice.Count; $i++) {
Write-Host $i " " $($tmpUserOffice[$i].DisplayName)
}
Write-Host $tmpUserOffice.Count " None of the above"
$chosen = Read-Host
if ($chosen -eq $tmpUserOffice.Count) {
Write-Warning "Nothing found. Try searching with different criteria or use wildcards"
Write-Output $null
}
Write-Host $tmpUserOffice[$chosen] " selected"
$tmpUserOffice = $tmpUserOffice[$chosen]
exit
}
else {
Write-Warning "More than 50 matches found. Try searching for more specific criteria"
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题之一是如何让以下行的内容完成
Write-Host $i " " $($tmpUserOffice[$i].DisplayName)
Run Code Online (Sandbox Code Playgroud)
目前的输出是
Many matching users in MSOL. Choose which one you want to save
0
1
2 None of the above
Run Code Online (Sandbox Code Playgroud)
我需要进行哪些更改才能确保该值实际写入一个值?
编者注:该问题与此处发布的代码无关,原则上确实有效。
我认为你只需要把它用双引号引起来:
Write-Host "$i $($tmpUserOffice[$i].DisplayName)"
Run Code Online (Sandbox Code Playgroud)
双引号允许您嵌入变量$i,并且$(...)允许在显示之前对值进行评估。
| 归档时间: |
|
| 查看次数: |
8215 次 |
| 最近记录: |