DragonCoder
DragonCoder

Reputation: 304

IF in a Function not working (PowerShell)

I got a problem with PowerShell and its IF-Cmdlet. It can also be the TextBox where I want to input a variable to set a default text. In either way it doesn't work as intended. What it should do exactly is described im the Code^^

Thanks to all people helping me out^^ It is not for work or anything...its just a little project I try^^ Oh...and sorry for my bad English(maybe), I'm from Germany.

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

function windowInstall1()
{

$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
    #Adding a Label(Header)
    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Size(10,10)
    $Label1.Text = "Welcome to the TimeStamp Installation Setup"
    $Label1.Size = New-Object System.Drawing.Size(450,40)
    $Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
    $window.Controls.Add($Label1)
    #Adding a Label
    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.Size(10,50)
    $Label2.Text = ("To Continue the Installation please press " + '"' + "Continue" + '"')
    $Label2.Size = New-Object System.Drawing.Size(450,20)
    $Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
    $window.Controls.Add($Label2)
    #Adding a Label
    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.Size(10,71)
    $Label3.Text = ("Else press " + '"' + "Exit" + '"' + " to cancel the installation")
    $Label3.Size = New-Object System.Drawing.Size(450,20)
    $Label3.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
    $window.Controls.Add($Label3)
    #Adding a button
    $windowButton1 = New-Object System.Windows.Forms.Button
    $windowButton1.Location = New-Object System.Drawing.Size(100,500)
    $windowButton1.Size = New-Object System.Drawing.Size(75,50)
    $windowButton1.Text = "Continue"
    $windowButton1.Add_Click({
        $global:status1 = "true"
        $window.Dispose()
        windowInstall2
    })
    $window.Controls.Add($windowButton1)
    #Adding a button
    $windowButton2 = New-Object System.Windows.Forms.Button
    $windowButton2.Location = New-Object System.Drawing.Size(300,500)
    $windowButton2.Size = New-Object System.Drawing.Size(75,50)
    $windowButton2.Text = "Exit"
    $windowButton2.Add_Click({
        $global:status1 = "false"
        $window.Dispose()
    })
    $window.Controls.Add($windowButton2)

[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))


}

function windowInstall2()
{
$window = New-Object System.Windows.Forms.Form
$window.Width = 550
$window.Height = 600
$window.Text = "TimeStampProgram Installer"
    #Adding a Label(Header)
    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Size(10,10)
    $Label1.Text = "Choose your Program"
    $Label1.Size = New-Object System.Drawing.Size(450,40)
    $Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
    $window.Controls.Add($Label1)
    #Adding RadioButtons in a GroupBox
    $radioButton1 = New-Object System.Windows.Forms.RadioButton
    $radioButton2 = New-Object System.Windows.Forms.RadioButton
    $groupBox1 = New-Object System.Windows.Forms.GroupBox
    $groupBox1.Controls.AddRange(
    @(
    $radioButton1,
    $radioButton2
    ))
    $groupBox1.Location = New-Object System.Drawing.Point(10, 60)
    $groupBox1.Name = 'groupBox'
    $groupBox1.Size = New-Object System.Drawing.Size(160, 100)
    $groupBox1.Text = 'Programms'

    # radioButton1
    $radioButton1.Location = New-Object System.Drawing.Point(8, 32)
    $radioButton1.Name = 'Button1'
    $radioButton1.Text = 'TimesStamp'
    $radioButton1.Size = New-Object System.Drawing.Size(150, 20)

    # radioButton2
    $radioButton2.Location = New-Object System.Drawing.Point(8, 64)
    $radioButton2.Name = 'Button2'
    $radioButton2.Text = 'TimeStamp with Text'
    $radioButton2.Size = New-Object System.Drawing.Size(150, 20)

    $window.Controls.Add($groupBox1)
    #Adding a Button
    $windowButton1 = New-Object System.Windows.Forms.Button
    $windowButton1.Location = New-Object System.Drawing.Size(100,500)
    $windowButton1.Size = New-Object System.Drawing.Size(75,50)
    $windowButton1.Text = "Continue"
    $windowButton1.Add_Click({
        $global:status2 = "true"
        $window.Dispose()
        windowInstall3
    })
    $window.Controls.Add($windowButton1)
    #Adding a Button
    $windowButton2 = New-Object System.Windows.Forms.Button
    $windowButton2.Location = New-Object System.Drawing.Size(300,500)
    $windowButton2.Size = New-Object System.Drawing.Size(75,50)
    $windowButton2.Text = "Exit"
    $windowButton2.Add_Click({
        $global:status2 = "false"
        $window.Dispose()
    })
    $window.Controls.Add($windowButton2)

[void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))

$status2
}

function folderDialog()
{
    #Choose a Folder
    $ChooseFolder = New-Object System.Windows.Forms.FolderBrowserDialog
    $ChooseFolder.Description = 'Select the Saving Location Folder'
    $ChooseFolder.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
    $global:tempDir = $ChooseFolder.SelectedPath
}

function windowInstall3()
{
    $window = New-Object System.Windows.Forms.Form
    $window.Width = 550
    $window.Height = 600
    $window.Text = "TimeStampProgram Installer"
        #Another Label(Header)
        $Label1 = New-Object System.Windows.Forms.Label
        $Label1.Location = New-Object System.Drawing.Size(10,10)
        $Label1.Text = "Setup Install Location"
        $Label1.Size = New-Object System.Drawing.Size(450,40)
        $Label1.Font = New-Object System.Drawing.Font("Lucidia Console" , 15, [System.Drawing.FontStyle]::Regular)
        $window.Controls.Add($Label1)
        #Another Label
        $Label2 = New-Object System.Windows.Forms.Label
        $Label2.Location = New-Object System.Drawing.Size(10,80)
        $Label2.Text = "Choose the Folder where the Installation should take place"
        $Label2.Size = New-Object System.Drawing.Size(450,40)
        $Label2.Font = New-Object System.Drawing.Font("Lucidia Console" , 10, [System.Drawing.FontStyle]::Regular)
        $window.Controls.Add($Label2)

        #Here is where I have Problems
        #If $tempDir is empty it should put a default Path
        #If not it should use $tempDir
        $windowTextBox1 = New-Object System.Windows.Forms.TextBox
        $windowTextBox1.Location = New-Object System.Drawing.Size(10,130)
        $windowTextBox1.Size = New-Object System.Drawing.Size(300,150) 
        if($tempDir = "")
        {
            $windowTextBox1.Text = "C:\Program Files (x86)"
        }
        else
        {
            $windowTextBox1.Text = $tempDir
        }
        $window.Controls.Add($windowTextBox1)

        $windowButton1 = New-Object System.Windows.Forms.Button
        $windowButton1.Location = New-Object System.Drawing.Size(100,500)
        $windowButton1.Size = New-Object System.Drawing.Size(75,50)
        $windowButton1.Text = "Continue"
        $windowButton1.Add_Click({
            $global:status3 = "true"
            $window.Dispose()
        })
        $window.Controls.Add($windowButton1)
        #Add another Button
        $windowButton2 = New-Object System.Windows.Forms.Button
        $windowButton2.Location = New-Object System.Drawing.Size(300,500)
        $windowButton2.Size = New-Object System.Drawing.Size(75,50)
        $windowButton2.Text = "Exit"
        $windowButton2.Add_Click({
            $global:status3 = "false"
            $window.Dispose()
        })
        $window.Controls.Add($windowButton2)

    [void]$window.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))

    $status3
}

windowInstall1

Upvotes: 1

Views: 62

Answers (1)

Sage Pourpre
Sage Pourpre

Reputation: 10323

The equal sign here is incorrect.

= is used for variable assignment

You should be using the -eq operator instead.

Correct

if ($tempDir -eq "") {
    $windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
    $windowTextBox1.Text = $tempDir
}

Incorrect

# This is not a valid IF condition
if ($tempDir = "") {
    $windowTextBox1.Text = "C:\Program Files (x86)"
}
else {
    $windowTextBox1.Text = $tempDir
}

Additionally, if your $tempdir variable is $null instead of an empty script, this will be seen as if $tempdir is correctly populated. To cover both an empty string and a $null value, you can use [String]::IsNullOrEmpty($tempdir) in your condition statement.

Upvotes: 3

Related Questions