[英]Get available Hostnames related to existing Active Directory Computers using Powershell
I want to run a Powershell script withtin a SCCM task sequence, where a specific hostname by naming convention is entered, searched in Active Directory and checked for it's availability. 我想运行一个带有SCCM任务序列的Powershell脚本,在该脚本序列中输入了一个按命名约定命名的特定主机名,在Active Directory中进行搜索并检查其可用性。
I found the following code on the internet: 我在互联网上找到以下代码:
$Prefix = $EnteredHostname -match '^(.*?)\d*$' | Out-Null # strip possibly trailing numbers
$Prefix = $Matches[1]
$Number = 0 # initialize last Number to zero
Get-ADComputer -Filter * |
Where-Object {$_.Name -match "^$Prefix(\d+)$" }|
Select-Object @{n='UsedNumber';e={[int]$Matches[1]}} | Sort-Object UsedNumber |
ForEach-Object {
While ($Number +1 -lt $_.UsedNumber){"{0}{1}" -f $Prefix,++$Number}
$Number = $_.UsedNumber
}
# if there was no gap get the next one.
"{0}{1}" -f $Prefix,++$Number
It is working fine but it only processes the last numbers after the entered Text. 它工作正常,但仅处理输入的文本后的最后一个数字。 (Before that code is running, a Textbox appears where a desired hostname without numbers is entered and stored in the variable $enteredHostname). (在运行该代码之前,将出现一个文本框,在其中输入所需的主机名(不带数字)并将其存储在变量$ enteredHostname中)。
Our companys naming convention is like this: "HostnameXXXX". 我们公司的命名约定如下:“ HostnameXXXX”。 For example: Hostname0005, Hostname0015, Hostname0150, ect. 例如:主机名0005,主机名0015,主机名0150等。 But this Code only delivers --> Hostname5, Hostname15, Hostname150, .. 但是此代码仅提供-> Hostname5,Hostname15,Hostname150,..
How can I adjust the code to our naming convention, so that the hostnames are displayed correctly? 如何调整代码以符合我们的命名约定,以便正确显示主机名?
I couldn't figure it out myself yet. 我自己还没弄清楚。 I'm new to Powershell, only have been using it for a few weeks 我是Powershell的新手,仅使用了几周
Any sggestions are highly appreaciated 强烈建议
The format operator (`-f') allows for padding of digits. 格式运算符(-f)允许填充数字。 In this case, the padding will provide extra zeroes in front of the number. 在这种情况下,填充将在数字前面提供额外的零。
"{0}{1:d4}" -f $Prefix,++$Number
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.