在Windows中设置变量,形成PowerShell文本输入VB



我花了几个小时才让它工作,有谁知道如何在我点击安装时设置变量主机名、工作组?我尝试了几个选项,但无法使其工作。插入变量后,我可以在form.closed之前向handler_button1_click添加一个命令。

如果主机名给出错误,它将返回到form1以重新输入该主机名,那就更完美了。

function GenerateForm {
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$listBox1 = New-Object System.Windows.Forms.ListBox
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$b1= $false
$b2= $false
$b3= $false
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
$handler_button1_Click= 
{
    if ($checkBox1.Checked)     
    {
    Write-Host "Install SysTrax OnTrax " -NoNewLine
    New-Item -ItemType directory -Path c:sysadminbaseinstallapps -Force  | Out-Null
    $URL = "https://www.systrax.nl/download/ONTRAX_AGENT.EXE"
    $Output = "c:sysadminbaseinstallappsONTRAX_AGENT.EXE"
    $Silent = "/silent"
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground
    & $Output $Silent
    while (!(Test-Path "C:ProgramDataMicrosoftWindowsStart MenuProgramsAdvanced Monitoring Agent.lnk")) { Start-Sleep 5 }
    }
    if ($checkBox2.Checked)    
    {  
    Write-Host "Install HP Support Assistant " -NoNewLine
    New-Item -ItemType directory -Path c:sysadminbaseinstallapps -Force  | Out-Null
    $URL = "https://www.systrax.nl/download/HPSupportAssistant.exe"
    $Output = "c:sysadminbaseinstallappsHPSupportAssistant.exe"
    $Silent = "/S"
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground
    & $Output $Silent
    Write-Host "- DONE" -ForegroundColor DarkGreen -BackgroundColor green
    while (!(Test-Path "C:UsersPublicDesktopHP Support Assistant.lnk")) { Start-Sleep 5 }
    }
    if ($checkBox3.Checked)    
    {
    Invoke-Item C:Windows
    }
    $form1.Close()
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$form1.Text = "Systrax External Apps"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 300
$System_Drawing_Size.Height = 160
$form1.ClientSize = $System_Drawing_Size
$form1.StartPosition = "CenterScreen"
$form1.Topmost = $True
$mhostname = New-Object System.Windows.Forms.TextBox 
$mhostname.Text="hostname" 
$mhostname.Top="25" 
$mhostname.Left="10" 
$mhostname.Anchor="Left,Top" 
$mhostname.Size = New-Object System.Drawing.Size(100,23) 
$form1.Controls.Add($mhostname) 
$mworkgroup = New-Object System.Windows.Forms.TextBox 
$mworkgroup.Text="workgroup" 
$mworkgroup.Top="50" 
$mworkgroup.Left="10" 
$mworkgroup.Anchor="Left,Top" 
$mworkgroup.Size = New-Object System.Drawing.Size(100,23) 
$form1.Controls.Add($mworkgroup) 
$button1.TabIndex = 4
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Install"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 130
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)
$form1.Controls.Add($button1)
$checkBox3.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 140
$System_Drawing_Size.Height = 24
$checkBox3.Size = $System_Drawing_Size
$checkBox3.TabIndex = 2
$checkBox3.Text = "Spare"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 150
$System_Drawing_Point.Y = 75
$checkBox3.Location = $System_Drawing_Point
$checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox3.Name = "checkBox3"
$form1.Controls.Add($checkBox3)
$checkBox2.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 140
$System_Drawing_Size.Height = 24
$checkBox2.Size = $System_Drawing_Size
$checkBox2.TabIndex = 1
$checkBox2.Text = "HP Support Assistant"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 150
$System_Drawing_Point.Y = 50
$checkBox2.Location = $System_Drawing_Point
$checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox2.Name = "checkBox2"
$form1.Controls.Add($checkBox2)
$checkBox1.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 140
$System_Drawing_Size.Height = 24
$checkBox1.Size = $System_Drawing_Size
$checkBox1.TabIndex = 0
$checkBox1.Text = "SysTrax OnTrax"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 150
$System_Drawing_Point.Y = 25
$checkBox1.Location = $System_Drawing_Point
$checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox1.Name = "checkBox1"
$form1.Controls.Add($checkBox1)
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm

这:)最后

function GenerateForm {
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$listBox1 = New-Object System.Windows.Forms.ListBox
$checkBox3 = New-Object System.Windows.Forms.CheckBox
$checkBox2 = New-Object System.Windows.Forms.CheckBox
$checkBox1 = New-Object System.Windows.Forms.CheckBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$b1= $false
$b2= $false
$b3= $false
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
$handler_button1_Click= 
{
    if ($checkBox1.Checked)     
    {
    Write-Host "Install SysTrax OnTrax " -NoNewLine
    New-Item -ItemType directory -Path c:sysadminbaseinstallapps -Force  | Out-Null
    $URL = "https://www.systrax.nl/download/ONTRAX_AGENT.EXE"
    $Output = "c:sysadminbaseinstallappsONTRAX_AGENT.EXE"
    $Silent = "/silent"
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground
    & $Output $Silent
    while (!(Test-Path "C:ProgramDataMicrosoftWindowsStart MenuProgramsAdvanced Monitoring Agent.lnk")) { Start-Sleep 5 }
    }
    if ($checkBox2.Checked)    
    {  
    Write-Host "Install HP Support Assistant " -NoNewLine
    New-Item -ItemType directory -Path c:sysadminbaseinstallapps -Force  | Out-Null
    $URL = "https://www.systrax.nl/download/HPSupportAssistant.exe"
    $Output = "c:sysadminbaseinstallappsHPSupportAssistant.exe"
    $Silent = "/S"
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground
    & $Output $Silent
    Write-Host "- DONE" -ForegroundColor DarkGreen -BackgroundColor green
    while (!(Test-Path "C:UsersPublicDesktopHP Support Assistant.lnk")) { Start-Sleep 5 }
    }
    if ($checkBox3.Checked)    
    {
    Invoke-Item C:Windows
    }
    #Change hostname
    Rename-Computer -NewName $mhostname.Text
    #Change workgroupname
    Add-Computer -WorkGroupName $mworkgroup.Text
    $form1.Close()
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$form1.Text = "Systrax External Apps"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 300
$System_Drawing_Size.Height = 160
$form1.ClientSize = $System_Drawing_Size
$form1.StartPosition = "CenterScreen"
$form1.Topmost = $True
$mhostname = New-Object System.Windows.Forms.TextBox 
$mhostname.Text="hostname" 
$mhostname.Top="25" 
$mhostname.Left="10" 
$mhostname.Anchor="Left,Top" 
$mhostname.Size = New-Object System.Drawing.Size(100,23) 
$form1.Controls.Add($mhostname) 
$mworkgroup = New-Object System.Windows.Forms.TextBox 
$mworkgroup.Text="workgroup" 
$mworkgroup.Top="50" 
$mworkgroup.Left="10" 
$mworkgroup.Anchor="Left,Top" 
$mworkgroup.Size = New-Object System.Drawing.Size(100,23) 
$form1.Controls.Add($mworkgroup) 
$button1.TabIndex = 4
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Install"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 130
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)
$form1.Controls.Add($button1)
$checkBox3.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 140
$System_Drawing_Size.Height = 24
$checkBox3.Size = $System_Drawing_Size
$checkBox3.TabIndex = 2
$checkBox3.Text = "Spare"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 150
$System_Drawing_Point.Y = 75
$checkBox3.Location = $System_Drawing_Point
$checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox3.Name = "checkBox3"
$form1.Controls.Add($checkBox3)
$checkBox2.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 140
$System_Drawing_Size.Height = 24
$checkBox2.Size = $System_Drawing_Size
$checkBox2.TabIndex = 1
$checkBox2.Text = "HP Support Assistant"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 150
$System_Drawing_Point.Y = 50
$checkBox2.Location = $System_Drawing_Point
$checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox2.Name = "checkBox2"
$form1.Controls.Add($checkBox2)
$checkBox1.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 140
$System_Drawing_Size.Height = 24
$checkBox1.Size = $System_Drawing_Size
$checkBox1.TabIndex = 0
$checkBox1.Text = "SysTrax OnTrax"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 150
$System_Drawing_Point.Y = 25
$checkBox1.Location = $System_Drawing_Point
$checkBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox1.Name = "checkBox1"
$form1.Controls.Add($checkBox1)
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm

最新更新