Renaming PC with Drop Down Menus

Hi Guys,

I could not find a script anywhere on the net to create a pop box, with drop down menus, so I took a couple of pieces from different blogs, and created my own. I am new to this and would appreciate some help.

I want to join the info from the drop down menus to create a new PC name, but I can not get the output to the host. Can someone perhaps help?

Any help will be greatly appreciated.

Here is a link to my script at GistHubGist:

 

<script src=“https://gist.github.com/benster310/fc92897792ed8558010b245d7ee94258.js”></script>

 

Your help is really appreciated

 

Regards,

Benjamin

 

Its not easy to look at this code until its properly formated, you could use gist.github.com to post you code and update the post with you link by removing the code here.

First, I would probably try to use a hashtable or psobject rather than arrays. Something like this:

https://social.technet.microsoft.com/Forums/sharepoint/en-US/b0967c47-7530-4bcf-9eec-d29860e93833/combobox-display-name-and-value?forum=winserverpowershell

You should be able to do something like this:

$deviceType = @()
$deviceType += [pscustomobject]@{
    Type   = 'DESKTOP'
    Prefix = 'WS'
}
$deviceType += [pscustomobject]@{
    Type   = 'LAPTOP'
    Prefix = 'LT'
}

$site= @()
$site += [pscustomobject]@{
    Name   = 'BUTTERWORTH'
    Suffix = 'BUT'
}
$site += [pscustomobject]@{
    Name   = 'BIZANA'
    Suffix = 'BZA'
}

$cmbDevType.Items.addrange($deviceType)
 
$cmbDevType.displaymember = "Type"
$cmbDevType.ValueMember = "Value"

$cmbSite.Items.addrange($site)
 
$cmbSite.displaymember = "Type"
$cmbSite.ValueMember = "Value"

Then you just need to glue the values together:

'{0}-{1}-{2}' -f $cmbDevType.ValueMember,$cmbSite.ValueMember,(Get-Random -Minimum 10000 -Maximum 99999)

Which would give you something like LT-BZA-58392. Didn’t test this, but the link above appears to have implemented something similar

Thank you for your reply Rob

 

I have removed the “Arrays” and changed it to the following:

$DropDownType.Items.AddRange(@(“DESKTOP”,“LAPTOP”))

This took away the need to have the two arrays.

 

I will have a look at your idea and let you know

Hi,

 

I have posted it to GitHubGist

 

Here is the link to the latest code:

<script src=“https://gist.github.com/benster310/fc92897792ed8558010b245d7ee94258.js”></script>

 

Regards,

Benjamin

As for this…

I could not find a script anywhere on the net to create a pop box, with drop down menus

… where did you look. As there is a module for this type of thing in the MS PowerSellGallery.

Find-Module -Name 'AnyBox' | Format-Table -AutoSize

Version Name   Repository Description
------- ----   ---------- -----------
0.3.3   AnyBox PSGallery  Designed to facilitate script input/output with an easily customizable WPF window

The authors doc on the tool, specifically the drop down part.

$prompt = New-AnyBoxPrompt -Name 'fav_sport' `
  -Message 'What is your favorite sport?' `
  -ValidateSet @('Basketball', 'Football', 'Baseball', 'Soccer', 'Hockey', 'Other') `
  -DefaultValue 'Baseball'

Show-AnyBox -Icon 'Question' -Prompt $prompt -Buttons 'OK'

This uses also the more modern, support WPF (Windows Presentations Foundation) vs WF (Windows Forms) that your code is using.

Prompts https://www.donaldmellenbruch.com/doc/anybox/prompts

Secondly, rule of thumb, when you get into a space of If/then exceeds say 3 or more, it’s time to look at using a switch statement, for ease of readability, maintenance, etc. You also almost never read Write-Host, unless you are using foreground or background colors or other custom formatting needs. If you are looking to just send to the screen, Write-Output is the default, so, you don’t need Write-* at all.

Example:

function Return-DropDown 
{
    $script:Choice1 = $DropDownType.SelectedItem.ToString()
    
    switch ($Choice1.Text) 
    { 
        'DESKTOP' { $Desktop | Out-Default}
        'LAPTOP'  { $LAPTOP | Out-Default }
        default   { 'The choice could not be determined.' }
    }
}



function Return-DropDown 
{
    $script:Choice2 = $DropDownRegion.SelectedItem.ToString()

    switch ($Choice2.Text) 
    { 
        'ZWELITSHA'         { $Site00 | Out-Default }
        'BUTTERWORTH'       { $Site02 | Out-Default }
        'BIZANA'            { $Site03 | Out-Default }
        'COFIMVABA'         { $Site04 | Out-Default }
        'CRADOCK'           { $Site05 | Out-Default }
        'EAST LONDON'       { $Site06 | Out-Default }
        'FORT BEAUFORT'     { $Site07 | Out-Default }
        'GRAHAMS TOWN'      { $Site08 | Out-Default }
        'GRAAF REINET'      { $Site09 | Out-Default }
        'IDUTYWA'           { $Site10 | Out-Default }
        'JJ SERFONTEIN'     { $Site11 | Out-Default }
        'KING WILLIAMSTOWN' { $Site12 | Out-Default }
        'LIBODE'            { $Site13 | Out-Default }
        'LUSIKISIKI'        { $Site14 | Out-Default }
        'MT AYLIFF'         { $Site18 | Out-Default }
        'MT FLETCHER'       { $Site15 | Out-Default }
        'MT FRERE'          { $Site16 | Out-Default }
        'MALUTHI'           { $Site17 | Out-Default }
        'MTHATHA'           { $Site19 | Out-Default }
        'NGCOBO'            { $Site20 | Out-Default }
        'PORT ELIZABETH'    { $Site21 | Out-Default }
        'QUMBU'             { $Site22 | Out-Default }
        'QUEENSTOWN'        { $Site23 | Out-Default }
        'STERKSPRUIT'       { $Site24 | Out-Default }
        'UITENHAGE'         { $Site25 | Out-Default }
        'LADY FRERE'        { $Site28 | Out-Default }
        default             { 'The choice could not be determined.' }
    }
}

And as a rule, unless you need to expand variables, use single quotes for strings. If you only have one variable, that you are using / outputting, it does not really needs double quotes either. It does not hurt having them there, but just saying.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules https://www.sconstantinou.com/powershell-quotes https://www.red-gate.com/simple-talk/sysadmin/powershell/when-to-quote-in-powershell https://www.itprotoday.com/powershell/single-quotes-vs-double-quotes-powershell-whats-difference https://trevorsullivan.net/2016/07/20/powershell-quoting https://blogs.msdn.microsoft.com/koryt/2018/03/01/powershell-for-programmers-strings-quotes-and-quirks

As for…

I am new to this and would appreciate some help.

You can start with clicking and using the resources on this site, by clicking the ‘Free Resources’ link in the left navigation pane as well as these resource threads.

https://www.reddit.com/r/PowerShell/comments/aw8cvk/course_to_increase_knowledge_of_windows/ehl4ixm/?context=3
https://www.reddit.com/r/PowerShell/comments/ausa1n/beginner_help/ehawij5/?context=3
https://www.reddit.com/r/PowerShell/comments/ar6cvt/powershell_in_depth_second_edition/egmlpom/?context=3
https://ww.reddit.com/r/PowerShell/comments/afqmmw/i_want_to_help_my_husband_advance_his_powershell/ee3k6p6/?context=3

And this…

https://blogs.msmvps.com/richardsiddaway/2019/02/21/the-source-of-powershell-cmdlets

Hi Rob,

 

Do I leave the functions as they are, or do I delete them? I am totally new to this. I understand the logic in what you have given me, but not sue how to fit it into my script.

 

Regards,

Benjamin

Thank you for your help!

This is my first time trying powershell, and I have tried to use some stuff that I got to try and make mine work.

I will definitely look into this and add it. Will let you know what the outcome is.

 

Regards,

Benjamin

Take a look at this. I remarked out some things that were unnecessary. Being a beginner, doing a GUI first is difficult and as you learn more the probability of leveraging a function over a GUI. It’s going to much easier to learn be stripping away all of the GUI components and learning Powershell.

A label was added so when you press OK, the concatenated string will be shown.

$deviceType = @()
$deviceType += [pscustomobject]@{
    Type   = 'DESKTOP'
    Prefix = 'WS'
}
$deviceType += [pscustomobject]@{
    Type   = 'LAPTOP'
    Prefix = 'LT'
}

$site= @()
$site += [pscustomobject]@{
    Name   = 'BUTTERWORTH'
    Suffix = 'BUT'
}
$site += [pscustomobject]@{
    Name   = 'BIZANA'
    Suffix = 'BZA'
}

#$a = New-Object -comobject wscript.shell
#$b = $a.popup(“This will rename, and add your PC to the new ECDOE Domain “,0,“Message from BlackOps XXX”,1)

# Form building

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form=New-Object System.Windows.Forms.Form
$Form.width = 550
$Form.height = 300
$Form.Text = ”Rename Device”
#$Form.BackColor = [System.Drawing.Color]:: "green"
$Form.StartPosition = "CenterScreen"

$DropDownType = New-Object System.Windows.Forms.ComboBox
$DropDownType.DropDownStyle = 'DropDownList'
$DropDownType.AutoCompleteSource = 'ListItems'
$DropDownType.AutoCompleteMode = 'Append'
$DropDownType.Items.AddRange($deviceType)
$DropDownType.DisplayMember = "Type"
$DropDownType.ValueMember = "Value"
$DropDownType.Location = New-Object System.Drawing.Size(200,10)
$DropDownType.Size = New-Object System.Drawing.Size(150,30)
$DropDownType.Font = 'Microsoft Sans Serif,12'
$DropDownType.SelectedIndex = 0

$Form.Controls.Add($DropDownType)

$DropDownTypeLabel = New-Object System.Windows.Forms.Label
$DropDownTypeLabel.Location = New-Object System.Drawing.Size(10,10)
$DropDownTypeLabel.Size = New-Object System.Drawing.Size(160,30)
$DropDownTypeLabel.Font = 'Microsoft Sans Serif,8'
$DropDownTypeLabel.Text = "TYPE:"
$Form.Controls.Add($DropDownTypeLabel)

# Function to return the name of the selected "type" from the first DropDown box
#function Return-DropDownType {
#    $Choice1 = $DropDownType.SelectedItem.ToString()
#    
#   ForEach ($Item in $MyDropDownType) {
#       $DropDownRegion.Items.Add($Item)
#   } 
#}

$DropDownRegion = New-Object System.Windows.Forms.ComboBox
$DropDownRegion.DropDownStyle = 'DropDownList'
$DropDownRegion.AutoCompleteSource = 'ListItems'
$DropDownRegion.AutoCompleteMode = 'Append'
$DropDownRegion.Items.AddRange($site)
$DropDownRegion.DisplayMember = "Name"
$DropDownRegion.ValueMember = "Suffix"
$DropDownRegion.Location = New-Object System.Drawing.Size(200,55)
$DropDownRegion.Size = New-Object System.Drawing.Size(210,30)
$DropDownRegion.Font = 'Microsoft Sans Serif,12'
$DropDownRegion.SelectedIndex = 0

$Form.Controls.Add($DropDownRegion)

$DropDownRegionLabel = New-Object System.Windows.Forms.Label
$DropDownRegionLabel.Location = New-Object System.Drawing.Size(10,55)
$DropDownRegionLabel.size = New-Object System.Drawing.Size(160,30)
$DropDownRegionLabel.Text = "REGION:"
$Form.Controls.Add($DropDownRegionLabel)

# Function to return the name of the selected "region" from the second DropDown box
#function Return-DropDownRegion {
#    $Choice2 = $DropDownRegion.SelectedItem.ToString()

    #$Form.Close()

 #ForEach ($Item in $MyDropDownRegion) {
 #   $DropDownRegion.Items.Add($Item)
 #   } 

#}

## Input-Box for Persal number

$TextBoxPersalNumber = New-Object System.Windows.Forms.TextBox
$TextBoxPersalNumber.Location = New-Object System.Drawing.Point(200,105) 
$TextBoxPersalNumber.Size = New-Object System.Drawing.Size(80,30)
$TextBoxPersalNumber.Font = 'Microsoft Sans Serif,13' 

$form.Controls.Add($TextBoxPersalNumber)

$TextBoxPersalNumberLabel = New-Object System.Windows.Forms.Label
$TextBoxPersalNumberLabel.Location = New-Object System.Drawing.Size(10,105)
$TextBoxPersalNumberLabel.size = New-Object System.Drawing.Size(160,30)
$TextBoxPersalNumberLabel.Text = "PERSAL NUMBER:"

$Form.Controls.Add($TextBoxPersalNumberLabel)

## Temp label ###
$TempLabel = New-Object System.Windows.Forms.Label
$TempLabel.Location = New-Object System.Drawing.Size(10,150)
$TempLabel.size = New-Object System.Drawing.Size(400,30)
$TempLabel.Text = "Test String"
$Form.Controls.Add($TempLabel)

## OK Button ###

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(290,190)
$OKButton.Size = New-Object System.Drawing.Size(120,35)
$OKButton.Font = 'Microsoft Sans Serif,10'
$OKButton.ForeColor = [System.Drawing.Color]:: "white"
$OKButton.BackColor = [System.Drawing.Color]:: "green"
$OKButton.Text = "OK"

$OKButton.Add_Click({
    if ($TextBoxPersalNumber.Text.Length -lt 1) {
        $msg = 'Number cannot be empty!!'
    }
    else {
        $msg = '{0}-{1}-{2}' -f $DropDownType.SelectedItem.Prefix,$DropDownRegion.SelectedItem.Suffix,$TextBoxPersalNumber.Text
    }

    $TempLabel.Text = $msg
    #$Form.DialogResult = "OK"
    #$Form.close()
})

$form.Controls.Add($OKButton)

## Cancel Button ##

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(400,190)
$CancelButton.Size = New-Object System.Drawing.Size(120,35)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor =  "white"
$CancelButton.BackColor =  "red"
$CancelButton.Text = "Cancel"

$CancelButton.Add_Click({
    $Form.DialogResult = "Cancel"
    $Form.close()
})

$Form.Controls.Add($CancelButton)

$Form.Add_Shown({$Form.Activate()})

$result = $Form.ShowDialog()
Damn Rob!
This is just awesome! Thank you so much!
I added all the other sites, and added the following at the end: "write-host $NewName.Text" This allows the next part of my script to rename the pc without restarting it.
One more question if I may? How do I restrict the text input to eight characters only?
Regards,
Benjamin