AJF
AJF

Reputation: 1921

Powershell popup to list of usernames

I wish to have a powershell script that provides a popup message to a specific list of users. I need to use a list of relevant users in a spreadsheet with their network username and not use Active Directory. I have the powershell script 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. Although the following two lines for the Warning icon would not work in the form so had to use a picture box

$WarningIcon = New-Object ([System.Windows.MessageBoxImage]::Warning)
$Form.Controls.Add($WarningIcon)

The script for the form is as follows. There is probably a cleaner way of creating a form like this but I am quite new to Powershell!

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:\FA_Files\Windows_Warning_Exclamation9.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()

I can retrieve a list of network usernames from the spreadsheet okay. Below is an image of the csv content

enter image description here

But I have googled a lot and cannot find a way of sending the popup to the list of usernames retrieved from the spreadsheet. So far I have tried the following where I have the form above set to the $msg varaiable and also tried having the form in another file and referencing that file in the $msg variable but it doesn't work

$csv = Import-csv "C:\FA_Files\NetNames.csv"

foreach($line in $csv)
{
  $name = $line.("Name")
  $netName = $line.("NetworkName")
  #Echo "Name is $name and Network Name is $netName"
  msg $netName $msg
}

It also has to be on the username and not the machine name.

How do I correct this please?

Upvotes: 0

Views: 1611

Answers (2)

supermerio
supermerio

Reputation: 240

All users that are a member of Active Directory by default receive read rights, this is by design as Active Directory is exactly the kind of system that should be relied on for the kind of functionality you desire... not sure why your ICT manager is so against the idea.

You cannot send a message to a user (with only the username), you must know the machine the user is logged into as well.

The only way I can think to achieve this is to iterate through every computer and query who is logged on and if the user matches your list then send a message to that computer. This would require reading a list of computers from AD or iterating through every available IP address on your local network(s).

Upvotes: 0

FoxDeploy
FoxDeploy

Reputation: 13537

FWIW, msg.exe has replaced net /send in Windows, and is meant for sending messages to users, so I would begin by looking at the syntax for message.exe here. .

It has a /server: switch you can use to send a message to a remote host. So, take your user and computer name list and put them in a CSV file like this:

//MyInputFile.csv
ComputerName,UserName
Laptop01,BillG
Laptop02,StephenO
PC03,WayneH
Desktop04,JimA

You could use a short script like this to achieve your goal (or get you mostly there, at least 😁)

$Msg = "Put your message here"
$SpreadSheet = Import-CSV .\PathTo\MyInputFile.csv
ForEach ($row in $SpreadSheet){
    "Sending message to \\$($row.ComputerName)\$($row.UserName) : $msg"
    msg /server:$($row.ComputerName) $($row.UserName) $msg
}

You'll see an output like this in the console:

Sending message to \\Laptop01\BillG : Put your message here
Sending message to \\Laptop02\StephenO : Put your message here
Sending message to \\PC03\WayneH : Put your message here
Sending message to \\Desktop04\JimA : Put your message here
Sending message to \\localhost\* : Put your message here

Then your lucky user will see the following on their workstation.
enter image description here

If a computer can't be reached, the process will time out after five seconds or so and move on to the next one in the list.

Upvotes: 1

Related Questions