Click here to Skip to main content
15,797,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)

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, 
$FontB = New-Object System.Drawing.Font("Ariel",14, 

$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

$Label = New-Object System.Windows.Forms.Label
$Label.Location = "260,30"
$Label.Font = $Font
$Label.ForeColor = "Red"
$Label.Text = "WARNING!"
$Label.AutoSize = $True

$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

$LabelC = New-Object System.Windows.Forms.Label
$LabelC.Location = "100,160"
$LabelC.Font = $FontB
$LabelC.Text = "Please save all work immediately"
$LabelC.AutoSize = $True

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = "300,280"
$okButton.Font = "$FontB"
$okButton.Size = "85,28"
$okButton.Text = "Okay"

$okButton.Add_Click = ({$Form.Close()})
Updated 30-Jan-19 3:38am

1 solution can't send a Form to a users machine and have it appear on their Desktop. This won't work at all. You can't even send an image of the form.
Share this answer
jar8695 30-Jan-19 12:36pm    
Okay. Thanks. Is it possible to format a popup message then. I know I can create one of those with a warning exclamation triangle but can I increase the size of the pop up and text please?
Dave Kreskowiak 30-Jan-19 13:05pm    
Unless you write an app and install it on every machine, you have no way of displaying a message on remote machines. This app would run out of the registry Run key upon any user that logs in. It would then start a server and listen for messages from a central app that you use. You could then send any message you want and have the client app display it any way you want.

If you think about this, you'll figure out that remotely interacting with a users Desktop is a HUGE security risk.
jar8695 31-Jan-19 4:44am    
Well! I suppose. Probably best to think of something else. Thanks anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900