简体   繁体   English

使用Powershell获取与现有Active Directory计算机相关的可用主机名

[英]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.

相关问题 Powershell:获取操作系统连接到 Active Directory 的计算机数量 - Powershell: Get number of computers connected to Active Directory by OS 如何使用Powershell从Active Directory上的计算机获取磁盘信息 - How to get disk information from computers on Active Directory with Powershell 使用PowerShell获取所有活动域计算机 - Using PowerShell to get all active-domain computers Powershell-将计算机添加到Active Directory中的安全组 - Powershell - Adding computers to a security group in Active Directory 我需要使用Powershell将计算机从默认的活动目录“容器”复制到安全组。 - I need to copy computers from the default active directory “container” to security group using powershell 使用PowerShell删除基于现有Active Directory帐户的目录 - Delete Directories Based on Existing Active Directory Accounts Using PowerShell 对Active Directory中的所有计算机运行Powershell脚本 - Running a powershell script against all the computers in Active Directory 如何在PowerShell中将所有Active Directory计算机读入阵列? - How can I read all Active Directory computers into an array in PowerShell? Active Directory Powershell追加到现有属性值 - Active Directory Powershell append to existing attribute value 使用Powershell将Azure网站/ WebApp主机名获取为变量 - Get Azure Website/WebApp hostnames as a variable using Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM