HI all,
I trying to have a powershell script message appear on certain users' machines on the network to alert them of a shutdown by looping through a CSV file of machine names. I have the powershell script created to display a suitable message with a Warning icon and found to format the popup in a certain way I had to use a "FORM" and not a default popup messagebox. I also found I had to use a picture box to display the warning Icon as the code below would not work
$WarningIcon = New-Object ([System.Windows.MessageBoxImage]::Warning)
$Form.Controls.Add($WarningIcon)
I can loop through the CSV of machine names too and trying to send the Form as a "msg" to each user machine but so far it won't work.
What I have tried:
Searched the internet a lot but so far cannot find a way of sending the Form to the list of machine names retrieved from the CSV. So far I have tried setting the form (script below) to the $msg variable and also tried having the form in another file and referencing that file in the $msg variable but it doesn't work. Also tried having the form directly after the "$ID" in the loop below and before the loop closing bracket.
Here is the Powershell loop msg script and Form script where I tried having the form script in a different file that could be referenced. The form shows exactly what I want to display. But as I say nothing I tried so far works. Can this be done please? Thanks in advance
$msg = C:\PShell\Form.txt
$csv = Import-csv "C:\PShell\Machines.csv"
foreach($line in $csv)
{
$ID = $line.("LPID")
#Echo "Laptop ID is $ID"
msg $ID $msg
}
#FORM Script
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Width = 700
$Form.Height = 400
$Form.BackColor = "#DCDCDC"
$Form.Text = "System Restart Alert"
$Font = New-Object System.Drawing.Font("Ariel",30,
[System.Drawing.FontStyle]::Bold::Underline)
$FontB = New-Object System.Drawing.Font("Ariel",14,
[System.Drawing.FontStyle]::Bold)
$Picture = (get-item ("C:\PShell\Exclamation.jpg"))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-object System.Drawing.Size(160,30)
$pictureBox.Height = "100"
$pictureBox.Image = $img
$Form.controls.add($pictureBox)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = "260,30"
$Label.Font = $Font
$Label.ForeColor = "Red"
$Label.Text = "WARNING!"
$Label.AutoSize = $True
$Form.Controls.Add($Label)
$LabelB = New-Object System.Windows.Forms.Label
$LabelB.Location = "100,130"
$LabelB.Font = $FontB
$LabelB.Text = "Due to essential maintenance system requires rebooting"
$LabelB.AutoSize = $True
$Form.Controls.Add($LabelB)
$LabelC = New-Object System.Windows.Forms.Label
$LabelC.Location = "100,160"
$LabelC.Font = $FontB
$LabelC.Text = "Please save all work immediately"
$LabelC.AutoSize = $True
$Form.Controls.Add($LabelC)
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = "300,280"
$okButton.Font = "$FontB"
$okButton.Size = "85,28"
$okButton.Text = "Okay"
$Form.Controls.Add($okButton)
$okButton.Add_Click = ({$Form.Close()})
$Form.ShowDialog()