我正在尝试构建PS脚本(用于Office 365),该脚本将帮助我管理日常任务,例如为用户设置电子邮件转发,添加电子邮件等
我已经开始进行连接过程了,我想让Powershell向我询问用户名,然后运行一些命令并向我显示结果,这样我就可以通过“菜单”运行它了: / p>
它工作正常并且可以完成我想做的事情,问题是,例如,如果有两个具有相同名称的用户(如“ trevor”),结果将向我显示
特雷弗·杰克逊和特雷弗·布拉赫
我如何使脚本告诉我“我找到了两个具有相同名称的用户”?
这是代码的简短版本(与O365的连接不需要在这里)
$askusername= Read-Host "What is the user name? you can write part of the user name to"
write-host "`n"
$checkuser = Get-Mailbox -Identity *$askusername*
write-host -ForegroundColor White -BackgroundColor Blue "Found this user: $checkuser"
#menu
$menu = Read-Host -Prompt "
`n1. Enalbe Email Forwarding from $checkuser to a spesific user WITH copy?
`n2. Enable Email Forwarding from $checkuser to a spesific user WITHOUT a copy?
`n3. Disable Forwarding
`n4. Exit
`n What would you like to do?"
Switch($menu){
1{Get-Mailbox -Identity *$askusername* |select name} (for testing)
2{Write-Host "it's working" green}
3{exit}
}
答案 0 :(得分:0)
$askusername= Read-Host "What is the user name? you can write part of the user name to"
write-host "`n"
$checkuser = Get-Mailbox -Identity *$askusername*
if($checkuser.count -eq 1){
Write-Host -ForegroundColor White -BackgroundColor Blue "Found this user: $($checkuser.Name)"
}else{
$checkuser = $checkuser | Out-GridView -PassThru -Title "Multiple Users found, please select the correct user."
}
write-host -ForegroundColor White -BackgroundColor Blue "Found this user: $checkuser"
#menu
$menu = Read-Host -Prompt "
`n1. Enalbe Email Forwarding from $checkuser to a spesific user WITH copy?
`n2. Enable Email Forwarding from $checkuser to a spesific user WITHOUT a copy?
`n3. Disable Forwarding
`n4. Exit
`n What would you like to do?"
Switch($menu){
1{Get-Mailbox -Identity $checkuser | select name} #(for testing)
2{Write-Host "it's working" green}
3{exit}
}
您需要计数,您必须检查结果是否包含1个以上的对象。这些思路可以告诉您是否有多个。
编辑:编辑脚本以提供计数和网格视图以选择要追加的用户。此外,您在选择列表中一直使用错误的变量,应使用checkuser变量。