Solved

Help to convert powershell script into a gui

Posted on 2016-11-10
9
66 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 12

Expert Comment

by:Dustin Saunders
Comment Utility
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
Comment Utility
Or convert it to an HTA?
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 

Author Comment

by:eperez0968
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Awesome Work!!! quick and clean....
1
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
Q,

Is there also a different way to do:

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

Any documentation links on these changes?

~bp
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
That is the recommended way.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Powershell code used to work 7 39
Script is not working 3 25
SUM 2 INTEGER ARRAYS INTO 1 10 56
Copying file in Powershell 2 15
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article will help you understand what HashTables are and how to use them in PowerShell.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now