Monday, August 26, 2013

DSQuery Active Directory Attributes

DSQuery is a great tool for querying Active Directory however the syntax is a little tricky and difficult to use especially if you do not use it on a regular basis.  This is only a small post to reference the syntax to query all Active Users in the domain to display only two attributes:
  • The users display name
  • The users department
Dsquery * -filter "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))" -attr displayname,department

Feel free to use this article when doing basic queries for active directory attributes against user accounts.

Scripting with Sysinternals tools - Removing the Licensing Agreeement

Mark Russinovich and Bryce Cogswell from the Microsoft Sysinternals team publish many great command line and GUI applications for advance system management and diagnostic tasks.  When using any of their tools, as a user you must first accept a licensing agreement which can be annoying especially when you want to use some of their software in something such as a logon script.  The following is an example using their Disk Usage executable which I copied to C:\Windows\System32:


Now if you do not want this license agreement to pop up for every user, you must add the registry key that accepts the license key to each users profile before the script is launched.  If you are scripting in batch this can be done with:

reg.exe ADD "HKCU\Software\Sysinternals\du" /v EulaAccepted /t REG_DWORD /d 1 /f
All Sysinternals utilities are all configured the same, just replace the \du with the name of the tool such as \psexec.

I also found the a bunch of other Sysinternals applications which you can add to the registry on Peter Hahndorf's blog.  This can be found on the following URL:

http://peter.hahndorf.eu/blog/post/2010/03/07/WorkAroundSysinternalsLicensePopups

Batch File - Output Command to Variable

I am by no means an expert in batch scripting and if you follow my posts you will have noticed I'm more of a VB scripting man.  However I was doing some batch scripting for a customer of mine today in which I need to export some data from a command line executable tool to a variable.

This can be done as follows:

FOR /F "delims=" %i IN ('date /t') DO set today=%i
echo %today%


Now one thing which caught me out,  if you put this code into a batch script "as is" you will notice the script will error out.  This is because to declare variables in a batch script you must use two % signs instead of one.

For example, "set today=%I" needs to be "set today=%%i".  To put the code in a batch script and to make it run you would use:

FOR /F "delims=" %%i IN ('date /t') DO set today=%%i
echo %today%

I hope this small post has been helpful - thankyou for reading.

Sunday, August 25, 2013

VBS - Access is Denied with .Size Option with Vista/2008 or later Operating Systems

I needed to write a VB Script to audit the maximum file size on a bunch of workstations.  To do this in VBS its very simple with only a few lines of code using the .Size option with the folder object you declare under the Scripting.FileSystemObject API.  I have demonstrated the code required to grab the file size below in blue.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\boessenc!\)
Wscript.Echo objFolder.Size


Running this code on ANY folder on a Vista/2008 or higher workstation will give you a Permission denied error:

Microsoft VBScript runtime error: Permission denied


Running this on any older version of Windows such as Windows XP or 2003 works without problems.  The test above I ran the script against my users profile - a folder which of course my account has access to demonstrating the issue.

I also tested running this script against the WMI database with the FileSize property of the Win32_Directory class.  That doesn't work either.

As a result need to utilise another method for grabbing the directory size.