我有一个程序,该程序收集有关计算机的信息并显示一些数据。但是,如果我想收集有关另一台计算机的信息,我无法弄清楚如何在变量上获取新值。下面是我的一些代码。
包含计算机值的变量是DatorNamn。
关于如何将新值添加到该变量的任何建议?
[void] [System.Reflection.Assembly]::LoadWithPartialName("system.windows.form")
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
#test av variable
$DatorNamn = [Microsoft.VisualBasic.Interaction]::InputBox('Skriv in DatorNamn', 'DatorNamn', "$env:Computername") | Write-Output
#Skriver Formen
$Form1 = New-Object System.Windows.Forms.Form
$form1.Size = New-Object System.Drawing.Size(450,415)
$form1.text = "Scripts Target:$DatorNamn"
$form1.BackColor = "darkgray"
$form1.FormBorderStyle = "FixedDialog"
$form1.MinimizeBox = $true
$form1.MaximizeBox = $true
$Form1.ControlBox = $true
$form1.Font = "Segoe UI"
#Skriver Knapp byt DatorNamn
$KnappBdata = New-Object System.Windows.Forms.Button
$KnappBdata.Location = New-Object System.Drawing.Size(260,152)
$KnappBdata.Size = New-Object System.Drawing.Size(100,32)
$KnappBdata.TextAlign = "MiddleCenter"
$KnappBdata.ForeColor = "black"
$KnappBdata.BackColor = "White"
$KnappBdata.Text = "Byt Dator"
$KnappBdata.Add_click({
$outputBox.Text = [Microsoft.VisualBasic.Interaction]::InputBox('Skriv in DatorNamn', 'DatorNamn', "$env:COMPUTERNAME") | Write-Output
})
$Form1.Controls.Add($KnappBdata)
[void] $Form1.ShowDialog()
´´´
答案 0 :(得分:0)
您拥有所需的一切:Button事件需要在每次按下时刷新GUI对象或变量的文本。我对你的事做了些周记。尝试按下按钮并多次更改文本:
[void] [System.Reflection.Assembly]::LoadWithPartialName("system.windows.form")
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
#test av variable
$DatorNamn = [Microsoft.VisualBasic.Interaction]::InputBox('Skriv in DatorNamn', 'DatorNamn', "$env:Computername") | Write-Output
#Skriver Formen
$Form1 = New-Object System.Windows.Forms.Form
$form1.Size = New-Object System.Drawing.Size(450,415)
$form1.text = "Scripts Target:$DatorNamn"
$form1.BackColor = "darkgray"
$form1.FormBorderStyle = "FixedDialog"
$form1.MinimizeBox = $true
$form1.MaximizeBox = $true
$Form1.ControlBox = $true
$form1.Font = "Segoe UI"
#Skriver Knapp byt DatorNamn
$KnappBdata = New-Object System.Windows.Forms.Button
$KnappBdata.Location = New-Object System.Drawing.Size(260,152)
$KnappBdata.Size = New-Object System.Drawing.Size(100,32)
$KnappBdata.TextAlign = "MiddleCenter"
$KnappBdata.ForeColor = "black"
$KnappBdata.BackColor = "White"
$KnappBdata.Text = "Byt Dator"
$KnappBdata.Add_click({
# since you don't have $outputBox it will update the button
$KnappBdata.Text = [Microsoft.VisualBasic.Interaction]::InputBox('Skriv in DatorNamn', 'DatorNamn', "$env:COMPUTERNAME") | Write-Output
})
$Form1.Controls.Add($KnappBdata)
[void] $Form1.ShowDialog()
答案 1 :(得分:0)
您需要使用某种控制循环才能多次重复执行任务。
由于您的互动方法包括取消按钮,因此我们可以在执行任务之前测试来自InputBox
的响应是否为空白。在散文中,这将是“在满足特定条件的情况下,继续重复执行任务”。
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
while ($Var = [Microsoft.VisualBasic.Interaction]::InputBox('Description', 'Title', "default")) {
Write-Host "You typed: $Var"
}
如果运行此命令,它将继续运行大括号内的命令,直到不输入任何内容或单击“取消”按钮为止。