gpt4 book ai didi

powershell - 功能中的IF不起作用(PowerShell)

转载 作者:行者123 更新时间:2023-12-03 00:58:22 25 4
gpt4 key购买 nike

我遇到了PowerShell及其IF-Cmdlet问题。
也可以是我要在其中输入变量以设置默认文本的TextBox。无论哪种方式,它都无法按预期工作。在代码中描述了它应该确切执行的操作^^

感谢所有帮助我的人^^
它不是为了工作或其他任何东西...它只是我尝试的一个小项目^^
哦...抱歉我的英语不好(也许),我来自德国。

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

最佳答案

这里的等号不正确。
=用于变量分配

您应该改为使用-eq运算符。

更正

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

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

另外,如果您的$ tempdir变量是 $null而不是一个空脚本,则将好像正确地填充了$ tempdir一样。要覆盖空字符串和 $null值,可以在条件语句中使用 [String]::IsNullOrEmpty($tempdir)

关于powershell - 功能中的IF不起作用(PowerShell),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com