Link to home
Create AccountLog in
Avatar of Nathan Kaufman
Nathan KaufmanFlag for United States of America

asked on

Batch job to run now on every machine without reboot

I have a  batch file that runs at startup via Group Policy to disable NETBIOS.  Once a server or desktop in our domain reboots the script runs fine.  However we can't reboot every server or workstation for various reasons. I was wondering if there is a way to run the batch job now on every machine without having to reboot?


wmic nicconfig where (TcpipNetbiosoptions!=Null and TcpipNetbiosoptions!=2) call SetTcpipNetbios 2

Open in new window

Avatar of Gabriel Clifton
Gabriel Clifton
Flag of United States of America image

Try this VBScript to do it remotely:
On Error Resume Next
intArgIter = 0

If WScript.Arguments.Count = 0 Then
	WScript.Echo "NETConfig [\\Computer][Show|SetON|SetOFF]"
Else
	strComputerMAIN = "Non Inizializzata"
	Do While intArgIter <= WScript.Arguments.Count - 1
		strUserGivenArg = WScript.Arguments.Item(intArgIter)
		If LCase(Left(strUserGivenArg,2))="\\" Then
			strComputerMAIN = Mid(strUserGivenArg,3)
		End If
		intArgIter = intArgIter + 1
	Loop
	If strComputerMAIN = "Non Inizializzata" Then
		strComputerMAIN = "."
	End If
	intArgIter = 0
	Do While intArgIter <= WScript.Arguments.Count - 1
		strUserGivenArg = WScript.Arguments.Item(intArgIter)
		Select Case LCase(strUserGivenArg)
			Case LCase("SHOW")
				CALL SUBnetshow (strComputerMAIN)
			Case LCase("SETON")
				CALL SUBnetset (strComputerMAIN,0)
			Case LCase("SETOFF")
				CALL SUBnetset (strComputerMAIN,2)
		End Select
	intArgIter = intArgIter + 1
	Loop
End If
WSCript.Quit

Sub SUBnetshow (strComputer)

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objNicConfig In colNicConfigs
  strDNSHostName = objNicConfig.DNSHostName
  intNetBIOS = objNicConfig.TcpipNetbiosOptions
  Select Case intNetBIOS
    Case 0 strNetBIOS = "Use NetBIOS setting from the DHCP server."
    Case 1 strNetBIOS = "Enable NetBIOS over TCP/IP."
    Case 2 strNetBIOS = "Disable NetBIOS over TCP/IP."
    Case Else strNetBIOS = "Unable to determine setting for NetBIOS " & _
     "Over TCP/IP."
  End Select
  strWINSSettings = strWINSSettings & VbCrLf & VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description & VbCrLf & VbCrLf & _
   "    NetBIOS: " & strNetBIOS & VbCrLf & _
   "    WINS Primary Server:             " & _
   objNicConfig.WINSPrimaryServer & VbCrLf & _
   "    WINS Secondary Server:           " & _
   objNicConfig.WINSSecondaryServer & VbCrLf & _
   "    WINS Scope ID:                   " & _
   objNicConfig.WINSScopeID & VbCrLf & _
   "    WINS Enable LMHosts Lookup:      " & _
   objNicConfig.WINSEnableLMHostsLookup & VbCrLf & _
   "    WINS Host Lookup File:           " & _
   objNicConfig.WINSHostLookupFile & VbCrLf & _
   "    DNS Enabled For WINS Resolution: " & _
   objNicConfig.DNSEnabledForWINSResolution
Next

WScript.Echo VbCrLf & "WINS Settings" & VbCrLf & VbCrLf & _
 "Host Name: " & strDNSHostName & strWINSSettings

End SUB

Sub SUBnetset (strComputer,strOpt)

Const ENABLE_NETBIOS_VIA_DHCP = 0
Const ENABLE_NETBIOS = 1
Const DISABLE_NETBIOS = 2

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

WScript.Echo VbCrLf & "Host Name: " & strComputer
For Each objNicConfig in colNicConfigs
  WScript.Echo VbCrLf & "  Network Adapter " & objNicConfig.Index & _
   "    " & objNicConfig.Description & VbCrLf & _
  "    Attempting to set NetBIOS over TCP/IP."
  intNetBIOS = objNicConfig.SetTCPIPNetBIOS(strOpt)
  If intNetBIOS = 0 Then
    WScript.Echo "    Successfully set NetBIOS over TCP/IP"
  ElseIf intNetBIOS = 1 Then
    WScript.Echo "    Successfully set NetBIOS over TCP/IP" & _
     VbCrLf & "    Must reboot."
  Else
    WScript.Echo "    Unable to set NetBIOS."
  End If
Next

WScript.Echo VbCrLf & String(80, "-")

Set colNicConfigs = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objNicConfig In colNicConfigs
  WScript.Echo VbCrLf & _
   "  Network Adapter " & objNicConfig.Index & VbCrLf & _
   "    " & objNicConfig.Description
  intNetBIOS = objNicConfig.TcpipNetbiosOptions
  Select Case intNetBIOS
    Case 0 strNetBIOS = "Use NetBIOS setting from the DHCP server"
    Case 1 strNetBIOS = "Enable NetBIOS over TCP/IP"
    Case 2 strNetBIOS = "Disable NetBIOS over TCP/IP"
  End Select
  WScript.Echo "    NetBIOS Over TCP/IP: " & strNetBIOS
Next

End Sub

Open in new window

Avatar of Don
Avatar of Nathan Kaufman

ASKER

I am using Group Policy and that part works when a computer is rebooted.  What I want is a way to force the batch script to run so computers don't have to be rebooted, thanks.
ASKER CERTIFIED SOLUTION
Avatar of mlongoh
mlongoh
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Here's a script that allows you to remotely execute something.
On Error Resume Next
Dim wsShell, objNetwork, objFSO
Set wsShell = CreateObject("WScript.Shell")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objArgs = WScript.Arguments
Select Case objArgs.Count
    Case 2
		cmd = objArgs(1)
		strComputer = objArgs(0)
	Case Else
		wscript.echo "USAGE: cscript RRun.vbs COMPUTERNAME ""Program to run and options""" & vbcrlf & "   COMPUTERNAME = The workstation to run the program on" & vbcrlf & "   Program to run = The command and options to run" & vbcrlf & "   ie:CSCRIPT RRUN.VBS W01234 ""AVSCAN.EXE C:\"""
		wscript.quit
End Select

If InStr(cmd, "#") > 0 Then cmd = Replace(cmd,"#", """")
wscript.echo strComputer & " " & cmd

LaunchProcess(cmd)

Sub LaunchProcess(sCmdLine)
	Const HIDDEN_WINDOW = 0
	Set objRemWMIService = GetObject("winmgmts:" _
                        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set objStartup = objRemWMIService.Get("Win32_ProcessStartup")
	Set objConfig = objStartup.SpawnInstance_
	objConfig.ShowWindow = 1
	Set objProcess = objRemWMIService.Get("Win32_Process")
	intReturn = objProcess.Create(sCmdLine, Null, objConfig, intProcessID)
	If intReturn <> 0 Then
		Wscript.Echo "Process could not be created." & vbNewLine & "Command line: " & sCmdLine & vbNewLine & "Return value: " & intReturn
	Else
		Wscript.Echo "Process created." & vbNewLine & "Command line: " & sCmdLine & vbNewLine & "Process ID: " & intProcessID
	End If
	Set objProcess = nothing
	Set objConfig = nothing
	Set objStartup = nothing
	Set objRemWMIService = nothing
End Sub

Open in new window

Thanks everyone for your help I was able to get it to work with mlongoh's advice of PSEXEC.  Below is the command line used.  I left out the name of the server and just replaced it with my server name.  One thing that was weird is it would not enumerate the domain with NETBIOS disabled when running the psexec.exe command....  I couldn't reboot or flushdns on the server so that could be it too, but just throwing that out there.  Again thanks for your help!

psexec.exe -d "\\*" -c \\server\Share\Disable_NETBIOS.bat

Open in new window