目前,我正在尝试为我的powershell脚本添加一个函数,目标如下:
在一台尚未添加到域中的计算机上,让它根据用户的输入搜索本地AD服务器(而不是azure)中的下一个可用名称。
我过去曾尝试使用数组,但都失败了,我想在这里使用Get-ADComputer cmdlet,但我不确定如何实现它。
$usrinput = Read-Host 'The current PC name is $pcname , would you like to rename it? (Y/N)'
if($usrinput -like "*Y*") {
Write-Output ""
$global:pcname = Read-Host "Please enter the desired PC Name"
Write-Output ""
$userinput = Read-Host "You've entered $pcname, is this correct? (Y/N)"
if($usrinput -like "*N*") {
GenName
#name of the parent function
}
Write-Output ""
上面的代码是一个更大的脚本的一部分,该脚本解析计算机名称并最终将其分配给正确的OU。
我们的命名方案是这样工作的:BTS-ONE-LAP 000
因此它是:部门-位置-设备类型-设备计数
然后,代码将获取第一部分"BTS-ONE“,并对其进行分析以确定其应转到的正确OU,然后使用Add-Computer cmdlet对其进行分配。它还会将计算机重命名为用户键入的名称($pcname)。
因此,在它解析名称之前,我希望它搜索AD中的所有当前名称。
因此,用户可以输入:“BTS-ONE- it”,它会自动找到下一个可用的设备计数,并将其添加到名称中。因此,它将自动生成"BTS-ONE-LAP-041“。
补充说明:
我使用了Get-ADComputer -Filter 'Name -like "BTS-ONE-LAP-*"' | FT Name
,输出是
Name
----
BTS-ONE-LAP-001
BTS-ONE-LAP-002
BTS-ONE-LAP-006
BTS-ONE-LAP-007
BTS-ONE-LAP-009
BTS-ONE-LAP-010
BTS-ONE-LAP-022
BTS-ONE-LAP-024
BTS-ONE-LAP-025
BTS-ONE-LAP-028
BTS-ONE-LAP-029
BTS-ONE-LAP-030
BTS-ONE-LAP-031
BTS-ONE-LAP-032
BTS-ONE-LAP-034
BTS-ONE-LAP-035
BTS-ONE-LAP-036
BTS-ONE-LAP-037
BTS-ONE-LAP-038
BTS-ONE-LAP-039
BTS-ONE-LAP-040
BTS-ONE-LAP-041
BTS-ONE-LAP-050
BTS-ONE-LAP-051
我不知道如何解析它,所以代码知道BTS-ONE-LAP-003是可用的(我不擅长数组)。
https://stackoverflow.com/questions/56994408
复制相似问题