【发布时间】:2018-06-20 09:54:48
【问题描述】:
我是 PowerShell 新手,我正在尝试通过为日常任务(获取用户名、重置密码和解锁帐户)创建脚本来节省几次点击。
问题是:当我启动脚本时,Powershell 将执行除 cmdlet 之外的所有内容,并由它们完成。
例如,当我使用第一个选项(获取用户名)时,它会提示我“按回车键结束脚本”,然后显示我的 GetUserName 函数的结果。
请在下面找到我的代码:
# Author : Maxime
# Creation date : 20/06/2018
# Version : 0.1
Function GetUserName($name)
{
$u = 'surname -like "' +$name+ '"'
$res = Get-ADUser -Filter $u | select Name, GivenName, Surname
return $res
}
echo "Menu"
echo "------"
$choix = read-host " 1. Trouver utilisateur par nom `n 2. Reset Password `n 3. Deverrouiller compte `nChoix "
if($choix = 1)
{
$name = Read-Host "Entrez nom utilisateur "
GetUserName($name)
}
elseif( $choix = 2)
{
$id = Read-Host "Entrez ID"
Set-ADAccountPassword $id -confirm -Reset
}
elseif ($choix -= 3)
{
$id = Read-Host "Entrez ID "
Unlock-ADAccount -Confirm $id
}
else
{
echo "Mauvais choix"
}
read-host "Press enter to end the script ..."
编辑:
当我在没有 ISE 的情况下启动脚本时:
当我使用 ISE 启动脚本时,我可以看到我的结果在确认关闭后显示。
我希望脚本显示信息,然后显示确认以关闭窗口。
【问题讨论】:
-
我不太明白这个问题。据我所知,代码看起来不错(我现在没有服务器要检查)。
-
让我们编辑和添加截图:)
-
当我测试它时,它会显示输出,但在你按下回车后(我正在 ISE 中测试,所以我看到了)。尝试使用这个(保存到变量和管道到表:
$res = GetUserName($name); $res|ft -
@robdy,没错!您的解决方案有效,您知道为什么它在“按 Enter”后显示吗?
-
可能与this 相关。但是,我很高兴它有效,让我将其转换为答案,以便您可以根据需要接受它。
标签: powershell powershell-cmdlet