簡體   English   中英

Powershell 列出免費域計算機

[英]Powershell to list free domain computers

我正在使用 PowerShell 獲取我們所有域計算機的 lit,然后查找那些被跳過的計算機,因此我們可以輕松地獲得此名稱范圍內的免費計算機名稱。

例如:

Get-ADComputer -Filter {Name -like "PC016*"} | Sort-Object | select Name

這讓我所有的電腦都以“PC016”開頭,它可以工作。 從這里我想列出所有跳過的名字。

例如,如果我有這個輸出:

PC016225
PC016226
PC016228
PC016229

我希望 Powershell 列出跳過的項目( PC016227 )。

我怎樣才能做到這一點?

從名稱中刪除前綴,以便留下數字后綴(例如225 ),然后對結果值進行排序以輕松找到最低和最高值,然后簡單地輸出不在原始值之間的任何數字列表:

# Define computer name prefix
$prefix = "PC016"

# Create regex pattern for the prefix + a pattern to match only computer names ending with numbers
$prefixPattern = "^$([regex]::Escape($prefix))"
$namePattern   = "${prefixPattern}\d+$"

# Query the directory for computer objects with the given naming prefix,
# filter the result so we only store computers with a numerical suffix
$computerNames = Get-ADComputer -Filter "Name -like '${prefix}*'" |Where-Object Name -match $namePattern |ForEach-Object Name

# Extract the numerical suffix and convert to a numeric type
$values = @($computerNames -replace $prefixPattern) -as [int[]]

# Sort the values
$values = $values |Sort-Object 

# Now generate the range of values from the smallest to the biggest, 
# filter out any values that are already found in the existing list
$skipped = $values[0]..$values[-1] |Where-Object { $_ -notin $values }

# ... and finally attach the prefix again and output the new possible names
$skipped |ForEach-Object { -join $prefix,$_ }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM