2017-06-12 87 views
0

我花了幾個小時纔得到這個工作,有沒有人知道如何設置一個變量主機名,工作組,當我點擊安裝?我嘗試了幾個選項,但無法使其工作。在變量被插入後,我可以在form.closed之前向handler_button1_click添加命令。在Windows窗體中設置變量powershell textinput VB

如果主機名發出錯誤,它會回到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:\sysadmin\baseinstall\apps -Force | Out-Null 
    $URL = "https://www.systrax.nl/download/ONTRAX_AGENT.EXE" 
    $Output = "c:\sysadmin\baseinstall\apps\ONTRAX_AGENT.EXE" 
    $Silent = "/silent" 
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground 
    & $Output $Silent 
    while (!(Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Advanced Monitoring Agent.lnk")) { Start-Sleep 5 } 
    } 

    if ($checkBox2.Checked)  
    { 
    Write-Host "Install HP Support Assistant " -NoNewLine 
    New-Item -ItemType directory -Path c:\sysadmin\baseinstall\apps -Force | Out-Null 
    $URL = "https://www.systrax.nl/download/HPSupportAssistant.exe" 
    $Output = "c:\sysadmin\baseinstall\apps\HPSupportAssistant.exe" 
    $Silent = "/S" 
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground 
    & $Output $Silent 
    Write-Host "- DONE" -ForegroundColor DarkGreen -BackgroundColor green 
    while (!(Test-Path "C:\Users\Public\Desktop\HP 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 

回答

0

這部作品:)終於

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:\sysadmin\baseinstall\apps -Force | Out-Null 
    $URL = "https://www.systrax.nl/download/ONTRAX_AGENT.EXE" 
    $Output = "c:\sysadmin\baseinstall\apps\ONTRAX_AGENT.EXE" 
    $Silent = "/silent" 
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground 
    & $Output $Silent 
    while (!(Test-Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Advanced Monitoring Agent.lnk")) { Start-Sleep 5 } 
    } 

    if ($checkBox2.Checked)  
    { 
    Write-Host "Install HP Support Assistant " -NoNewLine 
    New-Item -ItemType directory -Path c:\sysadmin\baseinstall\apps -Force | Out-Null 
    $URL = "https://www.systrax.nl/download/HPSupportAssistant.exe" 
    $Output = "c:\sysadmin\baseinstall\apps\HPSupportAssistant.exe" 
    $Silent = "/S" 
    Start-BitsTransfer -Source $url -Destination $output -Priority Foreground 
    & $Output $Silent 
    Write-Host "- DONE" -ForegroundColor DarkGreen -BackgroundColor green 
    while (!(Test-Path "C:\Users\Public\Desktop\HP 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