简体   繁体   English

在Powershell中添加新用户,然后在添加用户后显示消息

[英]Adding a new user in powershell and then giving a message after added user

How can i give an information to output that i have successfuly added a new user in an ou in powershell after the script have been run? 运行脚本后,如何在Powershell的ou中成功添加新用户的输出信息? Because, now when i am running the script i do not get any message about successful operation in powershell. 因为,现在当我运行脚本时,我没有收到有关在Powershell中成功运行的任何消息。 I want to give a message that prints out in powershell after added the script about if the code worked or not. 我想提供一条消息,该消息在添加有关代码是否有效的脚本后在Powershell中打印出来。

New-ADUser -Name "Test Eksamen" -GivenName "Test" -Surname Eksamen -SamAccountName Test-UserPrincipalName Test@TestTo.local -path "OU=powershell, DC=TestTo, DC=local"

So, you could save the output of that command to a variable, then use an if/else to test if that variable contains expected output for a success, something like the following: 因此,您可以将该命令的输出保存到变量,然后使用if / else测试该变量是否包含成功的预期输出,如下所示:

$result = New-ADUser -Name "Test Eksamen" -GivenName "Test" -Surname Eksamen -SamAccountName Test-UserPrincipalName Test@TestTo.local -path "OU=powershell, DC=TestTo, DC=local"

if($result -Like "some string you expect from that command")
{
   echo "command succeeded"
}
else
{
   echo "command failed"
}

If the -Like command isn't giving you expected results try -Contains, here is a little blog post on the differences, I always have trouble remembering which does what haha http://www.itprotoday.com/management-mobility/powershell-contains 如果-Like命令没有给您预期的结果,请尝试-Contains,这是有关差异的小博文,我总是很难记住哪个做了什么哈哈http://www.itprotoday.com/management-mobility/powershell -包含

I guess Try Catch may help you. 我想Try Catch可能会帮助您。

Try {
    New-ADUser -Name "Test Eksamen" -GivenName "Test" -Surname "Eksamen" -SamAccountName "Test" -UserPrincipalName "Test@TestTo.local" -path "OU=powershell, DC=TestTo, DC=local"
    echo "User Created"
Catch{
    echo "User is not created"}

Or you could just check if user created right after "New-Aduser" 或者,您可以仅检查用户是否在“新广告用户”之后创建

if (Get-aduser -filter {name -eq "Test Eksamen"}) {echo "User exists"}
else {echo "user doesn't exist"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM