Why does my PowerShell script get stuck when closing from task bar? - windows

I'm trying to make a little script with which I can alter a keyword in a file to something I have typed into a text box. I make a copy of a directory with files, and copy them with a unique directory name to a map called temp. It also starts a file. When closing the script or by pressing Cancel, the started program gets closed and the files get deleted again.
It all works okay, but whenever I close the form via the Windows task bar, and I want to start the script again, the form won't show up and the PowerShell console shows, even when minimized, completely empty, unable to edit anything. I can close it with Ctrl+C, but it stucks again when I launch the script the next time.
This is the first script I have ever made and I'm not sure how to fix the issue.
If there is a way to delete the files in that unique random directory when the process is stopped, that would be even better. For instance, if I close the file settings.txt that I opened via the script, then delete all files.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
# Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
# makes form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(200,180)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,70)
$okButton.Size = New-Object System.Drawing.Size(160,20)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(10,100)
$cancelButton.Size = New-Object System.Drawing.Size(160,20)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(170,20)
$label.Text = 'Please enter the Server name:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(160,20)
$form.Controls.Add($textBox)
$form.Topmost = $true
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$server = $textBox.Text
$server
$folder = [System.IO.Path]::GetRandomFileName()
Copy-Item -Path C:\powershell\main\ -Destination c:\powershell\temp –Recurse
Rename-Item -Path "C:\powershell\temp\main\" -NewName $folder
(Get-Content -Path "C:\powershell\temp\$folder\settings.txt" -raw) -replace '<SERVERNAME>',"$server" | Set-Content "C:\powershell\temp\$folder\settings.txt"
$app = (Start-Process "C:\powershell\temp\$folder\settings.txt" -passthru).ID
$form.Size = New-Object System.Drawing.Size(250,100)
$form.StartPosition = 'CenterScreen'
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = "Click 'cancel' to close Server $server"
$cancelButton.Location = New-Object System.Drawing.Point(10,40)
$cancelButton.Size = New-Object System.Drawing.Size(180,20)
$cancelButton.Text = 'Cancel'
$result = $form.ShowDialog()
if($result -eq [System.Windows.Forms.DialogResult]::cancel)
{
Stop-Process $app
Remove-Item –path "C:\powershell\temp\$folder\" –recurse
}
}

Give the computer a restart to clean up the memory leaks then you test this code.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
# The reason that your form does not open sometime is caused by a memory leak. The code below should resolve the issue.
# If you add to the GUI them add that control below. If a variable does not have a .dispose() then .NET will clean it up.
# These are Events and will fire when the window closes.
# These will fix the memory leak issue.
$Form.Add_FormClosing({
$form.Dispose()
$okButton.Dispose()
$cancelButton.Dispose()
$label.Dispose()
$textBox.Dispose()
})
$Form2.Add_FormClosing({
$form2.Dispose()
$label2.Dispose()
$cancelButton2.Dispose()
})
# Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
# makes form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(200,180)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,70)
$okButton.Size = New-Object System.Drawing.Size(160,20)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(10,100)
$cancelButton.Size = New-Object System.Drawing.Size(160,20)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(170,20)
$label.Text = 'Please enter the Server name:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(160,20)
$form.Controls.Add($textBox)
$form.Topmost = $true
# Is this line needed
#$form.Add_Shown({$textBox.Select()}) # this sometimes will not let me type in the textbox
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$server = $textBox.Text
$server
$folder = [System.IO.Path]::GetRandomFileName()
Copy-Item -Path C:\powershell\main\ -Destination c:\powershell\temp –Recurse
Rename-Item -Path "C:\powershell\temp\main\" -NewName $folder
(Get-Content -Path "C:\powershell\temp\$folder\settings.txt" -raw) -replace '<SERVERNAME>',"$server" | Set-Content "C:\powershell\temp\$folder\settings.txt"
$app = (Start-Process "C:\powershell\temp\$folder\settings.txt" -passthru).ID
# This is winform. They work on comobjects and you not reuse the variable name because it will not
# free up memory resource when the are reused. I rename your variables to get around this issue.
$form2 = New-Object System.Windows.Forms.Form
$form2.Size = New-Object System.Drawing.Size(350,250)
$form2.StartPosition = 'CenterScreen'
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,20)
$label2.Size = New-Object System.Drawing.Size(350,20)
$label2.Text = "Click 'cancel' to close Server $server"
$form2.Controls.Add($label2)
$cancelButton2 = New-Object System.Windows.Forms.Button
$cancelButton2.Location = New-Object System.Drawing.Point(10,80)
$cancelButton2.Size = New-Object System.Drawing.Size(180,40)
$cancelButton2.Text = 'Cancel'
$form2.CancelButton = $cancelButton2
$form2.Controls.Add($cancelButton2)
$result = $form2.ShowDialog()
if($result -eq [System.Windows.Forms.DialogResult]::cancel)
{
Stop-Process $app
Remove-Item –path "C:\powershell\temp\$folder\" –recurse
}
}

Related

Powershell script to install font family

Below is my script istalling Monserrat fonts from zip file. I can't figure how to check if a font already installed. After installation I can open folder C:\Windows\Fonts\Montserrat and I see al of them. When I am running script second time, it is not recognize existance of this folder. Where is my mistake?
$Source = "Montserrat.zip"
$FontsFolder = "FontMontserrat"
Expand-Archive $Source -DestinationPath $FontsFolder
$FONTS = 0x14
$CopyOptions = 4 + 16;
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
$allFonts = dir $FontsFolder
foreach($File in $allFonts)
{
If((Test-Path "C:\Windows\Fonts\Montserrat") -eq $True)
{
echo "Font $File already installed"
}
Else
{
echo "Installing $File"
$CopyFlag = [String]::Format("{0:x}", $CopyOptions);
$objFolder.CopyHere($File.fullname,$CopyFlag)
}
}
Finally my script:
$Source = "Montserrat.zip"
$FontsFolder = "FontMontserrat"
Expand-Archive $Source -DestinationPath $FontsFolder -Force
$FONTS = 0x14
$CopyOptions = 4 + 16;
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
$allFonts = dir $FontsFolder
foreach($font in Get-ChildItem -Path $fontsFolder -File)
{
$dest = "C:\Windows\Fonts\$font"
If(Test-Path -Path $dest)
{
echo "Font $font already installed"
}
Else
{
echo "Installing $font"
$CopyFlag = [String]::Format("{0:x}", $CopyOptions);
$objFolder.CopyHere($font.fullname,$CopyFlag)
}
}
I am running this script by following cmd:
set batchPath=%~dp0
powershell.exe -noexit -file "%batchPath%InstMontserrat.ps1"
I don't have to run it as administrator, but user have admin permissions.
Corrections of your script based on my comment assuming Windows 10:
# well-known SID for admin group
if ('S-1-5-32-544' -notin [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups) {
throw 'Script must run as admin!'
}
$source = 'Montserrat.zip'
$fontsFolder = 'FontMontserrat'
Expand-Archive -Path $source -DestinationPath $fontsFolder
foreach ($font in Get-ChildItem -Path $fontsFolder -File) {
$dest = "C:\Windows\Fonts\$font"
if (Test-Path -Path $dest) {
"Font $font already installed."
}
else {
$font | Copy-Item -Destination $dest
}
}
If you do not want to install the font on OS level but only make it available for programs to use until reboot you may want to use this script that:
Will fail/throw if it cannot register/unregister font.
Broadcasts WM_FONTCHANGE to inform all windows that fonts have changed
Does not require administrator privileges
Does not install fonts in Windows, only makes them available for all programs in current session until reboot
Has verbose mode for debugging
Does not work with font folders
Usage:
register-fonts.ps1 [-v] [-unregister <PATH>[,<PATH>...]] [-register <PATH>[,<PATH>...]] # Register and unregister at same time
register-fonts.ps1 [-v] -unregister <PATH>
register-fonts.ps1 [-v] -register <PATH>
register-fonts.ps1 [-v] <PATH> # Will register font path
Param (
[Parameter(Mandatory=$False)]
[String[]]$register,
[Parameter(Mandatory=$False)]
[String[]]$unregister
)
# Stop script if command fails https://stackoverflow.com/questions/9948517/how-to-stop-a-powershell-script-on-the-first-error
$ErrorActionPreference = "Stop"
add-type -name Session -namespace "" -member #"
[DllImport("gdi32.dll")]
public static extern bool AddFontResource(string filePath);
[DllImport("gdi32.dll")]
public static extern bool RemoveFontResource(string filePath);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam = 0, int lParam = 0);
"#
$broadcast = $False;
Foreach ($unregisterFontPath in $unregister) {
Write-Verbose "Unregistering font $unregisterFontPath"
# https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-removefontresourcea
$success = [Session]::RemoveFontResource($unregisterFontPath)
if (!$success) {
Throw "Cannot unregister font $unregisterFontPath"
}
$broadcast = $True
}
Foreach ($registerFontPath in $register) {
Write-Verbose "Registering font $registerFontPath"
# https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-addfontresourcea
$success = [Session]::AddFontResource($registerFontPath)
if (!$success) {
Throw "Cannot register font $registerFontPath"
}
$broadcast = $True
}
if ($broadcast) {
# HWND_BROADCAST https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea
$HWND_BROADCAST = New-Object IntPtr 0xffff
# WM_FONTCHANGE https://learn.microsoft.com/en-us/windows/win32/gdi/wm-fontchange
$WM_FONTCHANGE = 0x1D
Write-Verbose "Broadcasting font change"
# Broadcast will let other programs know that fonts were changed https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea
$success = [Session]::PostMessage($HWND_BROADCAST, $WM_FONTCHANGE)
if (!$success) {
Throw "Cannot broadcase font change"
}
}
The script was inspired by this gist https://gist.github.com/Jaykul/d53a16ce5e7d50b13530acb4f98aaabd

Bring Powershell-Console to front from WinForms

im trying to bring my powershell console to front, even if it is minimized.
I found following code:
function Show-Process($Process, [Switch]$Maximize)
{
$sig = '
[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
'
if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
$type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
$hwnd = $process.MainWindowHandle
$null = $type::ShowWindowAsync($hwnd, $Mode)
$null = $type::SetForegroundWindow($hwnd)
}
Show-Process -Process (Get-Process -Id $pid)
It works fine, but when i call the function from a Button Click event, the console wont show.
What is the Problem? Is there a way to bring the powershell Console to front when using a WinForms GUI?
Here is the example GUI Code:
function Show-Process($Process, [Switch]$Maximize)
{
$sig = '
[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
'
if ($Maximize) { $Mode = 3 } else { $Mode = 4 }
$type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
$hwnd = $process.MainWindowHandle
$null = $type::ShowWindowAsync($hwnd, $Mode)
$null = $type::SetForegroundWindow($hwnd)
}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '446,266'
$Form.text = "Form"
$Form.TopMost = $false
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "button"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(75,29)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button1.Add_Click({
Show-Process -Process (Get-Process -Id $pid)
})
$Form.controls.AddRange(#($Button1))
[void]$Form.ShowDialog()
Thanks to #iRon's answer, i was able to figure it out, how i want it.
For anyone curious, the problem is, you only can get the consoles MainwindowHandle as long as ShowDialog wasn't called.
So i save the console Handle in a variable and i use the Form_Shown event to get the Form WindowHandle, since Form_Load still returns the Console Handle.
$sig = '
[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);'
$type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
[IntPtr]$handleConsole = (Get-Process -Id $pid).MainWindowHandle
[void]$type::ShowWindowAsync($handleConsole, 4);[void]$type::SetForegroundWindow($handleConsole)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '446,266'
$Form.text = "Form"
$Form.TopMost = $false
$Form.Add_Shown({
$global:handleForm = (Get-Process -Id $pid).MainWindowHandle
})
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Clone ad-USer"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(75,29)
$Button1.Font = 'Microsoft Sans Serif,10'
$Button1.Add_Click({
[void]$type::ShowWindowAsync($handleConsole, 4);[void]$type::SetForegroundWindow($handleConsole)
Read-Host -Prompt "Please Enter a Value"
[void]$type::ShowWindowAsync($global:handleForm, 4);[void]$type::SetForegroundWindow($global:handleForm)
})
$Form.controls.AddRange(#($Button1))
[void]$Form.ShowDialog()
Now, if i press the Button, to console pops up in front.
After the User enter something into the Console, the Form comes to front again.
Unfortunately, I can't completely fix it, but maybe others might help you further based on my findings:
First of all, the process within the button click event is a different process space as where the parent PowerShell host runs in. This can be easily proven but revealing the $hwhd with Write-Host $hwnd in the Show-Process function and also calling the Show-Process function prior calling the ShowDialog:
Show-Process -Process (Get-Process -Id $pid)
[void]$Form.ShowDialog()
In other words: to fix this part, you will need to the catch the parent $Pid from the PowerShell window first:
$Button1.Add_Click({
Show-Process -Process $MyProcess
})
$Form.controls.AddRange(#($Button1))
$MyProcess = Get-Process -Id $pid
Show-Process -Process $MyProcess
[void]$Form.ShowDialog()
The above snippet works, but as soon I remove (or comment out) the line Show-Process -Process $MyProcess (at the host level), it breaks again...
As you've discovered, .MainWindowHandle is not a static property (from the linked docs; emphasis added):
The main window is the window opened by the process that currently has the focus [...]
Therefore, what the value of the current process' .MainWindowHandle property changes from the console-window handle to the WinForms window while the form is being displayed.[1]
Caching the console-window handle before you display the form is definitely an option, but there's an easier way, given that you're already using Add-Member with WinAPI P/Invoke declarations: The GetConsoleWindow() WinAPI function always returns the current process' console-window handle.
Additionally, your $Forms form instance has a .Handle property, which directly returns the form's window handle - no (Get-Process -Id $pid).MainWindowHandle call needed.
The following solution therefore needs no global or script-level variables and confines querying the window handles to the button-click event handler:
# P/Invoke signatures - note the addition of GetConsoleWindow():
$sig = '
[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow();'
$type = Add-Type -MemberDefinition $sig -Name WindowAPI -PassThru
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form -Property #{
ClientSize = '446,266'
text = "Form"
}
$Button1 = New-Object system.Windows.Forms.Button -Property #{
text = "Test"
location = New-Object System.Drawing.Point(75, 29)
}
$Button1.Add_Click({
# Get this form's window handle.
$handleForm = $Form.Handle # More generically: $this.FindForm().Handle
# Get the console window's handle.
$handleConsole = $type::GetConsoleWindow()
# Activate the console window and prompt the user.
$null = $type::ShowWindowAsync($handleConsole, 4); $null = $type::SetForegroundWindow($handleConsole)
Read-Host -Prompt "Please Enter a Value"
# Reactivate this form.
$null = $type::ShowWindowAsync($handleForm, 4); $null = $type::SetForegroundWindow($handleForm)
})
$Form.controls.AddRange(#($Button1))
$null = $Form.ShowDialog()
[1] Note that a cached process object doesn't dynamically update its .MainWindowHandle value; you have to call .Refresh() manually.
Because iRon's solution caches the current-process object before displaying the form, it still happens to reflect the console-window handle inside the button-click handler.

Powershell to Whitelist IP in IIS?

i am trying to Whitelist a IP using powershell. I am aiming to do this via a GUI. My code for whitelisting IP runs smooth otherwise but while taking GUI approach it throws Below error:
My code for the same is:
import-module WebAdministration
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(600,400)
$type="Allow"
function SaveConfig
{
Set-WebConfigurationProperty -Filter /system.webserver/security/ipsecurity -Name allowUnlisted -Value $False -Location "$url.text"
Add-WebConfiguration /system.webserver/security/ipsecurity -loaction "$url.text" -Value #{ipAddress="$ip.text";allowed="True"} -pspath IIS:\
$r1 = "The given range of ip addresses are added into whitelist"
$outputbox.text = $r1
}
$url_label = New-Object System.Windows.Forms.Label
$url_label.Location = New-Object System.Drawing.Size(40,20)
$url_label.Size = New-Object System.Drawing.Size(280,20)
$url_label.Text = "Please enter the URL"
$Form.Controls.Add($url_label)
$url = New-Object System.Windows.Forms.TextBox
$url.Location = New-Object System.Drawing.Size(40,50)
$url.Size = New-Object System.Drawing.Size(260,60)
$Form.Controls.Add($url)
$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(40,80)
$DropDownBox.Size = New-Object System.Drawing.Size(180,20)
$DropDownBox.DropDownHeight = 400
$Form.Controls.Add($DropDownBox)
$wksList=#("Allow","Deny")
foreach ($wks in $wksList)
{
$DropDownBox.Items.Add($wks)
}
$ip_label = New-Object System.Windows.Forms.Label
$ip_label.Location = New-Object System.Drawing.Size(40,110)
$ip_label.Size = New-Object System.Drawing.Size(280,20)
$ip_label.Text = "Please enter the IP address"
$Form.Controls.Add($ip_label)
$ip = New-Object System.Windows.Forms.TextBox
$ip.Location = New-Object System.Drawing.Size(40,140)
$ip.Size = New-Object System.Drawing.Size(260,60)
$Form.Controls.Add($ip)
$mask_label = New-Object System.Windows.Forms.Label
$mask_label.Location = New-Object System.Drawing.Size(40,170)
$mask_label.Size = New-Object System.Drawing.Size(280,20)
$mask_label.Text = "Please enter the Subnet Mask"
$Form.Controls.Add($mask_label)
$mask = New-Object System.Windows.Forms.TextBox
$mask.Location = New-Object System.Drawing.Size(40,200)
$mask.Size = New-Object System.Drawing.Size(260,60)
$mask.Text="255.255.255.0"
$Form.Controls.Add($mask)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(40,230)
$Button.Size = New-Object System.Drawing.Size(110,50)
$Button.Text = "save"
$Button.Add_Click({SaveConfig})
$Form.Controls.Add($Button)
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(350,50)
$outputBox.Size = New-Object System.Drawing.Size(200,200)
$outputBox.MultiLine = $True
$outputBox.ReadOnly= $True
$Form.Controls.Add($outputBox)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
Add-WebConfiguration /system.webserver/security/ipsecurity -location "$url.text" -Value #{ipAddress="$ip.text";allowed="True"} -pspath IIS:\
Just remove inverted commas from the code in bold. :)

PowerShell Windows Form

I am trying to get some dynamic array values into a PowerShell windows Form. When I use the same command on the PowerShell console it works but in form I get a weird .dot net class returned. Below I mark the code that results in wrong output.
Import-Module NetAdapter
$nics = Get-NetAdapter | Select-Object name, interfacedescription,macaddress
function GenerateForm
{
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$frmVMTask = New-Object System.Windows.Forms.Form
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$btnCancel = New-Object System.Windows.Forms.Button
# $btnOK = New-Object System.Windows.Forms.Button
$ChkVMTask = New-Object System.Windows.Forms.CheckedListBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$handler_ChkVMTask_itemCheckChanged =
{
$VMTask = $ChkVMTask.CheckedItems
If ($ChkVMTask.CheckedItems.count -gt 1)
{
$buttons = [system.windows.forms.messageboxbuttons]::ok;
[System.Windows.Forms.MessageBoxDefaultButton]::Button1;
$icon = [system.windows.forms.messageboxicon]::error;
$tasks = [System.Windows.Forms.MessageBox]::Show("
############################
# Too Many VM Tasks are selected. #
#############################`n
Please Select 1 item only.
"
, "VM Task Error Summary", $buttons, $icon)
}
else
{
$VMTask = $ChkVMTask.CheckedItems
}
}
{
# Close the form if Cancel clicked
$frmVMTask.close()
}
$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 16
$System_Drawing_Point.Y = 16
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 940
$System_Drawing_Size.Width = 800
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 0
$groupBox1.TabStop = $False
$groupBox1.Text = "NIC Automation Task"
$groupBox1.add_Enter($handler_groupBox1_Enter)
$frmVMTask.Controls.Add($groupBox1)
$btnCancel.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 280
$System_Drawing_Point.Y = 430
$btnCancel.Location = $System_Drawing_Point
$btnCancel.Name = "btnCancel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 75
$btnCancel.Size = $System_Drawing_Size
$btnCancel.TabIndex = 15
$btnCancel.Text = "Cancel"
$btnCancel.UseVisualStyleBackColor = $True
$btnCancel.add_Click($handler_btnCancel_Click)
$groupBox1.Controls.Add($btnCancel)
$groupBox1.Controls.Add($lblTask)
$ChkVMTask.[Windows.Forms.SelectionMode]::One;
$ChkVMTask.CheckOnClick = $True;
$ChkVMTask.DataBindings.DefaultDataSourceUpdateMode = 0
$ChkVMTask.FormattingEnabled = $True
foreach ($nic in $nics)
{
Below is where output is code and not same as get-netadapter
$ChkVMTask.Items.Add("$Nic") | Out-Null
}
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 54
$ChkVMTask.Location = $System_Drawing_Point
$ChkVMTask.Name = "ChkVMTask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 158
$System_Drawing_Size.Width = 722
$ChkVMTask.Size = $System_Drawing_Size
$ChkVMTask.TabIndex = 1
$ChkVMTask.Tag = ""
$ChkVMTask.add_SelectedIndexChanged($handler_ChkVMTask_itemCheckChanged)
$groupBox1.Controls.Add($ChkVMTask)
#Save the initial state of the form
$InitialFormWindowState = $frmVMTask.WindowState
#Init the OnLoad event to correct the initial state of the form
$frmVMTask.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$frmVMTask.ShowDialog() | Out-Null
$frmVMTask.close() #new parent test close visible and hidden work perfectly. close not yet.
} #End Function
#Call the Function
GenerateForm

Alternatives to Msgbox in VBScript

I want to show status information after a VBS based login script runs. I could simply use a Messagebox, but would rather have this information appear above the clock briefly after the user logs in.
What alternatives do I have instead of a Messagebox?
What you are referring to is something like a balloon tip. There is nothing like this available in VBScript natively. You would need to use a third-party component.
As an aside, PowerShell IS able to do this.
[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"Reboot Required","Please restart your computer",[system.windows.forms.tooltipicon]::None)
If you can go pure powershell I highly recommend this snippet works great!
Example
Code
##################
# Messenger like popup dialog
# Usage:
# New-Popup.ps1
# New-Popup.ps1 -slide -message "hello world" -title "PowerShell Popup"
param(
[int]$formWidth=200,
[int]$formHeight=110,
[string]$title="Your title here",
[string]$message="Your message here",
[int]$wait=4,
[switch]$slide
)
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
################
# extract powershell icon if doesn't exist
$icon = "$env:temp\posh.ico"
if( !(test-path -pathType leaf $icon)){
[System.Drawing.Icon]::ExtractAssociatedIcon((get-process -id $pid).path).ToBitmap().Save($icon)
}
################
# Create the form
$form = new-object System.Windows.Forms.Form
$form.ClientSize = new-object System.Drawing.Size($formWidth,$formHeight)
$form.BackColor = [System.Drawing.Color]::LightBlue
$form.ControlBox = $false
$form.ShowInTaskbar = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.topMost=$true
# initial form position
$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
if($slide){
$top = $screen.WorkingArea.height + $form.height
$left = $screen.WorkingArea.width - $form.width
$form.Location = new-object System.Drawing.Point($left,$top)
} else {
$top = $screen.WorkingArea.height - $form.height
$left = $screen.WorkingArea.width - $form.width
$form.Location = new-object System.Drawing.Point($left,$top)
}
################
# pictureBox for icon
$pictureBox = new-object System.Windows.Forms.PictureBox
$pictureBox.Location = new-object System.Drawing.Point(2,2)
$pictureBox.Size = new-object System.Drawing.Size(20,20)
$pictureBox.TabStop = $false
$pictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$pictureBox.Load($icon)
################
# create textbox to display the message
$textbox = new-object System.Windows.Forms.TextBox
$textbox.Text = $message
$textbox.BackColor = $form.BackColor
$textbox.Location = new-object System.Drawing.Point(4,26)
$textbox.Multiline = $true
$textbox.TabStop = $false
$textbox.BorderStyle = [System.Windows.Forms.BorderStyle]::None
$textbox.Size = new-object System.Drawing.Size(192,77)
$textbox.Cursor = [System.Windows.Forms.Cursors]::Default
$textbox.HideSelection = $false
################
# Create 'Close' button, when clicked hide and dispose the form
$button = new-object system.windows.forms.button
$button.Font = new-object System.Drawing.Font("Webdings",5)
$button.Location = new-object System.Drawing.Point(182,3)
$button.Size = new-object System.Drawing.Size(16,16)
$button.Text = [char]114
$button.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$button.Add_Click({ $form.hide(); $form.dispose() })
if($slide) {$button.visible=$false}
################
# Create a label, for title text
$label = new-object System.Windows.Forms.Label
$label.Font = new-object System.Drawing.Font("Microsoft Sans Serif",8,[System.Drawing.FontStyle]::Bold)
$label.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
$label.Text = $title
$label.Location = new-object System.Drawing.Point(24,3)
$label.Size = new-object System.Drawing.Size(174, 20)
################
# Create a timer to slide the form
$timer = new-object System.Windows.Forms.Timer
$timer.Enabled=$false
$timer.Interval=10
$timer.tag="up"
$timer.add_tick({
if(!$slide){return}
if($timer.tag -eq "up"){
$timer.enabled=$true
$form.top-=2
if($form.top -le ($screen.WorkingArea.height - $form.height)){
#$timer.enabled=$false
$timer.tag="down"
start-sleep $wait
}
} else {
$form.top+=2
if($form.top -eq ($screen.WorkingArea.height + $form.height)){
$timer.enabled=$false
$form.dispose()
}
}
})
# add form event handlers
$form.add_shown({
$form.Activate()
(new-Object System.Media.SoundPlayer "$env:windir\Media\notify.wav").play()
$timer.enabled=$true
})
# draw seperator line
$form.add_paint({
$gfx = $form.CreateGraphics()
$pen = new-object System.Drawing.Pen([System.Drawing.Color]::Black)
$gfx.drawLine($pen,0,24,$form.width,24)
$pen.dispose()
$gfx.dispose()
})
################
# add controls to the form
# hide close button if form is not sliding
if($slide){
$form.Controls.AddRange(#($label,$textbox,$pictureBox))
} else {
$form.Controls.AddRange(#($button,$label,$textbox,$pictureBox))
}
################
# show the form
[void]$form.showdialog()
save as New-Popup.ps1 to keep the usage the same. Kudo's to the author of http://scriptolog.blogspot.com/2008/02/windows-messenger-like-popup.html

Resources