如果功能不工作(PowerShell)
我对PowerShell及其IF Cmdlet有问题。 它也可以是文本框,我想在其中输入一个变量来设置默认文本。无论是哪种方式,它都无法按预期工作。代码中描述了它应该做什么^^ 感谢所有帮助我的人^^ 这不是为了工作或其他什么…这只是我尝试的一个小项目^^ 哦,对不起,我的英语不好(也许),我来自德国如果功能不工作(PowerShell),powershell,Powershell,我对PowerShell及其IF Cmdlet有问题。 它也可以是文本框,我想在其中输入一个变量来设置默认文本。无论是哪种方式,它都无法按预期工作。代码中描述了它应该做什么^^ 感谢所有帮助我的人^^ 这不是为了工作或其他什么…这只是我尝试的一个小项目^^ 哦,对不起,我的英语不好(也许),我来自德国 Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing function window
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
}
不正确
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)
此处的等号不正确
=
用于变量赋值
您应该改用-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
}
不正确
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)
我所能看到的是,在函数windowInstall3
中使用变量$tempDir
,但在其余代码(或函数本身)中,没有定义过这个变量。它在函数文件夹对话框中使用。如果它没有被定义,它应该输出“C:\ProgramFiles(x86)”而不是什么都没有。这就是我现在遇到的问题…你在代码中的什么地方调用这个函数?还没有。但是如果我手动填充$tempDir
,它也不起作用。如果我按下一个按钮,它将被调用,但我仍然必须实现它。然后将if更改为if(!$tempDir)
我所能看到的是,在函数windowInstall3
中使用变量$tempDir
,但在代码的其余部分(或函数本身)中没有使用变量是否定义过此项。它在函数folderDialog
中使用。如果它没有被定义,它应该输出“C:\ProgramFiles(x86)”而不是什么都没有。这就是我现在遇到的问题…你在代码中的什么地方调用这个函数?还没有。但是如果我手动填充$tempDir
,它也不起作用。如果我按下一个按钮,它将被调用,但我仍然需要实现它。然后将if更改为if(!$tempDir)
谢谢,isNullorEmpty也工作了,不使用-eq完全愚蠢xD应该更清楚它。谢谢,isNullorEmpty也工作了,不使用-eq完全愚蠢xD应该更清楚它。