May be useful for some of you guys. Enables net send on remote machine, then sends a message, then disables net send.
I am sure it could be tweaked a little for the loop that waits for the service to start.
'#################################Declare objects#############################################
'#############################################################################################
'Array for PCnames input by person sending message
Dim arrPCnames()
'Array for service to check for
ArrServices = Array("Messenger")
'Variable to increment through the array of PC names
intSize = 0
'Variable to increment through the array of services
i = 0
'Setting up shell access
Set objShell = Wscript.CreateObject("WScript.Shell")
Set sh = CreateObject("Shell.Application")
'##############################################################################################
'#################################End declare objects##########################################
'Get the computer names, seperated by ',' from the person sending the message.
PC2Reach = InputBox("Please enter the computer name. For additional computers, separate each with a comma (,)")
'This is the message that will be sent out to the workstation
Message2Send = "Your mesage"
'Builds array from the PC names that were input, seperates by ','
arrStations = Split(PC2Reach, ",")
'Send message to each PC in the array
For Each PC2Reach In arrStations
'Changes messenger service to auto
objShell.Run "sc \\" & PC2Reach & " config messenger start= auto"
'Waits 3 seconds
wscript.sleep(3000)
'Turns on messenger service
objShell.Run "sc \\" & PC2Reach & " start messenger"
'For each service, (can check for multiple services, we are only checking for messenger)
For Each Service in ArrServices
'Connect to WMI interface
Set objWMIService = GetObject("winmgmts:\\" & PC2Reach & "\root\cimv2")
'Gather the Service status
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
'For each service
For Each objItem in colItems
'Do while messenger service is running
Do while i <> 30
If objItem.State = "Running" then
'Send the message to the computer
objShell.Run "net send " & PC2Reach & " " & Message2Send
'Wait 30 seconds
wscript.sleep(30000)
'Stop the messenger service
objShell.Run "sc \\" & PC2Reach & " stop messenger"
'Wait 5 seconds
wscript.sleep(5000)
'Disable the messenger service
objShell.Run "sc \\" & PC2Reach & " config messenger start= disabled"
End if
Exit Do
'If the messenger service was not running, it will increment 1
i = i + 1
'Loop 30 times, equivalent to 30 seconds
Loop
'Get the next computer in the list
ReDim Preserve arrPCnames(intSize)
arrPCnames(intSize) = PC2Reach
intSize = intSize + 1
Next
Next
Next
'Displays summary of message sent
For Each strName in arrPCNames
PCNamesList = PCNamesList & vbCrLf & strName
Next
MsgBox "Here is the message you sent:" & vbCrLf & _
vbCrLf & _
Message2Send & vbCrLf & _
vbCrLf & _
"Here are the computers that received the message:" & vbCrLf & _
PCNamesList
I am sure it could be tweaked a little for the loop that waits for the service to start.
'#################################Declare objects#############################################
'#############################################################################################
'Array for PCnames input by person sending message
Dim arrPCnames()
'Array for service to check for
ArrServices = Array("Messenger")
'Variable to increment through the array of PC names
intSize = 0
'Variable to increment through the array of services
i = 0
'Setting up shell access
Set objShell = Wscript.CreateObject("WScript.Shell")
Set sh = CreateObject("Shell.Application")
'##############################################################################################
'#################################End declare objects##########################################
'Get the computer names, seperated by ',' from the person sending the message.
PC2Reach = InputBox("Please enter the computer name. For additional computers, separate each with a comma (,)")
'This is the message that will be sent out to the workstation
Message2Send = "Your mesage"
'Builds array from the PC names that were input, seperates by ','
arrStations = Split(PC2Reach, ",")
'Send message to each PC in the array
For Each PC2Reach In arrStations
'Changes messenger service to auto
objShell.Run "sc \\" & PC2Reach & " config messenger start= auto"
'Waits 3 seconds
wscript.sleep(3000)
'Turns on messenger service
objShell.Run "sc \\" & PC2Reach & " start messenger"
'For each service, (can check for multiple services, we are only checking for messenger)
For Each Service in ArrServices
'Connect to WMI interface
Set objWMIService = GetObject("winmgmts:\\" & PC2Reach & "\root\cimv2")
'Gather the Service status
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
'For each service
For Each objItem in colItems
'Do while messenger service is running
Do while i <> 30
If objItem.State = "Running" then
'Send the message to the computer
objShell.Run "net send " & PC2Reach & " " & Message2Send
'Wait 30 seconds
wscript.sleep(30000)
'Stop the messenger service
objShell.Run "sc \\" & PC2Reach & " stop messenger"
'Wait 5 seconds
wscript.sleep(5000)
'Disable the messenger service
objShell.Run "sc \\" & PC2Reach & " config messenger start= disabled"
End if
Exit Do
'If the messenger service was not running, it will increment 1
i = i + 1
'Loop 30 times, equivalent to 30 seconds
Loop
'Get the next computer in the list
ReDim Preserve arrPCnames(intSize)
arrPCnames(intSize) = PC2Reach
intSize = intSize + 1
Next
Next
Next
'Displays summary of message sent
For Each strName in arrPCNames
PCNamesList = PCNamesList & vbCrLf & strName
Next
MsgBox "Here is the message you sent:" & vbCrLf & _
vbCrLf & _
Message2Send & vbCrLf & _
vbCrLf & _
"Here are the computers that received the message:" & vbCrLf & _
PCNamesList