Archive

Archive for the ‘PS Scripts’ Category

PowerShell Script To Send Message To Network Computer(s)

August 7, 2012 30 comments

Some time back, we had a requirement of sending a text message to all the client computers to inform about some activity.

I have created a little script which can send a POP UP MESSAGE to the computer(s), and it will simply prompt for input, and at the time of finish it will show you all the statistics.

The script can accept a single computer name or multiple computer names separated with comma (,) or path of txt file which is containing the list of computers.

When you run this script it will ask you for below Input.

Message :- Type your message which you want to send to the computers. Like Hi, “Hello, Update are being installed on your computer, pleas wait” Etc.

Computer Name: – Type the computer name to which you want to send the message, if you type PC1, it will send message to PC1 computer.

You can type multiple computer names separated by the comma “,”. PC1, PC2, PC3 or you can give the path of the txt file which is containing the computer names in below manner

PC1

PC2

PC3

Time :- Type time in SECONDS, till message pop up will remain on computer. After this time, the message pop up will disappear.

Like if you provide 5 in time prompt, the pop up will be remain till 5 seconds on user’s desktop.

===========================================================

You are free to Copy, Modify and use this script,

I welcome your suggestions and views to improve this script code.

============================================================

Copy the below code and save this as “.PS1” extension

———————————————————————————————————————————-   

<# Script to send message to Client computers using MSG utility

It will prompt you to enter the following information.

Message :-  type your message which you want to send to the computers. Like Hi, Hello etc.

Computer Name :- Type the computer name to which you want to send the Message, if you type PC1, it will send message to PC1 computer.

You can type multiple computer names separated by the comma “,”. PC1, PC2, PC3 or you can give the path of the txt file which is containing the computer names in below manner

PC1

PC2

PC3

Time :- Type time in SECONDS,till message pop up will remain on computer. After this time, the message pop up will disappear.

Like if you provide 5 in time prompt, the pop up will be remain till 5 seconds on user’s desktop.

#>

# Variable declaration

$Start_Time       =              Get-Date -Format T

$logFile          =                    ‘Not_Reachable_PCs.txt’

$Message          =               Read-Host -Prompt “Type Your Message Here”      

$ComputerName     =       Read-Host -Prompt “Type Computer Name Here”    

$Time             =       Read-Host -Prompt “Type Time Here” 

$Session          =       “*”

$ComputerName     =       $ComputerName -split ‘,’

if ($ComputerName -match “:”)

                      {

                      $Path = $ComputerName

                      $ComputerName = Get-Content $path

          }

                      $Total = $ComputerName.count 

                                foreach ($Computer in $ComputerName )

                                                {

                                                                if (Test-Connection -ComputerName $Computer -Count 1 -ErrorAction 0)

                                {

                                                                Write-Host “Sending Message to $Computer…….” -ForegroundColor yellow

                                msg $Session /Server:$Computer /Time:$Time $Message

                                                                Write-Host “Message Successfully Sent to $Computer” -ForegroundColor Green

                                                                }

                                                                else

                                                                                {

                                                                Out-File -FilePath $logFile -InputObject $Computer -Append -Force

                                                                                                Write-Host “$Computer is not Reachable…” -ForegroundColor red

                                                                                }

                                                }

                                $Not_Reachable_Count  = @(Get-Content $logFile).count

        $End_Time   =    Get-Date -Format T

        $Minute = (New-TimeSpan -Start $Start_Time -End $End_Time).Minutes

        $Second = (New-TimeSpan -Start $Start_Time -End $End_Time).Seconds

                                Write-Host Start at $Start_Time, End At $End_Time, Took About $Minute Minutes $seconds Seconds

                                Write-Host “Total $Total Computer Processed, $Not_Reachable_Count computers were offline. The list is stored in $logFile” -ForegroundColor white

—————————————————–

Advertisements
Categories: PS Scripts
Noam's scripting blog

"Ready to use" scripts and scripting tips for system admins with detailed walkthroughs/explanations.