[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

Help to convert powershell script into a gui

Basically

In the GUI Box it should have these basic elements.  Attached is picture of an Idea what it should look like. The code is below

1. Enter subsidiary code
2. is this a laptop or Desktop ( maybe a check box or radio button)
3. enter credentials username / Password boxes
4. enter button ( Join to domain)

========================================================================================================

<#
    .SYNOPSIS
        Connects localhost to the domain
    .DESCRIPTION
        Connects localhost to the domain and places the machine in the correct OU to allow local IT Admin to have administrative control
    .NOTES
        Version:        1.0
        Author:         Wade Dickens
        Creation Date:  11/9/2016
        Purpose/Change: Inital Creation
#>
$code = Read-Host "Please enter your 3 letter subsidiary code"
$type = Read-Host "Please enter '1' if this is a laptop or '2' if this is a desktop"

if ($type -eq "1") {$path = "OU=Laptops"}
if ($type -eq "2") {$path = "OU=Desktops"}

$fullPath = "$path,OU=Computers,OU=$code,OU=Company,DC=Dynutil,DC=com"
$hostname = $env:COMPUTERNAME -replace "111", $code

Rename-Computer -NewName $hostname -Force
Add-Computer -DomainName dynutil.com -OUPath $fullPath -Credential (Get-Credential) -Options JoinWithNewName -Restart
Join-Domain.PNG
0
Eric Perez
Asked:
Eric Perez
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Dustin SaundersDirector of OperationsCommented:
You can launch GUI elements in order, but it won't operate like a WinForm which is what it sounds like you want.  If you plan on using Powershell driven by a winform interface, you might try SAPIEN ( https://www.sapien.com/software/powershell_studio ).  Otherwise, you can use VisualStudio and make your calls through a Powershell Runspace (which is more complicated).
1
 
Eric PerezAuthor Commented:
Or convert it to an HTA?
0
 
Bill PrewCommented:
Here's a simple starting point you can use for a form that prompts for the fields you mention.  Naturally you can adjust design as needed, but it's a fairly manual process.  You may also want edit checks, etc.  I just displayed the input values for now, you can take this and add in the existing logic you have for processing.

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Windows.Forms.Application]::EnableVisualStyles() 
 
Function JoinDomain 
{ 
    Write-Host "Subsidary Code = " $field1.Text
    Write-Host "Laptop / Desktop = " $field2.Text
    Write-Host "Username = " $field3.Text
    Write-Host "Password Code = " $field4.Text
    $Form.Close()
} 
 
$Form = New-Object System.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(430,300) 
#$Form.AutoSize = $true
$Form.StartPosition = "CenterScreen" 
$Form.FormBorderStyle = 'Fixed3D' 
$Form.Text = "Join Domain" 

$fontHeader = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Bold) 

$label1 = New-Object System.Windows.Forms.Label 
$label1.Location = New-Object System.Drawing.Size(25,15) 
$label1.AutoSize = $true 
$label1.Text = "Join Laptop/PC to domain and add to Proper OU"
$label1.Font = $fontHeader

$label2 = New-Object System.Windows.Forms.Label 
$label2.Location = New-Object System.Drawing.Size(25,50) 
$label2.AutoSize = $true 
$label2.Text = "Enter 3 Letter Subsidary Code:" 

$field1 = New-Object Windows.Forms.TextBox
$field1.Location = New-Object Drawing.Point 225,50
$field1.Size = New-Object Drawing.Point 50,30
 
$label3 = New-Object System.Windows.Forms.Label 
$label3.Location = New-Object System.Drawing.Size(25,75) 
$label3.AutoSize = $true 
$label3.Text = "Enter 1 for Laptop or 2 for Desktop:" 

$field2 = New-Object Windows.Forms.TextBox
$field2.Location = New-Object Drawing.Point 225,75
$field2.Size = New-Object Drawing.Point 50,30

$label4 = New-Object System.Windows.Forms.Label 
$label4.Location = New-Object System.Drawing.Size(25,100) 
$label4.AutoSize = $true 
$label4.Text = "Enter Your Credentials" 

$label5 = New-Object System.Windows.Forms.Label 
$label5.Location = New-Object System.Drawing.Size(50,125) 
$label5.AutoSize = $true 
$label5.Text = "Username:" 

$field3 = New-Object Windows.Forms.TextBox
$field3.Location = New-Object Drawing.Point 225,125
$field3.Size = New-Object Drawing.Point 150,30

$label6 = New-Object System.Windows.Forms.Label 
$label6.Location = New-Object System.Drawing.Size(50,150) 
$label6.AutoSize = $true 
$label6.Text = "Password:" 

$field4 = New-Object Windows.Forms.TextBox
$field4.Location = New-Object Drawing.Point 225,150
$field4.Size = New-Object Drawing.Point 150,30

$buttonJoin = New-Object System.Windows.Forms.Button 
$buttonJoin.Location = New-Object System.Drawing.Size(175,200) 
$buttonJoin.AutoSize = $true
$buttonJoin.Text = "Join Domain" 
$buttonJoin.Add_Click({JoinDomain}) 

$Form.Controls.Add($label1) 
$Form.Controls.Add($label2) 
$Form.Controls.Add($field1)
$Form.Controls.Add($label3) 
$Form.Controls.Add($field2)
$Form.Controls.Add($label4) 
$Form.Controls.Add($label5) 
$Form.Controls.Add($field3)
$Form.Controls.Add($label6) 
$Form.Controls.Add($field4)
$Form.Controls.Add($buttonJoin) 

$Form.ShowDialog()

Open in new window

~bp
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Eric PerezAuthor Commented:
This is great! Bill, can you make one change... instead of asking the input of 1 for Laptops or 2 for Desktops, can you add instead Radio buttons were they click to select laptop or desktop? then that should be it.
0
 
Bill PrewCommented:
Okay, give this a try.

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Windows.Forms.Application]::EnableVisualStyles() 
 
Function JoinDomain 
{ 
    if ($radio1.Checked -eq $true) {$computer=1}
    if ($radio2.Checked -eq $true) {$computer=2}
    Write-Host "Subsidary Code = " $field1.Text
    Write-Host "Laptop / Desktop = " $computer
    Write-Host "Username = " $field3.Text
    Write-Host "Password Code = " $field4.Text
    $Form.Close()
} 
 
$form = New-Object System.Windows.Forms.Form 
$form.Size = New-Object System.Drawing.Size(430,300) 
#$form.AutoSize = $true
$form.StartPosition = "CenterScreen" 
$form.FormBorderStyle = 'Fixed3D' 
$form.Text = "Join Domain" 

$fontHeader = New-Object System.Drawing.Font("Arial",12,[System.Drawing.FontStyle]::Bold) 

$label1 = New-Object System.Windows.Forms.Label 
$label1.Location = New-Object System.Drawing.Point(25,15) 
$label1.AutoSize = $true 
$label1.Text = "Join Laptop/PC to domain and add to Proper OU"
$label1.Font = $fontHeader
$form.Controls.Add($label1) 

$label2 = New-Object System.Windows.Forms.Label 
$label2.Location = New-Object System.Drawing.Point(25,50) 
$label2.AutoSize = $true 
$label2.Text = "Enter 3 Letter Subsidary Code:" 
$form.Controls.Add($label2) 

$field1 = New-Object Windows.Forms.TextBox
$field1.Location = New-Object Drawing.Point(225,50)
$field1.Size = New-Object Drawing.Size(50,30)
$form.Controls.Add($field1)

$group1 = New-Object System.Windows.Forms.GroupBox
$group1.Location = New-Object System.Drawing.Point(25,75) 
$group1.size = New-Object System.Drawing.Size(350,45) 
#$group1.AutoSize = $true
$group1.text = "Select computer type:" 
$form.Controls.Add($group1) 

$radio1 = New-Object System.Windows.Forms.RadioButton 
$radio1.Location = new-object System.Drawing.Point(25,15) 
$radio1.Size = New-Object System.Drawing.Size(125,20) 
$radio1.Checked = $true 
$radio1.Text = "Desktop" 
$group1.Controls.Add($radio1) 

$radio2 = New-Object System.Windows.Forms.RadioButton
$radio2.Location = new-object System.Drawing.Point(200,15)
$radio2.Size = New-Object System.Drawing.Size(125,20)
$radio2.Checked = $false 
$radio2.Text = "Laptop"
$group1.Controls.Add($radio2)

$group2 = New-Object System.Windows.Forms.GroupBox
$group2.Location = New-Object System.Drawing.Point(25,125) 
$group2.size = New-Object System.Drawing.Size(350,70) 
#$group2.AutoSize = $true
$group2.text = "Enter Your Credentials:" 
$form.Controls.Add($group2) 

$label5 = New-Object System.Windows.Forms.Label 
$label5.Location = New-Object System.Drawing.Point(25,15) 
$label5.AutoSize = $true 
$label5.Text = "Username:" 
$group2.Controls.Add($label5) 

$field3 = New-Object Windows.Forms.TextBox
$field3.Location = New-Object Drawing.Point(200,15)
$field3.Size = New-Object Drawing.Size(130,30)
$group2.Controls.Add($field3)

$label6 = New-Object System.Windows.Forms.Label 
$label6.Location = New-Object System.Drawing.Point(25,40) 
$label6.AutoSize = $true 
$label6.Text = "Password:" 
$group2.Controls.Add($label6) 

$field4 = New-Object Windows.Forms.TextBox
$field4.Location = New-Object Drawing.Point(200,40)
$field4.Size = New-Object Drawing.Point(130,30)
$group2.Controls.Add($field4)

$buttonJoin = New-Object System.Windows.Forms.Button 
$buttonJoin.Location = New-Object System.Drawing.Point(175,225) 
$buttonJoin.AutoSize = $true
$buttonJoin.Text = "Join Domain" 
$buttonJoin.Add_Click({JoinDomain}) 
$form.Controls.Add($buttonJoin) 

$form.ShowDialog()

Open in new window

~bp
0
 
Eric PerezAuthor Commented:
Awesome Work!!! quick and clean....
1
 
QlemoDeveloperCommented:
Just a note:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

Open in new window

is old-style, you now use
Add-Type -Assembly System.Drawing

Open in new window

and so on.
1
 
Bill PrewCommented:
Q,

Is there also a different way to do:

[void] [System.Windows.Forms.Application]::EnableVisualStyles()

Any documentation links on these changes?

~bp
0
 
QlemoDeveloperCommented:
That is the recommended way.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now