Bookmark and Share

In this post:

 

Troubleshooting a VBScript That Uses Systems Management Server 2003

Hey, Scripting Guy! Question

Hey, Scripting Guy! I'm beating my head against the wall with a VBScript. My intent is to run this script on all my Windows XP machines (600), using Systems Management Server (SMS) 2003 to determine their uptime in hours since last reboot. I want to append to a single log file using a UNC path and shoot the report to my CIO. If I run the script manually on a machine by double-clicking it, the script appends to the log file and everything is hunky dory. If I deploy the script by using SMS 2003, I get nothing in my log. What am I doing wrong?

-- TS 

Hey, Scripting Guy! Answer

Hello TS,

Microsoft Scripting Guy Ed Wilson here. These kinds of problems are very annoying to troubleshoot because each potential solution takes hours to test as you make a change, update SMS, and then wait for the change to replicate and the script to run. Even in a lab setting, each change will take a long time to test. Because I do not currently have SMS deployed at home, I cannot test your scenario. Here are some random ideas to consider based upon my experience in using SMS in the past:

·         Permissions to the share or rights to write to the log file.

·         File locking issues.

·         Network latency.

I have never had really good luck trying to write to a UNC file share. The VBScript filesystemobject was not designed to do this, and therefore was not optimized for such a transaction. What I have done in the past was to create the log file locally on the machine where the script runs, and then have a separate process that copies it to the central location, and then have a third script that consolidates the separate log files into a single log file. It is a bit cumbersome, but it works much better than attempting to have a single script that is run against 600 different desktop computers write to a single log file. In fact, the idea of writing information locally and then transferring it to a central site is essentially the way SMS works.

You can try the Official Scripting Guys Forum to see if anyone there has any ideas. I am interested in what you find out, so let me know.

 

Can I Prompt a User to Determine Which Network Card to Enable or Disable?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I was just reading your article about switching networks by enabling and disabling the network adapter. This was very good because I have been looking into such a solution. The way I was thinking about writing my script was to first disable both network adapters, and then to enable just the one that is needed. I could not figure out how to disable the network card until I read your article.

At work we have this doctor who has two computers. One is for work, and the other is for him to play with or to use at home whenever the Internet is down. As it stands right now, when he wants to use the Internet, we have to disable his one ethernet card, and then enable the card that is attached to the ADSL line. I would like the script to ask him which network card to enable or to disable. Can this be done?

-- DY 

Hey, Scripting Guy! Answer

Hello DY,

You could easily modify the script listed in the article to prompt for a command-line response. You might want to use the Read-Host cmdlet to perform the prompting. This is seen here:

$nic = Read-Host "Which network card would you like to disable <enet / inet>"
if($nic -eq "enet")
  { "Disabling enet. Enabling inet"}
ElseIF($nic -eq "inet")
  { "Disabling inet. Enabling enet"}
Else
  {"I do not understand $nic. Please type enet or inet"}

When you run the script, a prompt such as the following is seen:

Image of prompt that appears when script is run

 

Working with Domain Admin Privileges and Embedded Passwords

Hey, Scripting Guy! Question

Hey, Scripting Guy! For our help desk to use a script, they are required to have domain admin privileges because they are resetting domain accounts. What I would like to do is to be able to remove the domain admin privileges and use a password (that the help desk does not see) to allow them to reset the accounts. Is this possible?

-- TW 

Hey, Scripting Guy! Answer

Hello TW,

There are several solutions you might want to consider:

1.     Embed the domain admin password in the script.

2.     Prompt for the domain admin password.

3.     Store the domain admin password somewhere in a text file and read in the password from the text file when running the script.

4.     Configure RunAs to work for a VBScript then use RunAs when running the script. Enter credentials when running the script.

5.     Configure a special account that has permission to change user passwords. Use 1-4 above with this account.

6.     Configure a Web service/Web site with permissions to change the password. Grant permission to help desk personnel to use that Web site.

7.     Use the Delegation of Administration Wizard to grant the help desk people the right to change passwords. This is the best solution to your problem. We have a Best Practices Guide for Delegating Active Directory Administration, as well as documentation on TechNet.

 

How Can I List Available WMI Namespaces?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Nice WMI series. So I can do quick WMI queries in Windows PowerShell (hooray), and now I can list the available classes using –list.  But how can I list the available namespaces? Sure, I have Scriptomatic, but since Windows PowerShell does it all, why open another tool if the info is already there?

-- KB 

Hey, Scripting Guy! Answer

Hello KB,

I am glad you enjoyed the WMI series. To see the available WMI namespaces, we need to remember that everything in WMI is a class of some sort. The system classes all begin with a double underscore: __Namespace, __Provider, and so on. We can therefore use WMI to tell us about WMI. This script is very similar to one I wrote in the Microsoft Press book, Microsoft VBScript Step by Step (except that, hey, this command is not really a script is it?):

PS C:\> Get-WmiObject -Class __Namespace -Namespace root | Select-Object -Property name

name
----
subscription
DEFAULT
MicrosoftDfs
CIMV2
Cli
Nap
SECURITY
SmsDm
SecurityCenter2
RSOP
HP
ccm
WMI
directory
Policy
ServiceModel
SecurityCenter
MSAPPS12
Microsoft
aspnet

 

This command can be abbreviated by using aliases to the following query:

PS C:\> gwmi __Namespace -N root | select name

name
----
subscription
DEFAULT
MicrosoftDfs
CIMV2
Cli
Nap
SECURITY
SmsDm
SecurityCenter2
RSOP
HP
ccm
WMI
directory
Policy
ServiceModel
SecurityCenter
MSAPPS12
Microsoft
Aspnet

 

But as they say on late night television: But wait, there’s more:

This is because I know you know there are deeper WMI namespaces. In fact the namespaces are hierarchical like the file system or Active Directory Domain Services. So to find subnamespaces, we need to recurse. The following script is from my upcoming book, Windows PowerShell Best Practices.

RecursiveListingOfWMINameSpaces.ps1

Function Get-WmiNameSpace($namespace, $computer)
{
 Get-WmiObject -class __NameSpace -computer $computer `
 -namespace $namespace -ErrorAction "SilentlyContinue" |
 Foreach-Object `
 -Process `
   {
     $subns = Join-Path -Path $_.__namespace -ChildPath $_.name
     $subns
     $script:i ++
     Get-WmiNameSpace -namespace $subNS -computer $computer
   }
} #end Get-WmiNameSpace

# *** Entry Point ***

$script:i = 0
$namespace = "root"
$computer = "LocalHost"
"Obtaining WMI Namespaces from $computer ..."
Get-WmiNameSpace -namespace $namespace -computer $computer
"There are $script:i namespaces on $computer"

 

How Can I Change Passwords for All Scheduled Tasks on a System?

Hey, Scripting Guy! Question

Hey, Scripting Guy!  I am looking for a script to change passwords for all scheduled tasks on a system. Any suggestions or help?

-- PS 

Hey, Scripting Guy! Answer

Hello PS, Interesting question. On Windows Vista, there is a COM object that works with scheduled tasks. Beginning with Windows XP, there is the schtasks.exe command-line utility that can also work with scheduled tasks. The task scheduler API for Windows Vista and later is documented on MSDN. The schtasks.exe command is also documented on MSDN. You can use either tool to modify an existing scheduled task.

 

How Can I Write and Compile a VBScript File in Visual Studio Express?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How do I write and compile a VBScript file in Visual Studio Express? I have an existing file (saved as a .vbs extension) that I wish to debug in Visual Studio Express. However, I’m not able to debug it or step through the code.

-- JR 

Hey, Scripting Guy! Answer

Hello JR,

You are asking a few questions. Let me see if I can answer them:

1. I have tried to write a VBScript in Visual Studio Express and have not been successful. I have done it in Visual Studio 2005, but that was a really long time ago, and the list of instructions ran several pages in length.

2. You cannot compile a VBScript. There are third-party tools that enable you to make an executable out of the VBScript, but it requires you to have that tool installed on each computer that runs the VBScript. You may want to investigate scripting components. There is also a wizard you can download that automates much of the work.

3. As for debugging VBScript, personally I have had little need to debug a VBScript. I have found that writing well-organized scripts that make use of functions and subroutines makes troubleshooting very easy to do. Most of the time, when troubleshooting a script I get by with using a simple wscript.echo to display the value of certain variables throughout the script. There are third-party script editors that have pretty good debuggers built into them. There is also the script debugger you can download from the Microsoft Download Center. It is really old and has not been updated because VBScript itself is in maintenance mode and is not being updated. Debugging is built into Windows PowerShell and works much better.

 

This brings us to the end of another Quick-Hits Friday. It is also the end of another week on the TechNet Script Center. Thank you for all of the questions sent to scripter@microsoft.com. Join us next week as we answer additional scripting questions.

If you want to know exactly what we will be scripting next week, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you on Monday. Peace!

 

Ed Wilson and Craig Liebendorfer, Scripting Guys