Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

net send vbscript

Status
Not open for further replies.

mat305

Technical User
Sep 11, 2009
4
US
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
 
Having a sleepless night and this looked interesting so I thought I would create an alternative with a little more functionality.

Just went to test it on my home domain and discovered that the messenger service is depricated on Windows Vista and Windows 7. So I guess this isn't that helpful.

Code:
'==========================================================================
'
' NAME: NetSend.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 9/12/2009
' COPYRIGHT © 2009, All Rights Reserved
'
' COMMENT: 
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================


'First verify the Messenger service is running locally
Set WSHNetwork = CreateObject("Wscript.Network")
localHost = WSHNetwork.ComputerName
EnableMessenger(localHost)

'Enter the message to send
Message = InputBox("Enter message to send.""Message to send.")
Report = Message & vbCrLf & "Was sent to:" & vbCrLf

'Get computer name(s) to send message to.
strComputer = InputBox("Enter computer name to send message to." & vbCrLf & _
	"Seperate multiple computer names with commas or enter " & Chr(34) & "ALL" & Chr(34) & _
	" to send to all domain computers.", "Enter message target.")

If InStr(strComputer,",") > 0 Then
	'Multiple targets, create an array.
	strComputerArray = Array(strComputer)
	'Now Loop through the array
	For Each PC In strComputerArray
		'Call the function to send the message and manage the service
		SendMessage PC, Message
		Report = Report & PC & vbCrLf		
	Next
Else	
	If UCase(strComputer) = "ALL" Then
		'Enumerate domain computers
		Const ADS_SCOPE_SUBTREE = 2
		Set objConnection = CreateObject("ADODB.Connection")
		Set objCommand =   CreateObject("ADODB.Command")
		objConnection.Provider = "ADsDSOObject"
		objConnection.Open "Active Directory Provider"
		
		Set objDomain = getObject("LDAP://rootDse")
		Domain = objDomain.Get("defaultNamingContext")
		LDPATH = Chr(39) & "LDAP://" & Domain & Chr(39)
	
		Set objCommand.ActiveConnection = objConnection
		objCommand.CommandText = _
		    "Select Name, Location from " & LDPATH _
		        & "where objectClass='computer'"  
		objCommand.Properties("Page Size") = 1000
		objCommand.Properties("Timeout") = 30 
		objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
		objCommand.Properties("Cache Results") = False 
		Set objRecordSet = objCommand.Execute
		objRecordSet.MoveFirst
		Do Until objRecordSet.EOF
			'Call the function to send the message and manage the service
		    SendMessage objRecordSet.Fields("Name").Value, Message
		    Report = Report & objRecordSet.Fields("Name").Value & vbCrLf
		    objRecordSet.MoveNext
		Loop
	Else
		SendMessage strComputer, Message
		Report = Report & strComputer & vbCrLf
	End If
End If
'Show the report
Set WSHShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Const ForWriting = 2
If fso.FileExists("NetSendReport.txt") Then
	fso.DeleteFile "NetSendReport.txt"
End If
Set ts = fso.CreateTextFile ("NetSendReport.txt", ForWriting)
ts.write Report
ts.Close
WSHShell.Run("NetSendReport.txt")
'MsgBox Report
MsgBox "Done"

Function SendMessage(strTarget,Message)
	'Create the Shell object
	Set WSHShell = CreateObject("Wscript.Shell")
	
	'Enable messenger service on target 
	EnableMessenger(strTarget)

	'Now send the message
	WSHShell.Run "net send " & strTarget & " " & Message
    WScript.sleep(3000)

	'Stop and disable the messenger service on the target machine
	DisableMessenger(strTarget)
End Function

Function EnableMessenger(strTarget)
	On Error Resume Next
	'Connect to wmi on target machine.
	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strTarget & "\root\cimv2")
    If Err.Number = 0 Then
		Set colServiceList = objWMIService.ExecQuery _
	    ("Select * from Win32_Service where Name='Messenger'")
		
		'Set the service to manual and start it
		For Each objService in colServiceList
	    	errReturnCode = objService.Change( , , , , "Manual")   
	    	If objService.State <> "Started" Then 
	    		errReturn =  objService.StartService()
	    	End If
		WScript.Sleep(3000)	
		Next
	End If
End Function

Function DisableMessenger(strTarget)
	'Connect to wmi on target machine.
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strTarget & "\root\cimv2")
	Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='Messenger'")
	
	'Set the service to manual and start it
	For Each objService in colServiceList
    	If objService.State <> "Started" Then 
    		errReturn =  objService.StopService()
    	End If
    	errReturnCode = objService.Change( , , , , "Disabled")   
	WScript.Sleep(3000)	
	Next
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top