Do you have a scripting-related question or problem? You can send your question or problem to email@example.com.
I want to use the current date in the ddmmyy format as a unique filename for files that I create daily. How do I set up a variable in Windows NT or Windows 95 that stores the current date in this format?
You can use VBScript to construct and format dates to meet your specific needs. Listing 1 demonstrates one possible approach—using a custom function named MyDate—to generate the ddmmyy format. MyDate accepts one argument in the form of a date.
The first line in Listing 1 uses VBScript's Date function to obtain the current system date (e.g., Wednesday, December 1, 1999), passes that date to the MyDate function, and echoes MyDate's return value. MyDate then uses VBScript's Day, Month, and Year functions to separate the day, month, and year and assigns those strings to separate variables (e.g., strDay = 1, strMonth = 12, and strYear = 1999). If you're unfamiliar with the Day, Month, and Year functions, go to http://msdn.microsoft.com/scripting /default.htm?/scripting/vbscript/techinfo/ vbsdocs.htm and select Language and Run-time Reference. From the Function drop-down list, select the appropriate function and click Go.
Next, MyDate manipulates the strings. It uses the Len function to pad the day and month strings with a leading zero if necessary (e.g., changes 1 to 01) and uses the Right function to trim the year string to the last two digits (e.g., changes 1999 to 99). For more information about the Len and Right functions, see Dino Esposito, "Understanding VBScript: Functions to Manipulate Strings," page 8. Finally, MyDate uses the concatenation (&) operator to join the three strings and returns the ddmmyy-formatted date as one string (e.g., 011299).
I'm using Active Directory Service Interfaces (ADSI) with Windows Scripting Host (WSH) on an NT 4.0 system. I'm trying to create a logon script that obtains each user's primary group name. I used the PrimaryGroupID property, but all I obtained was the primary group number. What am I doing wrong?
Unfortunately, you can't obtain a user's primary group name via ADSI. You must use Win32 API to obtain the primary group name.
How can I use WSH to apply security permissions on files and folders on an NTFS partition?
To apply security permissions on NTFS partitions, you can use not only WSH but also ADSI.
WSH. You can use the cacls.exe command-line utility that comes with Windows 2000 (Win2K) and NT. Use the WshShell object's Run method to launch the utility.
ADSI. The ADSI software development kit (SDK) includes a resource kit that contains a few COM components that provide additional functionality that some people might consider missing from the core ADSI distribution. One of those components, ADsSecurity.dll, lets you manage NTFS permissions. To obtain and install the ADsSecurity.dll component, follow these steps:
- Go to http://www.microsoft.com/ntserver/nts/ downloads/other/adsi25/sdk.asp, and follow the download instructions for the Microsoft Active Directory Services Interfaces 2.5 SDK (sdk.zip).
- Unzip the SDK into a new directory called C:\adsisdk.
- Move (or copy) ADsSecurity.dll from the C:\adsisdk\resourcekit directory to the C:\winnt\system32 directory or a common component directory. I moved ADsSecurity.dll to the C:\winnt\system32 directory because the standard ADSI components reside there. However, if you don't want to pollute the system32 directory, you can move the DLL to a common component directory (e.g. C:\components) instead.
- Change to the directory that contains ADsSecurity.dll, and issue the following command to install (i.e., register) the DLL:
C:\> regsvr32 ADsSecurity.dll
You'll receive a message specifying that you successfully registered the DLL. You can now use ADsSecurity.dll to manage NTFS permissions. The SDK documentation includes instructions for using ADsSecurity. The SDK also includes Visual Basic (VB) and VBScript code examples in the C:\adsisdk\resourcekit\adssecurity\file subdirectory.
I'm trying to write a script for several users that belong to a group. In this script, how can I determine whether a user is in that group?
You can use the ADSI IADsGroup::IsMember method to determine whether a user is a member of a group. The code in Listing 2 shows how you can test whether the specified user, Jane, is a member of the Domain Users group in the LAB domain.
Listing 2 begins by creating a reference to the target group. After you have a reference to the target group, you can invoke the IsMember method and pass it the ADsPath of the user account you're checking for membership in the group. IsMember returns True if the user is a group member and False if the user isn't a group member. If the return value is True, WScript object's Echo method displays the message Jane is a member. Otherwise, Echo displays the message Jane is NOT a member.
You can learn more about the IADsGroup interface and using ADSI to manage groups in the ADSI SDK. In addition, you can read Alistair G. Lowe-Norris' article "An ADSI Primer Part 2: Manipulating Active Directory Objects," February 1999.
I've been using Win32 API modules in Perl scripts to call Win32 API functions. For example, I have a script that uses Aldo Calpini's Win32::API module to call the ExitWindowsEx Win32 API function to log off the current user. Can you call Win32 API functions from WSH-VBScript files?
WSH doesn't let you make direct calls to the Win32 API. However, WSH lets you access system data and services that were once only accessible via the Win32 API. WSH accomplishes this accessibility through OLE automation. Microsoft is now exposing many system services via COM interfaces, which means WSH can facilitate a wide range of scripting tasks that were quite challenging in the past.
To give you an idea of the system services Microsoft is exposing, let's use your ExitWindowsEx example. You might be surprised to learn that you no longer need to call ExitWindowsEx because Windows Management Instrumentation (WMI—Microsoft's Web-Based Enterprise Management, or WBEM, implementation) provides this capability. WMI provides a robust scripting API. In this case, you can use the Win32Shutdown method that the Win32_OperatingSystem class exposes to log off the current user.
You can learn about WMI's technical details at http://www.microsoft.com/ ntserver/management/techdetails/default.asp. The Microsoft Developer Network (MSDN) Online Library also has WMI information at http://msdn.microsoft.com/ library/default.htm. Use the tree on the left side of the page to navigate to Platform SDK, Management Services, Windows Management Instrumentation.
If you still want to call Win32 APIs from WSH-VBScript code, you can create a wrapper that lets you call functions in external DLLs. Jeff Stong's Windows Developer's Journal article "An Automation Object for Dynamic DLL Calls," November 1998 (http://www.wdj.com/archive/0911), explains how to use an automation object that provides this capability.