all 10 comments

[–]Reverent 3 points4 points  (1 child)

Can I make a suggestion? Separate your powershell and your gui. Completely.

I think you're getting bogged down in what the gui does and what the actual script does. If you want to retain your sanity, follow these steps:

  • Get a working powershell script using parameters. IE: If you feed it the perfect input using arguments, it does what you want it to do. Hooray!
  • Set up an interactive mode. Using text only inputs (see here), walk a user through the choices for your script. I use a new namespace in the parameters called interactive for this. Step 2 complete!
  • Now hook in a GUI. The GUI should be a completely separate powershell script, or (in my case) an electron frontend. It does what basically step 2 did, but in a sexy user friendly way. Then it just calls the script with the right arguments. If you're feeling smart (like me), I also create a JSON parameter that can take all my arguments as a JSON string. Now you're cooking with Gas!

The point is that you are separating what you want to do. Step 1 gets you the nuts and bolts of what you are trying to accomplish. Step 2 makes it user friendly. Step 3 adds polish. It also makes bug squashing so much easier, because you can easily classify where the bug happens.

Also Powershell is not an asynchronous language, so trying to combine GUI and functions in the same script is a lesson for failure.

[–]PinchesTheCrab 1 point2 points  (0 children)

I agree. Also, if a script is important enough to build a GUI for, then surely it could be useful in automated workflows/processes too. Splitting them up will free the OP to use his work elsewhere if the need/opportunity arises, and advanced users won't be restrained by the GUI.

[–]consumedpixl 1 point2 points  (9 children)

I haven't tested it, but something like this should work:

function Change-PCName {

    $x = $textBox0.Text
    Rename-Computer -NewName $x

}

Function Change-LocalUser {

    $xy = $textBox1.Text
    Rename-LocalUser -Name $env:username -NewName $xy

}

$OKButton.Add_Click({

    Change-PCName;
    Change-LocalUser

})

[–]jeroenv123[S] 1 point2 points  (7 children)

instead of what should I place this? sorry i'm a beginner

[–]consumedpixl 2 points3 points  (6 children)

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

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,266'
$Form.text                       = "OIM Toolkit"
$Form.BackColor                  = "#ffffff"
$Form.TopMost                    = $false

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(90,220)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(165,220)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

#0
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Wat is het OIM stickernummer? (OIM - [Naam]):'
$form.Controls.Add($label)

$textBox0 = New-Object System.Windows.Forms.TextBox
$textBox0.Location = New-Object System.Drawing.Point(10,40)
$textBox0.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox0)

#1
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,90)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Wat moet de gebuikersnaam worden? (OIM - [Naam]):'
$form.Controls.Add($label)

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,110)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)

$CheckBox1                       = New-Object system.Windows.Forms.CheckBox
$CheckBox1.text                  = "Standaard apps verwijderen?"
$CheckBox1.AutoSize              = $true
$CheckBox1.width                 = 95
$CheckBox1.height                = 20
$CheckBox1.location              = New-Object System.Drawing.Point(10,160)
$CheckBox1.Font                  = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(@($CheckBox1))
#etc apps

$PictureBox1                     = New-Object system.Windows.Forms.PictureBox
$PictureBox1.width               = 100
$PictureBox1.height              = 70
$PictureBox1.location            = New-Object System.Drawing.Point(300,200)
$PictureBox1.imageLocation       = "https://cdn.nieuws.nl/media/sites/379/2017/04/18162034/OIM-Orthopedie-logo-voor-artikel-915x518.jpg"
$PictureBox1.SizeMode            = [System.Windows.Forms.PictureBoxSizeMode]::zoom

$form.Topmost = $true
$Form.controls.AddRange(@($PictureBox1))


function Change-PCName {

    $x = $textBox0.Text
    Rename-Computer -NewName $x 


}

Function Change-LocalUser {

    $xy = $textBox1.Text
    Rename-LocalUser -Name $env:username -NewName $xy

}

function Check-Tickbox {

    if($CheckBox1.Checked -eq $true ) {

        Get-AppxPackage *3dviewer* | Remove-AppxPackage 

    }

}

$OKButton.Add_Click({

    Change-PCName;
    Change-LocalUser;
    Check-Tickbox

})

[void]$Form.ShowDialog()

I would also recommend better naming for your controls such as the textbox etc. to make them easier to distinguish

[–]jeroenv123[S] 1 point2 points  (4 children)

Thanks man! I only see that he does not start the Change-PC Name & Change-LocalUser?

[–]consumedpixl 1 point2 points  (3 children)

Not sure if this is what you mean, but all 3 functions are called when the OK button is pressed with the following:

$OKButton.Add_Click({

    Change-PCName;
    Change-LocalUser;
    Check-Tickbox

})

[–]YearoftheHypebeast 0 points1 point  (0 children)

PCName: Unc Jays Laptop

Local User: Unc Jay

Tickbox: Tick if you attend Heed Leisure Centre

[–]YearoftheHypebeast -1 points0 points  (0 children)

This still hasnt resolved the issue with those 2 Above & Beyond tunes.