gpt4 book ai didi

powershell - 在 Powershell 中自动将数字添加到变量中

转载 作者:行者123 更新时间:2023-12-03 00:34:02 25 4
gpt4 key购买 nike

我在网上查看了一些网站并在这里浏览了一些答案,但我的问题没有运气。

我有一个 PowerShell 脚本,可以使用在主机中输入的信息自动创建帐户。我的问题是,如果我的脚本已经存在,如何设置我的脚本在提交的数据末尾自动添加一个数字?代码块如下:

$Username = Read-host "Enter Desired Username"

#Test

IF(!(Get-ADUser -Identity $Username))
{ Write-Host "$username exists. Adding number.
HERE IS THE CODE I AM LOOKING FOR TO TAKE THE $Username and automatically add the number at the end.
}

如果已经回答,请将链接发送给我,我会将其标记为已回答,但如果没有,任何建议都会很棒。

谢谢!

最佳答案

由于此脚本不会自动运行并且有用户输入,因此我建议在使用名称时重新提示用户:

Do
{
$Username = Read-Host -Prompt 'Enter desired username'
} While (Get-ADUser -Identity $Username)

或者:
$Username = Read-Host -Prompt 'Enter desired username'
While (Get-ADUser -Identity $Username)
{
"Username '$Username' taken!"
$Username = Read-Host -Prompt 'Enter desired username'
}

关于powershell - 在 Powershell 中自动将数字添加到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50296572/

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