Since NetSend is gone in Windows 7 we (like many others) still need to send broadcast messages to our environment. I found the code below which works. It allows me to send a MSG.EXE message to machines. But the issue is I need to run test-connect first since I only want to send this Function to online machines.
I know there are smart people on this forum and what I need help with is this: if I want to send a House-Wide message I would of course get a list of machines from AD. The variable below called$mypc would run Get-ADComputer etc...etc... so now lets assume $pc has 2,000 workstation names within it.
How do I run test-connect against $pc but not one-at-a-time? I want to ping blocks of machines and if they are online how would I then call this function below?
My goal is to send a message to every online Windows 7 machines within 90 seconds.
Function Send-NetMessage {
Param(
[Parameter(Mandatory=$True)]
[String]$Message,
[String]$Session="*",
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias("Name")]
[String[]]$Computername=$env:computername,
[Int]$Seconds="5",
[Switch]$VerboseMsg,
[Switch]$Wait
)
Begin
{
Write-Verbose "Sending the following message to computers with a $Seconds second delay: $Message"
}
Process
{
ForEach ($Computer in $ComputerName)
{
Write-Verbose "Processing $Computer"
$cmd = "msg.exe $Session /Time:$($Seconds)"
if ($Computername){$cmd += " /SERVER:$($Computer)"}
if ($VerboseMsg){$cmd += " /V"}
if ($Wait){$cmd += " /W"}
$cmd += " $($Message)"
Invoke-Expression $cmd
}
}
End
{
Write-Verbose "Message sent."
}
}
$msg1 = Read-Host "enter a message"
$mypc = @("mg2014","cclab2","MG11751","mg10462","mg11768","mg11786","mg11741","mg13244","mg13434","mg14464", "mg10257")
$mypc | Send-NetMessage $msg1 -Seconds 30 -VerboseMsg
mqh7