Extending the MDT Documentation – Some goodies from ZTIUtility.vbs – String and File handling
The Microsoft Deployment Toolkit comes with a pretty extensive and almost complete documentation, which is even more remarkable, as it’s a free tool.
But even this almost complete documentation, it’s lacking some information that might be helpful for some of you. I’ll start with two things that are available in the ZTIGather.vbs. Why should you care? Well, ZTIGather.vbs is more or less THE core script, that is referenced by almost any other scripts. It holds all the publicly available classes for Logging, environment handling etc. But it also contains two classes, that are only mentioned with one sentence each in the documentation. But can be really helpful if you need to do some advanced things in the customsettings.ini (as you probably know, you can use all available vbscript functions during the gather process, so these will be available too as ZTIUtility.vbs is referenced during that step) or if you write your own scripts, that most probably also reference the ZTIUtility.vbs.
String Handling
The “Strings” class, which is globally available using the object “oStrings” has the following helpful functions:
- isNullOrEmpty (Value) –> Returns True, if the supplied value is either Null or an empty string. False, if not.
- AddToList (List, Item, Delimiter) –> Adds a new item to a delimited list of items. E.G. AddToList (“One,Two,Three”, “Four”, “,”) will return “One,Two,Three,Four”.
- HexWidth (Value, length) –> Returns the supplied value as hex value of the specified length. E.G. HexWidth (2748, 8) returns 00000ABC
- HexWidthByte (Value, Length) –> Returns the first byte of the supplied value as hex value of the specified length. E.G. HexWidthByte (A, 8) returns 00000041
- IsWhiteSpace (Char) –> Returns True, if the supplied Character can be interpreted as a “Whitespace”, so a real space, Tab, VerticalTab, LineFeed, FormFeed or CariageReturn.
- TrimAllWS (String) –> Removes all Whitespaces from the supplied string. E.G. TrimAllWS (“Contributing is everything …”) returns “Contributingiseverything…”
- RightAlign (String, Length) –> Returns a string of the given length, with the supplied string aligned to the right. If Length is lower than the total length of the supplied string, only a substring counted from right will be returned.
- LeftAlign (String, Length) –> Returns a string of the given length with the supplied string. Is the length is lower than the total length of the supplied string, only a substring counted from left will be returned. If it’s larger, Spaces will be appended to the supplied string.
- ForceAsString (Value) –> Returns the supplied value as a string. If the supplied value can’t be interpreted as a string, an empty string will be returned. Arrays will be converted into a space delimited list of items.
- ForceAsArray (Value, Delimiter) –> Returns the supplied value as an array. If the supplied value is a string of delimited items (like from AddToList or ForceAsString), it will split this string at each delimiter into array items.
- GenerateRandomGUID –> Returns a new GUID. Very helpful if you need a unique value.
- base64Encode (Value) –> Returns the supplied value as Base64 encoded string. That’s how e.g. the values of all MDT properties are stored in the Variables.dat to survive a reboot.
- base64Decode (Value) –> Returns the original (decoded) value of the supplied Base64 encoded string.
So if you need a unique string of 6 characters for a computer name, all you need to do is adding the following to your customsettings.ini
1 2 3 4 5 6 7 |
[Settings] Priority=Init,...,Default [Init] [Default] OSDComputerName=XYZ-#Mid(oStrings.GenerateRandomGUID,2,6)# |
Which will then create something similar to XYZ-855CDD during the Gather step.
File Handling
ZTIUtility.vbs also comes with a publicly available class, that does some common file and folder handling. The following functions are available using the object “oFileHandling”. All Functions will log their activity in the log file.
- RemoveFolder (Path) –> Removes the folder specified by the path. It will also remove all files and subfolders. Call RemoveFolderEx (Path, False) to disable the logging.
- DeleteFile (Filepath) –> Deletes the specified File. Call DeleteFileEx (FilePath, False) to disable logging.
- MoveFile (FilePath, DestinationPath) –> Moves the specified file to the specified new Destination. Call MoveFileEx (FilePath, DestinationPath, False) to disable logging.
- CopyFile (FilePath, DestinationPath, Overwrite) –> Copies the specified to the specified location. If Overwrite is set to True, an existing file will be overwritten, if set to False, not. Use CopyFileEx (FilePath, DestinationPath, Overwrite, False) to disable logging.
- CopyFolder (FolderPath, DestinationPath, Overwrite) –> Copies the specified folder to the specified Path. If Overwrite is set to True, an existing folder will be overwritten, if set to False not. Use CopyFolder (FolderPath, DestinationPath, Overwrite, False) to disable logging.
- MoveFolder (FolderPath, DestinationPath) –> Moves the specified Folder to the specified path. User MoveFolder (FolderPath, DestinationPath, False) to disable logging.
- NormalizePath (Path) –> Returns the “complete” path of the supplied path. If you e.g. specify only a file name, the complete path including drive will be returned.
- GetTempFile –> Returns the path to a temporary file at the current temporary folder that could be used. It does not create the temporary file, just returns a unique name that can be used to create one. Use GetTempFileEx (Extension) to return a new temporary file name with the specified extension. On default it will be “tmp”.
- GetTempFolder –> Returns the path to the current Temporay folder.
- GetWindowsFolder –> Returns the path to the current Windows Folder. Should be identical to %WinDir%.
- CheckFileVersion (FileName, MinVersion) –> Returns True if the specified File has at least the specified version or greater. Returns False if less or if the File can’t be found.
2 Responses
[…] some time already and I’ve recently blogged about the available functions for File-handling (see Extending the MDT Documentation – Some goodies from ZTIUtility.vbs – String and File handling for more information). There are also a couple scripts available, that can do this for you and take […]
[…] some time already and I’ve recently blogged about the available functions for File-handling (see Extending the MDT Documentation – Some goodies from ZTIUtility.vbs – String and File handlin… for more information). There are also a couple scripts available, that can do this for you and take […]