Multiple commands on the same command window?

  • Thread starter Thread starter Ty Moffett
  • Start date Start date
T

Ty Moffett

I have a simple logon script that runs a few 'netsh' commands and a 'net
time' command. The problem (albiet small) is that they each fire off their
own command window and it's a little annoying.

Can they all be run in the same window? The script is below.

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_OperatingSystem",,48)
If objItem.Caption = "Microsoft Windows XP Professional" Then
set WshShell = CreateObject("WScript.Shell")
'Enable TCP port 135 (which is the port that WMI uses).
WshShell.Run "netsh firewall add portopening TCP 135 RemoteAdministration
ENABLE ALL"
'Enable the "File and Printer Sharing" ports on the firewall.
WshShell.Run "netsh firewall set service FILEANDPRINT ENABLE ALL"
'Enable the "Remote Desktop" ports on the firewall.
WshShell.Run "netsh firewall set service REMOTEDESKTOP ENABLE ALL"
'Registry hack that enables remote desktop.
WshShell.Run "regedit /s remotedesktopenable.reg"
End If

'Sync the time.
WshShell.Run "net time \\aristotle /set /yes"
 
Ty said:
I have a simple logon script that runs a few 'netsh' commands and a
'net time' command. The problem (albiet small) is that they each
fire off their own command window and it's a little annoying.

Can they all be run in the same window? The script is below.
[snip]
Hi

Hide the command prompt by setting the Run method's second parameter
(intWindowStyle ) to 0. You should also set the third parameter
(bWaitOnReturn) to True to let the script wait for the command to
finish before continuing (to avoid any "collisions" for e.g. the netsh
commands).

Here is an adjusted version of your script (see inline comments for
several other adjustments):

'--------------------8<----------------------

' moved this one outside the If test for WinXP, if not, the
' "net time" command will not run for non-WinXP computers
' (because no WshShell would exist in that case and you would
' not see the error because of the "On Error Resume Next" line).
Set WshShell = CreateObject("WScript.Shell")

' Should not be necessary with an "On Error Resume Next" statement
' here. Nothing in the script below should err unless some very
' basic OS functionality was corrupt.
'On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

' Selecting only the Caption property from Win32_OperatingSystem
Set colItems = _
objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem")

If objItem.Caption = "Microsoft Windows XP Professional" Then

'Enable TCP port 135 (which is the port that WMI uses).
WshShell.Run "netsh firewall add portopening TCP 135" _
& " RemoteAdministration ENABLE ALL", 0, True
'Enable the "File and Printer Sharing" ports on the firewall.
WshShell.Run "netsh firewall set service FILEANDPRINT ENABLE ALL", 0, True
'Enable the "Remote Desktop" ports on the firewall.
WshShell.Run "netsh firewall set service REMOTEDESKTOP ENABLE ALL", 0, True
'Registry hack that enables remote desktop.
WshShell.Run "regedit /s remotedesktopenable.reg", 0, True
End If

'Sync the time.
WshShell.Run "net time \\aristotle /set /yes", 0, True

'--------------------8<----------------------


WSH 5.6 documentation (local help file) can be downloaded from here
if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp
 
Awesome, thanks a lot.


Torgeir Bakken (MVP) said:
Ty said:
I have a simple logon script that runs a few 'netsh' commands and a
'net time' command. The problem (albiet small) is that they each
fire off their own command window and it's a little annoying.

Can they all be run in the same window? The script is below.
[snip]
Hi

Hide the command prompt by setting the Run method's second parameter
(intWindowStyle ) to 0. You should also set the third parameter
(bWaitOnReturn) to True to let the script wait for the command to
finish before continuing (to avoid any "collisions" for e.g. the netsh
commands).

Here is an adjusted version of your script (see inline comments for
several other adjustments):

'--------------------8<----------------------

' moved this one outside the If test for WinXP, if not, the
' "net time" command will not run for non-WinXP computers
' (because no WshShell would exist in that case and you would
' not see the error because of the "On Error Resume Next" line).
Set WshShell = CreateObject("WScript.Shell")

' Should not be necessary with an "On Error Resume Next" statement
' here. Nothing in the script below should err unless some very
' basic OS functionality was corrupt.
'On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

' Selecting only the Caption property from Win32_OperatingSystem
Set colItems = _
objWMIService.ExecQuery("Select Caption from Win32_OperatingSystem")

If objItem.Caption = "Microsoft Windows XP Professional" Then

'Enable TCP port 135 (which is the port that WMI uses).
WshShell.Run "netsh firewall add portopening TCP 135" _
& " RemoteAdministration ENABLE ALL", 0, True
'Enable the "File and Printer Sharing" ports on the firewall.
WshShell.Run "netsh firewall set service FILEANDPRINT ENABLE ALL", 0,
True
'Enable the "Remote Desktop" ports on the firewall.
WshShell.Run "netsh firewall set service REMOTEDESKTOP ENABLE ALL", 0,
True
'Registry hack that enables remote desktop.
WshShell.Run "regedit /s remotedesktopenable.reg", 0, True
End If

'Sync the time.
WshShell.Run "net time \\aristotle /set /yes", 0, True

'--------------------8<----------------------


WSH 5.6 documentation (local help file) can be downloaded from here
if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
 
Back
Top