Entries in Windows (4)

Friday
Apr162010

Generating a system list from Active Directory

Having a large number of workstations or servers within an enterprise brings a host of challenges in trying to maintain them. We are constantly having to reach out and perform an action on a set of boxes. Relying on your own or someone else's outdated system list is not an option. A quick and easy fix is to go straight to the source. Dsquery is a command-line too that allows us to query Active Directory based on criteria we feed into it. Today we are going to focus on one common criteria used, dsquery computer, which finds computers in the directory that matches our search patterns. Here's the syntax:

dsquery computer [{<StartNode> | forestroot | domainroot}] [-o {dn | rdn | samid}] [-scope {subtree | onelevel | base}] [-name <Name>] [-desc <Description>] [-samid <SAMName>] [-inactive <NumberOfWeeks>] [-stalepwd <NumberOfDays>] [-disabled] [{-s <Server> | -d <Domain>}] [-u <UserName>] [-p {<Password> | *}] [-q] [-r] [-gc] [-limit <NumberOfObjects>] [{-uc | -uco | -uci}]

So to pull a list of workstations from OU Test we can simply run:

C:\> dsquery computer "OU=Test,DC=testdomain,DC=com" -limit 0 -o rdn

"workstation1"
"workstation2"
"workstation3"

So if we break that command down we see that we are querying the Test OU in our domain for computers, -limit 0 means give me everything found, and in an output specifed as rdn. RDN displays the relative distinguished name of each entry. This will allow us to parse it in a for loop and run commands against it.

So let's say we want to ping those machines to see which ones are up:

C:\> for /f %w in ('dsquery computer "OU=Test,DC=testdomain,DC=com" -limit 0 -o rdn') do (
more? ping -n 1 %w
more? )

Pretty easy right!

So what if you don't know what OU a workstation is in? Well dsquery can find out that too! Lets suppose your workstations are called WORKSTATION1, WORKSTATION2, and so on. We can use a portion of the name to find out where they are:

C:\> dsquery computer -limit 0 | find "WORK"

Here's a quick example of a bat file I threw together taking advantage of dsquery.

@echo off
title Build WKS List
rem --------------------------------------------
rem ScriptName: build_wks_list.bat
rem
rem Usage:
rem build_wks_list.bat wks_OU output_dir
rem
rem Example:
rem build_wks_list.bat Test c:\admin
rem
rem Change History:
rem DATE DEVELOPER CHANGE
rem 19MAR10 shaun hess Initial Creation
rem --------------------------------------------
IF %1.==. GOTO USAGE
IF %2.==. GOTO USAGE

setlocal
set HOSTLIST=%2\%1_wkslist.txt
set HOSTLISTTEMP=%2\%1_wkslist.tmp
rem Modify the variable below to include OU's you
rem want to run against when passing the arg "All"
set SITELIST=Test, Engineering, Production
set OU=%1
set OUTPUT_DIR=%2
set ERRORLOG=%2\build_wks_list_error.log

echo.
if exist %HOSTLISTTEMP% del /f /q %HOSTLISTTEMP%
if exist %HOSTLIST% del /f /q %HOSTLIST%

if /I %1 == all (
for %%i in (%SITELIST%) do (
@echo Building workstation list for OU=%%i
dsquery computer "OU=%%i,DC=testdomain,DC=com" -limit 0 -o ^
rdn >> %HOSTLISTTEMP% 2> %ERRORLOG%
for /f "delims=" %%a in (%HOSTLISTTEMP%) do echo %%~a >> %HOSTLIST%
del /f /q %HOSTLISTTEMP%
@echo Workstation List: %HOSTLIST%
) else (
@echo Building workstation list for OU=%1
dsquery computer "OU=%1,DC=testdomain,DC=com" -limit 0 -o ^
rdn >> %HOSTLISTTEMP% 2> %ERRORLOG%
for /f "delims=" %%a in (%HOSTLISTTEMP%) do echo %%~a >> %HOSTLIST%
del /f /q %HOSTLISTTEMP%
@echo Workstation List: %HOSTLIST%
)
goto done

:USAGE
echo.
echo Usage:
echo.
echo build_wks_list.bat wks_OU output_dir
echo.
echo Ex: build_wks_list.bat Test c:\admin
echo.
echo You may also pass the argument "All" for
echo the OU field if your sitelist is setup.
echo.

:done
echo.
if not "%ERRORLEVEL%"=="0" (
echo Please see %ERRORLOG% for details.
echo.
echo Script failed with return code %ERRORLEVEL%
echo.
) else (
echo Script completed successfully.
)
endlocal

exit /b

Some other useful arguments to dsquery computer are:

-name <Name>

Searches for computers whose name attributes (value of CN attribute) matches <Name>. For example, "jon*" or "*ith" or "j*th".

-desc <Description>

Searches for computers whose description attributes match <Description>. For example, "jon*", "*ith", or "j*th".

-inactive <NumberOfWeeks>

Searches for computers that have been inactive (stale) for the number of weeks thatou specify.

-stalepwd <NumberOfDays>

Searches for computers whose passwords have not changed for the number of days that you specify.

-disabled

Searches for all computers whose accounts are disabled.


If you have other handy ways to build system lists feel free to share in the comments. If not give dsquery a try!

Thursday
Sep242009

Shutdown or Reboot a remote Windows box with remote credentials (Quick Tip)

Quick Tip: Shutdown or Reboot a remote Windows box with remote credentials from the cmd line

Heres the setup, your logged into a workstation and need to reboot a remote box, but your account doesn't have the juice to run the standard shutdown command:

Many people don't know there is a simple flag you can use to pass remote credentials (the acct doesn't need to exist on the local box or even the same domain!) using the runas command:

Success! Remember you can run all sorts of commands both remotely and locally using different credentials using this command.

Tuesday
Jun162009

Fastest method of copying files in Windows?

This question popped up in the community wiki over on ServerFault, so I decided to take a crack at it.

From a performance standpoint only, xcopy or robocopy will give you similar results. I ran through a couple of tests on a Vista 64bit sp2 box to do some comparisons. All copies were performed between a internal 7200 RPM Sata II disk and an external USB 2.0 drive or on the same internal drive itself where indicated. No special setup was done (make up your own mind if that invalidates/validates the test), only to input the command into a batch file to execute. Powershell was used to capture the start and stop times. After a couple of passes here are the averages from the tools I played with:

File: 732,909,568bytes (698MB) 1 ISO file copied to different dir on same internal disk

copy     6secs  (ex. copy G:\folder1\* G:\folder2\)
xcopy    6secs  (ex. xcopy G:\folder1 G:\folder2 /I /E /Y /R)
robocopy 6secs  (ex. robocopy G:\folder1\ G:\folder2 /E /NP)
teracopy 28secs (ex. TeraCopy.exe Copy G:\folder1\ G:\folder2\)
fastcopy 19secs (ex. fastcopy.exe /auto_close G:\folder1 /to=G:\folder2)

File: 732,909,568bytes (698MB) 1 ISO file copied to external usb disk

copy     36secs (ex. copy G:\folder1\* I:\folder2\)
xcopy    35secs (ex. xcopy G:\folder1 I:\folder2 /I /E /Y /R)
robocopy 36secs (ex. robocopy G:\folder1\ I:\folder2 /E /NP)
teracopy 36secs (ex. TeraCopy.exe Copy G:\folder1\ I:\folder2\)
fastcopy 38secs (ex. fastcopy.exe /auto_close G:\folder1 /to=I:\folder2)

Files: 45,039,616bytes (42.9MB) 5 random files copied to external usb disk

copy     6secs  (ex. copy G:\folder1\* I:\folder2\)
xcopy    5secs  (ex. xcopy G:\folder1 I:\folder2 /I /E /Y /R)
robocopy 6secs  (ex. robocopy G:\folder1\ I:\folder2 /E /NP)
teracopy 12secs (ex. TeraCopy.exe Copy G:\folder1\ I:\folder2\)
fastcopy 6secs  (ex. fastcopy.exe /auto_close G:\folder1 /to=I:\folder2)

Files/Dirs: 1,087,180,800bytes (1.01GB) 27 Files/8 Dirs copied to external usb disk

copy     *Not included in test
xcopy    57secs (ex. xcopy G:\folder1 I:\folder2 /I /E /Y /R)
robocopy 58secs (ex. robocopy G:\folder1\ I:\folder2 /E /NP)
teracopy 56secs (ex. TeraCopy.exe Copy G:\folder1\ I:\folder2\)
fastcopy 60secs (ex. fastcopy.exe /auto_close G:\folder1 /to=I:\folder2)

This is by no means an exhaustive test, but just throwing a quick real world scenario at some of the more popular tools in this genre shows that your pretty safe sticking with either xcopy or robocopy (from a performance standpoint only). Also the robocopy option /NP No Progress saves you 0 time. That doesn't mean you cannot benefit from using something other than xcopy however. RoboCopy is a great example (from Wikipedia):

Robocopy is notable for capabilities above and beyond the built-in Windows copy and
xcopy commands, including the following:

  • Ability to tolerate network outages and resume copying where it previously left off (incomplete files are noted with a date stamp corresponding to 1980-01-01 and contain a recovery record so Robocopy knows from where to continue).
  • Ability to correctly copy attributes, owner information, alternate data streams, auditing information, and timestamps by default, without the need for numerous often forgotten command line switches.
  • Ability to correctly copy NTFS ACLs, (when /COPYALL provided), and to assert the Windows NT "backup right" (/B) so an administrator may copy an entire directory, including files denied readability to the administrator.
  • Persistence by default, with a programmable number of automatic retries if a file cannot be opened.
  • A "mirror" mode, which keeps trees in sync by optionally deleting files out of the destination that are no longer present in the source.
  • Ability to copy large numbers of files that would otherwise crash the built-in XCOPY utility.
  • A progress indicator on the command line that updates continuously.
  • Ability to copy long file and folder names exceeding 256 characters — up to a theoretical 32,000 characters — without errors.