Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help to convert powershell script into a gui

Posted on 2016-11-10
9
Medium Priority
?
506 Views
Last Modified: 2016-11-12
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
Comment
Question by:eperez0968
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:Dustin Saunders
ID: 41882409
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
 

Author Comment

by:eperez0968
ID: 41882427
Or convert it to an HTA?
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 41883776
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

Author Comment

by:eperez0968
ID: 41883961
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
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 41884032
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
 

Author Closing Comment

by:eperez0968
ID: 41884137
Awesome Work!!! quick and clean....
1
 
LVL 71

Expert Comment

by:Qlemo
ID: 41884564
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 41884662
Q,

Is there also a different way to do:

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

Any documentation links on these changes?

~bp
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41884677
That is the recommended way.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question