Powershell to Whitelist IP in IIS? - user-interface

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. :)

Related

How to display a picture inside a DataGridView on Powershell ISE

I've been struggling to display either OK.png / NOK.png on a specific column by testing if the path of the actual PV.pdf exists ,
$dataGrid = New-Object System.Windows.Forms.DataGridView
$dataGrid.Width = 503
$dataGrid.Height = 250
$dataGrid.location = new-object system.drawing.point(120,380)
$dataGrid.DataSource = $DataTable
$dataGrid.ReadOnly = $true
$dataGrid.RowHeadersVisible = $false
$dataGrid.AllowUserToAddRows = $false
$dataGrid.AutoSizeColumnsMode = 'Fill'
$ImageColumn = New-Object System.Windows.Forms.DataGridViewImageColumn
$ImageColumn.Width = 40
$dataGrid.Columns.Insert($dataGrid.Columns.Count, $ImageColumn)
$dataGrid.Columns[$dataGrid.Columns.Count - 1].HeaderText = "PV"
$dataGrid.Columns[$dataGrid.Columns.Count - 1].Name = "ImageColumn"
$OKImage = [System.Drawing.Image]::FromFile([string]$curDir+"\images\OK.png")
$NOKImage = [System.Drawing.Image]::FromFile([string]$curDir+"\images\NOK.png")
for($i=0;$i -lt $dataGrid.RowCount;$i++) {
Write-Host $dataGrid.Rows[$i].Cells[1].Value
$dir = [string]$curDir+"\pvs\"+[string]$dataGrid.Rows[$i].Cells[1].Value+".pdf"
if ((Test-Path $dir) -eq $true) {
$dataGrid.Rows[$i].Cells[0].Value = $OKImage
} else {
$dataGrid.Rows[$i].Cells[0].Value = $NOKImage
}
}
$dataGrid.Refresh()
i get this when i run the script :
Screenshot
Please help
i was expecting to display the OK.png (20x20) when the file is found and NOK.png when the file is not found .

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

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
}
}

Select folder and write in a textbox the output powershell

I have created an application in powershell, and now i need to add the GUI to the app.
In this application I need to give the user the opportunity to select or write the path of a folder in a textbok. I have written this piece of code but i haven't achieved to get the output of calling the get-FolderLocation function in the texbox that i create.
Any ideas about how to achieve this?
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = "select the folder";
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
$textBox1.Text = "selected folder"
#############define select button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Browse”;
############# the output of calling the get-Folder Location function must be shown in the textbox1
$button.Add_Click({get-Folderlocation}) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textBox1);
$form.ShowDialog();
$textBox1.Text
$selectedDirectory
function get-Folderlocation([string]$Message, [string]$InitialDirectory, [switch]$NoNewFolderButton)
{
$browseForFolderOptions = 0
if ($NoNewFolderButton) { $browseForFolderOptions += 512 }
$app = New-Object -ComObject Shell.Application
$folder = $app.BrowseForFolder(0, $Message, $browseForFolderOptions, $InitialDirectory)
if ($folder) { $selectedDirectory = $folder.Self.Path } else { $selectedDirectory = '' }
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($app) > $null
return $selectedDirectory
}
You simply assign the output from get-FolderLocation to $textBox1.Text.
Since the $textBox1 variable is not in the same scope as the add_Click() scriptblock, PowerShell 3.0 and 4.0 will have issues resolving it. Use Get-Variable -Scope 1 to work around this:
$button.Add_Click({(Get-Variable -Name textBox1 -Scope 1).Value.Text = Get-Folderlocation})

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