gpt4 book ai didi

powershell - 如何在Powershell数组中写入托管元素的单个属性?

转载 作者:行者123 更新时间:2023-12-03 00:44:15 26 4
gpt4 key购买 nike

我向Office365查询了与克里斯的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"
}
}

我的问题之一是如何获取以下行的内容来完成
Write-Host $i " "  $($tmpUserOffice[$i].DisplayName) 

目前的输出是
Many matching users in MSOL. Choose which one you want to save 
0
1
2 None of the above

我需要进行哪些更改以确保该值实际写入一个值?

编者注::该问题与此处发布的代码无关,原则上是可行的。

最佳答案

我认为您只需要将其用双引号引起来即可:

Write-Host "$i  $($tmpUserOffice[$i].DisplayName)"

双引号允许您嵌入变量 $i,而 $(...)允许在显示值之前对其求值。

关于powershell - 如何在Powershell数组中写入托管元素的单个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51679770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com