Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
I need some assistance in the PowerShell scripting, so I can get the next available number of the Workstation based on Active Directory AD computer object.
My AD naming convention for computer objects like the below:
PRDVM001
PRDVM002
PRDVM003
...
PRDVM055
So how can I get the return from the Powershell script which basically says:
The next available AD computer name is PRDVM055
I just need to make sure that PRDVM055 is not already created in all of the AD domain before and is the largest number.
jvierra wrote: ↑Mon Oct 29, 2018 5:18 pm
Get what OU? A computer object does not have an OU property. Everything after the "CN=<name>," is the parent container.
Yes, how to get the CN where the object in $name was mentioned?
$name = Get-AdComputer -Filter "Name -like 'PRDVM*'" | sort name | Select -Expand Name | select -last 1
$patterns = 'PROD', 'TEST', 'DR'
$CurrentADComputerName = $null
$NextADComputerName = $null
ForEach ( $prefix in $patterns) {
$CurrentADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" |
Sort-Object Name |
Select-Object -Expand Name |
Select-Object -Last 1
Write-Host "The current AD Computername for $prefix is: $CurrentADComputerName" -BackgroundColor Blue -ForegroundColor Yellow
$CurrentOULocation = (Get-ADComputer $CurrentADComputerName -Properties CanonicalName).CanonicalName
$NextADComputerName = Get-ADComputer -Filter "Name -like '$($prefix)*'" |
ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
Measure-Object -Maximum |
ForEach-Object {$prefix + (++$_.Maximum).ToString('000')}
Write-Host "The next available AD Computername for $prefix is: $NextADComputerName" -BackgroundColor Blue -ForegroundColor Green
Write-Host "Which can be deployed in OU: $CurrentOULocation `n`n" -BackgroundColor Blue -ForegroundColor White
}
How to edit the $CurrentOULocation so it is displaying in domain.com/Server/Production format, without the Last computername ?
and there is some issue that I wanted to suppress or ignore:
Cannot convert value "PROD99-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
Cannot convert value "TEST67-DEV" to type "System.Int32". Error: "Input string was not in a correct format."
$buttonTPMStatus_Click={
$filters = @(
"Name -like 'PROD*'",
"Name -like 'TEST*'",
"Name -like 'DR*'"
)
foreach($filter in $filters){
$computer = Get-ADComputer -Filter $filter |
Sort-Object Name |
Select-Object -Last 1
Write-Host "The current AD Computer is $computer" -BackgroundColor Blue -ForegroundColor Yellow
$computer.Name
($computer.DistinguishedName -split ',')[1..99] -join ','
}
}
Mr. Vierra,
Pardon me, as I'm still learning in Powershell, I have found out that the below code is what I have managed to work but with the poor error handling, for example:
Cannot convert value "TEST-Server03-OLD" to type "System.Int32". Error: "Input string was not in a correct format."
At line:19 char:25
+ ... ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
Cannot convert value "PRD-CCTV-SITE1" to type "System.Int32". Error: "Input string was not in a correct format."
At line:19 char:25
+ ... ForEach-Object {[int]($_.Name -replace '(?:.*?)(\d+)\Z', '$1')} |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
This is the below code, which I believe can be further modified to be more effective:
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.