Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

Powershell script in Add_Click

I have a working script which I would like to execute in a GUI for Powershell. In general I would like to display the selected IP (interface) from a dropdown menu as a string ("venue"). Working Script:

$result = (Get-NetIPAddress -AddressFamily IPv4)
$client_addr = $result.IPAddress
$octets = $client_addr.Split(".")
if ($octets[2] -eq'17' -And $octets[3] -In 2..125) {
     $venue = "venue1"}
elseif ($octets[2] -eq '178' -And $octets[3] -In 129..253) {
     $venue = "venue2"
     }
Write-Output "You are connected to $venue."

So I would assume to paste this code after $OKButton.Add_Click({and finish with

    [void] [Windows.Forms.MessageBox]::Show("You are connected to $venue.")

})
$objForm.Controls.Add($OKButton)`

But unfortunately nothing happens (not even an error). Hope you can help me here out.

This is the whole code:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form

$objForm.StartPosition = "CenterScreen"

$objForm.Size = New-Object System.Drawing.Size(800,500)

$result = (Get-NetIPAddress -AddressFamily IPv4)
$client_addr = $result.IPAddress
$octets = $client_addr.Split(".")

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(300,60) 
$objLabel.Size = New-Object System.Drawing.Size(1000,20) 
$objLabel.Text = "Please select interface:"
$objForm.Controls.Add($objLabel) 

    $objCombobox = New-Object System.Windows.Forms.Combobox 
    $objCombobox.Location = New-Object System.Drawing.Size(300,80) 
    $objCombobox.Size = New-Object System.Drawing.Size(200,20) 
    $objCombobox.Height = 70
    $objForm.Controls.Add($objCombobox) 
    $objForm.Topmost = $True
    $objForm.Add_Shown({$objForm.Activate()})
    $objCombobox.Items.AddRange($client_addr) 
    $objCombobox.SelectedItem 
    
    $objCombobox.Add_SelectedIndexChanged({ })


 
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(500,420)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Name = "OK"
$OKButton.DialogResult = "OK" 
 

$OKButton.Add_Click({
#venue1
if ($octets[2] -eq '17' -And $octets[3] -In 2..125) {
    $venue = "venue1"
    }
#venue2
elseif ($octets[2] -eq '0' -And $octets[3] -In 129..253) {
    $venue = "venue2"
    }
Write-Output $venue

[void] [System.Windows.Forms.MessageBox]::Show("You are connected to $venue.")

})
$objForm.Controls.Add($OKButton) 


$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(600,420)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Name = "Cancel"
$CancelButton.DialogResult = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton) 

[void] $objForm.ShowDialog()
question from:https://stackoverflow.com/questions/65915638/powershell-script-in-add-click

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

maybe you just miss "System" for the MSGbox Call? like see below.

[System. Windows.Forms.MessageBox]::Show("Your Message", Type of msgbox eg 'Hint',Buttons e.g. 'OK' or "YesNo","MSGBox Titel")

lookout for the space after System. i had to make it so it would show up Bold ^^

regards


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...