DOS command to list all virtual directories in IIS 6 - iis-6

I'm looking for a DOS command to list all sites and virtual directories in IIS 6 under Windows 2003. I know there are ways to do this using Powershell / WMI, VBS, C#, etc. But all I want is a quick and dirty, no-fuss way to do it from DOS, without the need to create any new files on the Webserver.
EDIT: While researching for this question I managed to come up with a one-liner that does it, but please do suggest an alternative if you have a more elegant solution that fits the criteria above.

Here's what I came up with:
#FOR /F "delims=[]" %A IN ('#cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs ENUM /P /w3svc') DO #FOR /F delims^=^"^ tokens^=2 %B IN ('#cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs GET %A/ServerComment') DO #FOR /F delims^=^"^ tokens^=2 %C IN ('#cscript %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/Root/Path') DO #ECHO %A %B "%C"
The command outputs a list of virtual directory ID's, along with the "friendly name" and path for each, e.g.:
/w3svc/1 Default Web Site "c:\inetpub\wwwroot"
/w3svc/1236224994 FunWidgets "C:\Inetpub\wwwroot\FunWidgets"
/w3svc/1359392326 JimSmith.com "C:\Inetpub\wwwroot\JimSmith"
/w3svc/1835917338 BouncyToys "C:\Inetpub\wwwroot\bouncytoys"
/w3svc/198968327 AvalonWest "C:\Inetpub\wwwroot\AvWest"
If you want to pipe the output to a text file, first make sure it doesn't exist, then append >> filename.txt to the command above. (e.g.: DEL sites.txt & ... >> sites.txt)
Here's a breakdown of how the admittedly convoluted command works:
# is prefixed to every statement to avoid echoing the statement itself, which would pollute the output.
#FOR /F "delims=[]" %A IN ('#cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs ENUM /P /w3svc') DO
Invokes AdsUtil.vbs, which is installed with IIS6 (and reads the metabase on our behalf).
The ENUM /P /w3svc parameter tells it to spit out a list of all sites and virtual directory ID's starting at the root node.
The nologo switch suppresses the usual CScript copyright preamble, to render only the output we're interested in. A double-backslash is used to escape the slash character, since we're inside a string.
The output of the portion in single quotes resembles the following:
[/w3svc/1]
[/w3svc/1236224994]
[/w3svc/1359392326]
[/w3svc/1835917338]
[/w3svc/198968327]
[/w3svc/AppPools]
[/w3svc/Filters]
[/w3svc/Info]
This is passed into FOR /F, which loops through each line. delims=[] tells FOR to treat square brackets as delimiters. Everything after the DO will be executed once for each line, with the %A variable set to whatever is between the square brackets. (If this was a batch file you'd use %%A instead).
#FOR /F delims^=^"^ tokens^=2 %B IN ('#cscript //nologo %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/ServerComment') DO
This second FOR block runs AdsUtil with the GET parameter to retrieve the ServerComment property for the given site / virtual directory. This is the human-friendly name as seen in IIS. Unfortunately the output is a bit trickier to parse. e.g. For /w3svc/1 you get back:
ServerComment : (STRING) "Default Web Site"
The careting trick parses out the text between the quotes.
Note that nodes we aren't interested (AppPools, Filters and Info) don't have the ServerComment property, and give a result devoid of quotes, e.g.:
The path requested could not be found.
ErrNumber: -2147024893 (0x80070003)
Error Trying To GET the Object (GetObject Failed): w3svc/Filters
Thus the remaining portion of the command line is not invoked for them.
#FOR /F delims^=^"^ tokens^=2 %C IN ('#cscript %SystemDrive%\Inetpub\AdminScripts\adsutil.vbs //nologo GET %A/Root/Path') DO #ECHO %A %B "%C"
This final FOR retrieves the physical path, then outputs all three pieces of parsed information to the console.

Related

How to use command prompt to list file names in directory but exclude 1st 3 characters

I'm trying to write some vba code that sends a line of code to the command prompt and executes it. I have that part down, but I need help getting the actual code to work.
I want to list all of the files in a specific folder that are the .doc file extension, but I want to exclude the first three characters of the filename that gets printed to my output text file. (Note: I'm using vba because this is one of several different commands I'd like to get into a single vba macro, and I cannot use batch files b/c they are blocked on my system so I'd like to work directly with the command prompt)
The following code works and gives me the file names without the file extension (ie. ABC201704.doc will return as ABC201704)
*%comspec% /c for %i in (C:\Test\ABC*.doc) do #echo %~ni >> C:\Test\Output.txt*
However, I don't know how to modify this so that it doesn't include the first 3 characters (ie. I'd like it to return 201704 instead of ABC201704).
Any help would be greatly appreciated! I tried using the following link, but I couldn't figure out how to get that to work for my situation.
Not tested:
#echo off
setlocal enableDelayedExpansion
for %%a in ("C:\Test\ABC*.doc") do (
set docname=%%~nxa
echo !docname:~3!
)
In command prompt:
cmd /v:on /c "for %a in ("C:\Test\ABC*.doc") do set docname=%~nxa & echo !docname:~3!"

How do I use command prompt to identify derivative generation drop off?

I have one folder of approximately 7500 pdfs and a second folder with approximately 7300 tiff derivatives. Somewhere over the past 4 days of processing, intermittent tiff derivative generation failure occurred. How do I identify which files dropped off?
So far Ive tried:
diff -rq folder_pdfs folder_tiffs
However that reports all files as different given the difference in file extensions.
How do I identify which files dropped off?
Use the following batch file.
MissingFiles.cmd:
#echo off
setlocal
for /f %%f in ('dir /b folder_pdfs') do (
if not exist folder_tiffs\%%~nf.tiff (
echo folder_tiffs\%%~nf.tiff
)
)>>MissingFiles.txt
endlocal
Notes:
MissingFiles.txt will contain the list of missing files.
Example:
F:\test>dir /b folder_pdfs
1.pdf
2.pdf
3.pdf
4.pdf
5.pdf
F:\test>dir /b folder_tiffs
1.tiff
3.tiff
5.tiff
F:\test>MissingFiles.cmd
F:\test>type MissingFiles.txt
folder_tiffs\2.tiff
folder_tiffs\4.tiff
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.
parameters - A command line argument (or parameter) is any value passed into a batch script.

How to .. batch file which executes information in a text file

Sorry not much experience with batch files hence help needed please! ;-)
I'm working in a DOS box on a Windows 7 64 bit system.
I want to run an application as a batch file, but reading the information it needs from a text file which can be updated/amended regularly.
The syntax of the basic application is:
appname "variable" (the variable MUST be enclosed in quotes)
Successive variables can be concatenated to the following single line format:
appname "var1" "var2" "var3" "var4" ... etc
So I've created a batch file containing the above. However, this is unweildy when it comes to updating. Sometimes I omit the delimiting quotes which creates problems in the execution of the batch file.
It seems to me that from an updating/amending point it would be easier to set up a text file, say text.txt which would contain the following information:
"var1"
"var2"
"var3"
"var4"
etc. on successive lines.
This would make it easier for me to update and also to ensure I don't omit the delimiting quotes.
The batch file would get the application to "read" the text file, execute the first variable, then the second etc all the way through to the end. But I'm not sure if this is possible and if so, how to get the batch file to read successive lines in the text file and use those variables.
As I said earlier, I've not much experience with batch files and don't have a clue how to do this! :-(
Help please, thanks
Alan
Like this :
#echo off
set $textFile="test.txt"
for /f "delims=" %%a in ('type %$textFile%') do appname.exe %%a
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "batchline=appname"
FOR /f "delims=" %%a IN (q25193799.txt) DO SET "batchline=!batchline! "%%~a""
ECHO(%batchline%
GOTO :EOF
I used a file named q25193799.txt containing your data for my testing.
The application line is merely echoed for verification. Remove the ECHO( after verification to execute your application.
The data in the file need not have "enclosing quotes".
Thanks to both responses, both different but good in their own way. I use a few different instances and I've decided to use the first response for the longer lists and the second one for shorter lists (that way I can check see if something is going wrong, because I might have missed the second delimiting quote in the text file.
Superb, thanks to you both.

whats the difference between: %%a and %variable% variables?

for /f "tokens=*" %%a in ('find /v ":" "%appdata%\gamelauncher\options.txt" ^| find "menu=a"') do ( set usemenu=a )
for /f "tokens=*" %%a in ('find /v ":" "%appdata%\gamelauncher\options.txt" ^| find "menu=b"') do ( set usemenu=b )
for /f "tokens=*" %%a in ('find /v ":" "%appdata%\gamelauncher\options.txt" ^| find "menu=c"') do ( set usemenu=c )
Right, in this code (which may not work, that what i'm trying to find out) we have this "%%a" in that 'for' command.
First, whats the difference between %variable% and %%a?
Second, can someone explain the 'for' command to me? I have Google'd it way too much and all the explanations seem way to complicated...
What I am trying to do is pull a variable from options.txt, so i can change the menu style of my game launcher. there are 3 styles (a, b and c), so if the options.txt reads "menu=a" how can i get it to set a variable like %usemenu% to the value of a?
Thanks for any help in advance!
%variable% are environment variables. They are set with set and can be accessed with %foo% or !foo! (with delayed expansion if enabled). %%a are special variables created by the for command to represent the current loop item or a token of a current line.
for is probably about the most complicated and powerful part of batch files. If you need loop, then in most cases for has you covered. help for has a summary.
You can
iterate over files: for %x in (*.txt) do ...
repeat something n times: for /l %x in (1, 1, 15) do... (the arguments are start, step and end)
iterate over a set of values: for %x in (a, b, c) do ...
iterate over the lines of a file: for /f %x in (foo.txt) do ...
tokenize lines of a file: for /f "tokens=2,3* delims=," %x in (foo.txt) do ...
iterate over the output of a command: for /f %x in ('somecommand.exe') do ...
That's just a short overview. It gets more complex but please read the help for that.
Variables of the form %%a (or %a if for is used outside of batch files) are very similar to arguments to batch files and subroutines (%1, %2, ...). Some kinds of expansions can be applied to them, for example to get just the file name and extension if the variable represents a file name with path you can use %%~nxa. A complete overview of those is given in help for.
On the other hand, environment variables have other kinds of special things. You can perform replacements in them via %foo:a=b% would result in %foo% except that every a is replaced by a b. Also you can take substrings: %foo:~4,2%. Descriptions of those things can be found in help set.
As to why %variables% and %%a are different things that's a bit hard to answer and probably just a historical oddity. As outlined above there is a third kind of variable, %1, etc. which are very similar to those used in for and have existed for longer, I guess. Since environment variables are a bit unwieldy to use in for due to blocks and thus heavy reliance on delayed expansion the decision probably was made to use the same mechanisms as for arguments instead of environment variables.
Also environment variables could be more expensive, given that the process has a special “environment” block of memory where they are stored in variable=value␀ pairs, so updating environment variables involves potentially copying around a bit of memory while the other kind of variables could be more lightweight. This is speculation, though.
As for your problem, you don't really need for here:
find /v ":" "%appdata%\gamelauncher\options.txt" | find "menu=a" && set usemenu=a
This will only run the set if the preceding command was successful, i.e. menu=a was found. This should be considerably easier than for. From what I read you're trying to look whether menu=a exists in a line that does not contain a colon and in that case usemenu should be set to a, right? (And likewise for b and c. You could try coaxing for into doing that by looping over the lines of the file or output and tokenizing appropriately to figure out the value of menu but depending on the format of the lines this can be tricky. If what you have there works in theory then you should simply stick to that. You can however use a loop around it to avoid having to repeat the same line three times for a, b and c:
for %%X in (a b c) do (
find /v ":" "%appdata%\gamelauncher\options.txt" | find "menu=%%X" && set usemenu=%%X
)
If the file you are parsing is simple, however, with just name=value pairs in each line where : foo would be a comment, then you could use for as well:
for /f "tokens=1,* eol=: delims==" %%A in (%appdata%\gamelauncher\options.txt) do (
if "%%A"=="menu" set usemenu=%%B
)
But that depends a little on the exact format of the file. Above snippet would now read the file line by line and for each line would discard everything after a colon (the eol=: option), use the equals sign as a token delimiter and capture two tokens: The part before the first = and everything after it. The tokens are named starting with %%A so the second one is implicitly %%B (again, this is explained in help for). Now, for each line we examine the first token and look whether it's menu and if so, assign its value to the usemenu variable. If you have a lot of possible options to support this is certainly easier to maintain :-).

How do I reopen programs using CMD from a list or variable

I have a section of code;
:check
for /f "tokens=1* delims==" %%A in ('"wmic process get description, commandline /format:list"') do (
if "%%A"=="CommandLine" (
set "cmd=%%B"
) else if "%%A"=="Description" (
set "desc=%%B"
setlocal enableDelayedExpansion
set "desc=!desc:~0,-1!"
set "cmd=!cmd:~0,-1!"
if /i !desc! == %1 (
echo !cmd! >>C:\test.txt
)
endlocal
)
)
goto:eof
Which pretty much works (this is actually a function called from withing a batch file e.g
call:check processname1.exe
call:check processname2.exe
call:check processname3.exe
etc...
What I'd like to do (if possible), is, insead of echoing to a file, I'd like to be able to create 2 variables. something like;
processname1.exe processname3.exe <-- (for each process 'checked' if it IS running, append its name to this variable)
commandlinepath1 commandlinepath2 <-- (for each process 'checked' if it IS running, append its path to this variable)
If this is possible, and I can then call on these variable later in my script, I'd like to be able to tskill the running processes (easy enough if the variables above can be made), then later on, RE-OPEN these processes (using and command line parameter that were in the original path. This is where I'm lost.
My code above (writing to a file). will give results like;
"C:\somefolder\someexe.exe" -some_parameter
"C:\some therfolder\someotherexe.exe"
"C:\another older\anotherexe.exe" param1 param2
But What I need to do, is take each line of this file (or variable if possible), and run them (if I copy each line into the RUN command of windows, it works, but doing it through CMD it doesn't).
I've tried using a for loop to open the files, and it does, except the script waits for the process to finish beford continuing (and these process won't end, since they are applications). If I try to use START .. then it loads a new CMD window??
What I need to do (in case there is a better option) is
for a pre-determined set of processes, check to see if they are running
kill the ones that are (if they are not, fine ignore it)
delete some files (I can do this, the reason for killin the processes is they hold the files open, preventing deletion)
Re-open all the programs that were originally running
Thanks..
Not a direct answer, but since you already use wmic, maybe using its built in capabilities (query, start & stop) would make your goal easier to achieve?
I come up with the following:
#echo off
setlocal
for %%C in (notepad.exe) do (
for /f "skip=1 tokens=2 delims==" %%F in ('wmic process where description^="%%C" get commandline /format:list') do (
REM required to normalize unicode output from WMIC
set commandline=%%F
REM '\=\\' required as wmic treats \ as escape char in query
call wmic process where commandline='%%commandline:\=\\%%' terminate
REM do your work here
call wmic process call create '%%commandline%%'
)
)
What it does:
First for supplies process names. In my example, it simply is notepad.exe, but you could call with a list: for (process1 process2 process3), or replace it with for /f to supply values from file. If you want to use quoted names, you would have to remove quote from next line (description^="%%C").
Second for does real work: it gets a list of all processes matching description and sequentially stops and starts each of them.
To try it, simply put it in a batfile.bat, open notepad(s) and execute.
Note: if you open notepad with a file, either specify an absolute path, or do it via explorer (double click). The issue here is of current directory - which you could also stumble upon if any of your processes does reference relative paths (unlikely, but not impossible)
Last but not least - doing that in powershell would be the easiest, shortest and most reliable.