Link to home
Start Free TrialLog in
Avatar of Eric Perez
Eric PerezFlag for United States of America

asked on

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
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

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).
Avatar of Eric Perez

ASKER

Or convert it to an HTA?
Avatar of Bill Prew
Bill Prew

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome Work!!! quick and clean....
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.
Q,

Is there also a different way to do:

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

Any documentation links on these changes?

~bp
That is the recommended way.