I am an IT guy that images a lot of machines. Whenever I do so, I have to go into ADUC and look for the next object available, and then I have to create the object and add it to the correct security groups. I am scripting this out currently to save time.
I am running into a problem. The script I have created asks the admin which device type they are creating the object for (laptop/workstation/tablet), then it asks the unit code via a pop up list. It will store the admin input as variables and call them later on to accurately parse through AD using that input.
However, PowerShell sometimes (60%-70% of the time), skips the get-adcomputer
command completely and doesn't return any values (computer names) until you completely finish the script (command that is skipped listed below; the command works on it's own just fine after the variables have been associated with a value, and omitting the code below it returns a successful get-adcomputer
filter.
Get-ADComputer -filter ('Name -like "{0}"' -f $("JEGW" + $typechoice + "-" + $UnitCode + "*") ) | Select-Object -Property Name
The idea is that PS will filter AD for computer objects with a name like JEGW, and then use the admin input (stored as $typechoice and $unit code) to complete the search by searching for things like e.g. JEGWL-AMX* (L and AMX are the admin inputs).
PS will either completely omit the output of this code, or it will display it but only after the question "Do you want to create an object" is asked... or it will output the computer names after every option is selected, and sometimes it doesn't display the computer names at all. But if you omit the code after the get-adcomputer command (≈ line 101), it will work just fine.
Is the output of get-adcomputer getting suppressed by the promptforchoice? Why doesn't PS return that output all the time?
(If you want to test this you will just have to replace JEGW, $typechoice, and $unitcode with something that relates to your name convention.)
<# TITLE: OneShot Object Creation
Summary
This script is intended to ask the admin a few questions, and then use that user input to
find the next available computer name in AD.
It will then ask you if you want to create that object.
It will add the object to the 802.1x sec group.
It will then ask you if the machine is a workstation. If no, then it will add the object
to the wireless security groups.
#>
Import-Module ActiveDirectory
Import-Module dra
<#---------------------------------- DEVICE TYPE SELECTION ----------------------------------#>
$title = 'PC Type Selection'
$question = 'Select Object Type'
$choices = '&Laptop','&Tablet' ,'&Workstation'
$typedecision = $Host.UI.PromptForChoice($title, $question, $choices,1)
switch ($typedecision) #Administrators choose what type of machine they are creating.
{
0 {write-host -ForegroundColor Green 'You selected Laptop.'; $typechoice = 'L' }
1 {write-host -ForegroundColor Green 'You selected Tablet.'; $typechoice = 'T'}
2 {write-host -ForegroundColor Green 'You selected Workstation.'; $typechoice = 'W'}
#Default {}
}
<#---------------------------------- UNIT CODE SELECTION ----------------------------------#>
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'unit Code Selector'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select a unit:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
[void] $listBox.Items.Add('AMX') ; [void] $listBox.Items.Add('ASO')
[void] $listBox.Items.Add('CES') ; [void] $listBox.Items.Add('CF0')
[void] $listBox.Items.Add('CPT') ; [void] $listBox.Items.Add('FSS')
[void] $listBox.Items.Add('IGI') ; [void] $listBox.Items.Add('LRS')
[void] $listBox.Items.Add('MDG') ; [void] $listBox.Items.Add('MXG')
[void] $listBox.Items.Add('MXO') ;[void] $listBox.Items.Add('MXS')
[void] $listBox.Items.Add('OG0') ; [void] $listBox.Items.Add('OSS')
[void] $listBox.Items.Add('SFS') ; [void] $listBox.Items.Add('FS0')
[void] $listBox.Items.Add('COS') ;[void] $listBox.Items.Add('RANS') ; [void] $listBox.Items.Add('JFH')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$UnitCode = $listBox.SelectedItem
$Unitcode
}
<#---------------------------------- FIND NEXT OBJECT AVAILABLE ----------------------------------#>
<# Review
Find's next object available based on the previous input of device type and unit code
Device type is currently associated with $typechoice
Unit Code is currently associated wth $unitcode
#>
# $typeunitcombo = $typechoice + "-" + $UnitCode
Get-ADComputer -filter ('Name -like "{0}"' -f $("JEGW" + $typechoice + "-" + $UnitCode + "*") ) | Select-Object -Property Name
$title2 = 'My Leige'
$question2 = 'Do you want to create an Object?'
$choices2 = '&Yes','&No'
#Start-Sleep -Seconds 5
$continue = $Host.UI.PromptForChoice($title2, $question2, $choices2,0)
switch ($continue) #Administrators choose what type of machine they are creating.
{
0 {write-host -ForegroundColor Green 'Yes';
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = 'PC Name'
$msg = 'Enter PC Name:'
$PCName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
New-ANGComputer -ComputerName $pcname -Unit '124 CF'
}
1 {write-host -ForegroundColor Green 'No'; }
#Default {}
}
````````````````````````````````````````````````````````````````````
Thank you for your time.