tag:blogger.com,1999:blog-57476394314630926482024-03-13T14:09:25.363-07:00ScriptZillaBecause you really have nothing better to do than this?skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.comBlogger100125tag:blogger.com,1999:blog-5747639431463092648.post-54587259021581311442014-08-28T18:12:00.000-07:002014-08-28T18:15:11.971-07:00ASP - Cheap Debug Input DumperI call it: Dave's Dastardly Diabolical Debugging Dumbass Dumper, or just Debug Dump for short. It simply prints out any form inputs and querystring inputs and then stops the page rendering process dead. That allows you to structure a processing page to capture inputs and then dump them before proceeding to process them; allowing you a chance to see what may be going in the wrong direction before you go further into the debug abyss.<br />
<div>
<br /></div>
<div>
<span style="color: red;">[asp]</span></div>
<div>
<span style="color: blue; font-family: Courier New, Courier, monospace;"><%</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Sub Show_InputParams ()</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Dim fn, qv, fc, qc</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> fc = 0</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> qc = 0</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "<h1>
Form Inputs</h1>
"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> For each fn in Request.Form()</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "" & fn & " = " & Request.Form(fn) & "<br />
"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> fc = fc + 1</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Next</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "<h1>
QueryString Inputs</h1>
"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> For each qv in Request.QueryString()</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "" & qv & " = " & Request.QueryString(qv) & "<br />
"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> qc = qc + 1</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Next</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "<h2>
(processing halted)</h2>
"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.End</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">End Sub</span><br />
<span style="color: blue; font-family: Courier New, Courier, monospace;">%></span></div>
<div>
<span style="color: red;">[asp]</span></div>
<div>
<br /></div>
<div>
To try this out the quickest way, paste that mess above into a new file and save it as "dumbass.asp". Then access it via your browser along with some test parameters (querystring inputs for now). something like "/mysite/dumbass.asp?test1=123&a=def&x=4321"</div>
<div>
<br /></div>
<div>
It should print out something like the following...</div>
<div>
<br /></div>
<div>
<b>Form Inputs</b></div>
<div>
(no form inputs found)</div>
<div>
<br /></div>
<div>
<b>QueryString Inputs</b></div>
<div>
<br /></div>
<div>
test1 = 123</div>
<div>
a = def</div>
<div>
x = 4321</div>
<div>
3 querystring inputs found</div>
<div>
<br /></div>
<div>
(processing halted)</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3TC-rhawiU5HM-2ZV3FJMBUCow349_GiHh5Qfhes1KAZgpgX2HUMFhh0ojbO2qqg8UNusfvB9Xyfxt5l0aa899P_A4wNWaCMLlvnk56GESKVlZ-5YFHES4tsDBCqkn79JMgrVbiF7ulM/s1600/sz.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3TC-rhawiU5HM-2ZV3FJMBUCow349_GiHh5Qfhes1KAZgpgX2HUMFhh0ojbO2qqg8UNusfvB9Xyfxt5l0aa899P_A4wNWaCMLlvnk56GESKVlZ-5YFHES4tsDBCqkn79JMgrVbiF7ulM/s1600/sz.png" height="170" width="320" /></a></div>
<div>
<br /></div>
skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-57386738797643504482014-08-28T17:58:00.000-07:002014-08-28T17:58:05.754-07:00ASP - Function to Get Windows Logon UserNameThis requires that you have some form of authentication enabled on the web site configuration. I prefer Windows Authentication, but forms or basic might also suffice. Basically, regardless of the web browser, as long as some form of authentication is required, and the user provides it (or the browser hands it over silently, like IE usually does, cough-cough), it will spew forth the "logon_user" or "remote_user" server variable. Using that, you can parse out a NetBIOS domain prefix, such as "contoso\dumbass" to return just the "dumbass" part. <br />
<br />
<span style="color: red;">[asp]</span><br />
<span style="color: blue; font-family: Courier New, Courier, monospace;"><%</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Function Get_UserName()</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Dim tmp, result</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = ""</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> tmp = Trim(Request.ServerVariables("LOGON_USER"))</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> If tmp = "" Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> tmp = Trim(Request.ServerVariables("REMOTE_USER"))</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> End If</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> If tmp <> "" Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> If InStr(tmp, "\") > 0 Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> arrtmp = Split(tmp,"\")</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = Lcase(arrtmp(1)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Else</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = Lcase(tmp)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> End If</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> End If</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Get_UserName = result</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">End Function</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">' test it out...</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">If Get_UserName() = "dave" Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "yay! it's dave!"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Else</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Response.Write "boo. it's not dave. bummer."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">End If</span><br />
<span style="color: blue; font-family: Courier New, Courier, monospace;">%></span><br />
<span style="color: red;">[/asp]</span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3TC-rhawiU5HM-2ZV3FJMBUCow349_GiHh5Qfhes1KAZgpgX2HUMFhh0ojbO2qqg8UNusfvB9Xyfxt5l0aa899P_A4wNWaCMLlvnk56GESKVlZ-5YFHES4tsDBCqkn79JMgrVbiF7ulM/s1600/sz.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3TC-rhawiU5HM-2ZV3FJMBUCow349_GiHh5Qfhes1KAZgpgX2HUMFhh0ojbO2qqg8UNusfvB9Xyfxt5l0aa899P_A4wNWaCMLlvnk56GESKVlZ-5YFHES4tsDBCqkn79JMgrVbiF7ulM/s1600/sz.png" height="170" width="320" /></a></div>
<span style="color: red;"><br /></span>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-74855018438028059502014-08-27T05:14:00.001-07:002014-08-27T05:14:21.039-07:00Function: Get Maintenance Window Status (VBScript, ASP and PowerShell flavors)A little function you can use to determine if a date/time window is active, pending or expired. I've provided both PowerShell, VBScript and ASP examples (almost the same thing).<br />
<br />
Note that PowerShell requires defining the function before invoking it, with regards to single-file, sequential ("top-down") processing order.<br />
<br />
<span style="color: #990000;">[powershell]</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">function Get-TimeWindowStatus {</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Param(</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> [parameter(Mandatory=$true)]$Start,</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> [parameter(Mandatory=$true)]$End</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> )</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> $now = Get-Date</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> $dif1 = $(New-TimeSpan -Start $Start -End $now).Minutes</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> $dif2 = $(New-TimeSpan -Start $End -End $now).Minutes</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> if ($dif1 -lt 0) {</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> return -1</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> elseif ($dif2 -gt 0) {</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> return 1</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> else {</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> return 0</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> }</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">}</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">$d1 = "8/27/2014 9:30:00"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">$d2 = "8/27/2014 10:00:00"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">switch (Get-TimeWindowStatus -Start $d1 -End $d2) {</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> -1 {write-host "Maintenance window has not begun."}</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> 1 {write-hsot "Maintenance window has expired."}</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> default {Write-Host "Maintenance window is active."}</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">}</span><br />
<span style="color: #990000;">[/powershell]</span><br />
<br />
ASP and VBScript on the other hand pre-process script code, so you can define functions anywhere within a given script file, and the location for invoking the function doesn't matter as long as it's in the same file (or loaded in advance using "#include" if using ASP).<br />
<br />
<span style="color: blue;">[asp]</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">d1 = "8/26/2014 16:00"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">d2 = "8/27/2014 9:00"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Select Case TIME_WINDOW_STATUS (d1, d2)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Case 0: Response.Write "Maintenance window is in effect."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Case 1: Response.Write "Maintenance window has expired."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Case -1: Response.Write "Maintenance window has not begun."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">End Select</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Function TIME_WINDOW_STATUS (startDT, endDT)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Dim dd1, dd2, result</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> dd1 = DateDiff("n", d1, NOW)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> dd2 = DateDiff("n", d2, NOW)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> If dd1 > 0 And dd2 < 0 Then </span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = 0</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> ElseIf dd1 < 0 Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = -1</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> ElseIf dd2 > 0 Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = 1</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> End If</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> TIME_WINDOW_STATUS = result</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> End Function</span><br />
<div>
<span style="color: blue;">[/asp]</span></div>
<br />
<span style="color: #b45f06;">[vbscript]</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">d1 = "8/26/2014 16:00"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">d2 = "8/27/2014 9:00"</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Select Case TIME_WINDOW_STATUS (d1, d2)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Case 0: wscript.echo "Maintenance window is in effect."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Case 1: wscript.echo "Maintenance window has expired."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Case -1: wscript.echo "Maintenance window has not begun."</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">End Select</span><br />
<div>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"><br /></span></div>
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">Function TIME_WINDOW_STATUS (startDT, endDT)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> Dim dd1, dd2, result</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> dd1 = DateDiff("n", d1, NOW)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> dd2 = DateDiff("n", d2, NOW)</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> If dd1 > 0 And dd2 < 0 Then </span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = True</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> ElseIf dd1 < 0 Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = False</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> ElseIf dd2 > 0 Then</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> result = False</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> End If</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;"> TIME_WINDOW_STATUS = result</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">End Function</span><br />
<div>
<span style="color: #b45f06;">[/vbscript]</span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3TC-rhawiU5HM-2ZV3FJMBUCow349_GiHh5Qfhes1KAZgpgX2HUMFhh0ojbO2qqg8UNusfvB9Xyfxt5l0aa899P_A4wNWaCMLlvnk56GESKVlZ-5YFHES4tsDBCqkn79JMgrVbiF7ulM/s1600/sz.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3TC-rhawiU5HM-2ZV3FJMBUCow349_GiHh5Qfhes1KAZgpgX2HUMFhh0ojbO2qqg8UNusfvB9Xyfxt5l0aa899P_A4wNWaCMLlvnk56GESKVlZ-5YFHES4tsDBCqkn79JMgrVbiF7ulM/s1600/sz.png" height="170" width="320" /></a></div>
<div>
<span style="color: #b45f06;"><br /></span></div>
skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-68970263075315975252014-08-26T14:56:00.002-07:002014-08-26T14:56:32.585-07:00SCCM Database Queries: Find Executable Files by Name, Version and Install CountFind all instances of a particular file by its distinct Version number (e.g. "FileVersion" property). In this example, I'm looking for what versions of Sysinternals' PsExec.exe are in the environment, and how many instances were found for each version.<br />
<br />
<span style="color: #990000;">[Begin T-SQL]</span><br />
<span style="color: #45818e; font-family: Courier New, Courier, monospace;">SELECT DISTINCT </span><br />
<span style="color: #45818e; font-family: Courier New, Courier, monospace;">[ExecutableName0],[FileVersion0], COUNT(*) AS QTY</span><br />
<span style="color: #45818e; font-family: Courier New, Courier, monospace;">FROM [dbo].[v_GS_INSTALLED_EXECUTABLE]</span><br />
<span style="color: #45818e; font-family: Courier New, Courier, monospace;">WHERE ExecutableName0 = 'psexec.exe'</span><br />
<span style="color: #45818e; font-family: Courier New, Courier, monospace;">GROUP BY ExecutableName0, FileVersion0</span><br />
<span style="color: #45818e; font-family: Courier New, Courier, monospace;">ORDER BY FileVersion0</span><br />
<span style="color: #990000;">[End T-SQL]</span><br />
<span style="color: #990000;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLNik03tZks4nutgKI2Q4L4NnffiRUq881-e4aZi8BS4sOP43fGGDQsDekCCApN7UQ1Rp_7a5-Nay4goeI-HgZSTv_4hKYZ9L0sI1k3PJd750VVrCjfDfQSQsb08e0cabxvnS3j8OUWTM/s1600/sz.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLNik03tZks4nutgKI2Q4L4NnffiRUq881-e4aZi8BS4sOP43fGGDQsDekCCApN7UQ1Rp_7a5-Nay4goeI-HgZSTv_4hKYZ9L0sI1k3PJd750VVrCjfDfQSQsb08e0cabxvnS3j8OUWTM/s1600/sz.png" height="170" width="320" /></a></div>
<span style="color: #990000;"><br /></span>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-36502570375404830112014-08-26T14:26:00.000-07:002014-08-26T14:26:04.714-07:00Find Configuration Manager Collections with Both Direct and Query Rule MembersList all Collections which have both Direct members and Query-Rules assigned within your SCCM 2012 Site. This also works with Configuration Manager 2007...<br />
<br />
<span style="color: #990000;">[begin T-SQL]</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">SELECT DISTINCT </span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">dbo.v_CollectionRuleDirect.CollectionID, </span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">dbo.v_Collection.Name</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">FROM </span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">dbo.v_CollectionRuleDirect INNER JOIN</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">dbo.v_CollectionRuleQuery ON dbo.v_CollectionRuleDirect.CollectionID = dbo.v_CollectionRuleQuery.CollectionID INNER JOIN</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">dbo.v_Collection ON dbo.v_CollectionRuleDirect.CollectionID = dbo.v_Collection.CollectionID</span><br />
<span style="color: #38761d; font-family: Courier New, Courier, monospace;">ORDER BY dbo.v_Collection.Name</span><br />
<span style="color: #990000;">[end T-SQL]</span>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-87522177087293951202013-11-19T07:25:00.000-08:002013-11-19T07:25:57.527-08:00Deleting Sub-Folders with VBScript, Coffee and French FriesDelete all sub-folders beneath a given root folder on multiple (remote) computers. The old RD/RMDIR command will delete the named root folder along with sub-folders, by default. This script leaves the root folder alone and only deletes the sub-folders. Feel free to modify as needed.<br />
<br />
'****************************************************************<br />
' Filename..: fso_delete_subfolders.vbs<br />
' Author....: David M. Stein<br />
' Date......: 11/19/2013<br />
' Purpose...: delete all sub-folders beneath a root path, on multiple computers<br />
' NO WARRANTIES - USE AT YOUR OWN RISK - YOU DAREDEVIL YOU<br />
'****************************************************************<br />
<br />
Dim strServer, objSubFolder<br />
Dim strFolderRoot, strSubFolder, x<br />
<br />
Const strServerList = "SERVER1,SERVER2,SERVER3"<br />
Const strRootPath = "D$\TEMP"<br />
<br />
Set objFSO = CreateObject("Scripting.FileSystemObject")<br />
<br />
For each strServer in Split(strServerList, ",")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>wscript.echo "info: server is " & strServer<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>On Error Resume Next<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>strFolderRoot = "\\" & strServer & "\" & strRootPath<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Set objFolder = objFSO.GetFolder(strFolderRoot)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>If err.Number = 0 Then<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>For each objSubFolder in objFolder.SubFolders<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>strSubFolder = objSubFolder.Name<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>wscript.echo "info: deleting folder --> " & strFolderRoot & "\" & strSubFolder<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>x = objFSO.DeleteFolder(strFolderRoot & "\" & strSubFolder, True)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>wscript.echo "info: result is " & x<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Next<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Else<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>wscript.echo "error [" & err.Number & "]: " & err.Description<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>wscript.echo "info: could be caused by folder-not-found."<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>End If<br />
Nextskatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-35913721472010533622012-07-31T18:32:00.000-07:002012-07-31T18:32:19.192-07:00File Search using WMI CIM_DataFile with VBScript<span style="color: blue;">I looked around, but didn't find a script example that did exactly what I wanted, but I ran across several good alternatives. One thing I found was that if I don't specify the drive letter, it takes WAAAAAAAYYYYY longer to execute. Just a tip. In any case, I hope this helps someone out there...
</span><br />
<br />
<pre>'****************************************************************
' Filename..: fileSearch.vbs
' Author....: ScriptZilla / SkatterBrainz / Dave
' Date......: 07/30/2012
' Purpose...: search for files using WMI/CIM_DataFile
'****************************************************************
time1 = Timer
'----------------------------------------------------------------
' comment: search parameters
'----------------------------------------------------------------
strFileExt = "syn"
strFileName = "*"
strDriveLtr = "c:"
strComputer = "."
'----------------------------------------------------------------
' function:
'----------------------------------------------------------------
Function StringDate(dv)
Dim xdy, xdm, xdd, xdh, xdn, tmp
' example: 20120729195837.171181-240
xdy = Mid(dv,1,4) ' year
xdm = Mid(dv,5,2) ' month
xdd = Mid(dv,7,2) ' day
xdh = Mid(dv,9,2) ' hour
xdn = Mid(dv,11,2) ' minute
tmp = xdm & "/" & xdd & "/" & xdy & " " & xdh & ":" & xdn
StringDate = FormatDateTime(tmp, vbShortDate) & " " & _
FormatDateTime(tmp, vbLongTime)
End Function
'----------------------------------------------------------------
' comment: main script code begins
'----------------------------------------------------------------
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If strFileName = "*" Then
query = "Select * from CIM_DataFile Where Drive='" & strDriveLtr & "'" & _
" AND Extension='" & strFileExt & "'"
Else
query = "Select * from CIM_DataFile Where Drive='" & strDriveLtr & "'" & _
" AND FileName = '" & strFileName & "'" & _
" AND Extension='" & strFileExt & "'"
End If
wscript.echo "info: search criteria = " & strFileName & "." & strFileExt & " on " & strDriveLtr
Set colFiles = objWMIService.ExecQuery(query)
wscript.echo "info: beginning search..."
counter = 0
For Each objFile in colFiles
counter = counter + 1
wscript.echo objFile.Drive & objFile.Path & _
objFile.FileName & "." & objFile.Extension & _
vbTab & StringDate(objFile.CreationDate) & _
vbTab & StringDate(objFile.LastModified) & _
vbTab & objFile.FileSize
Next
wscript.echo "info: " & counter & " matching files found"
wscript.echo "info: " & Timer - time1 & " seconds"
</pre>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-43392172726590848112012-04-15T12:43:00.001-07:002012-04-15T12:43:17.463-07:00Query AD Computers with Custom HOSTS File Entries<pre>'****************************************************************
' Filename..: enum_ad_host_files.vbs
' Author....: David M. Stein
' Date......: 04/15/2012
' Purpose...: search for hosts files with custom entries
'****************************************************************
dns_netbios = "short_name_of_your_active_directory_domain"
Const ForReading = 1
Const ForWriting = 2
wscript.echo "info: scanning domain = " & dns_netbios
Set objDom = GetObject( "WinNT://" & dns_netbios )
Set objFSO = CreateObject("Scripting.FileSystemObject")
tcount = 0
For each obj in objDom
If Lcase(obj.Class) = "computer" Then
computerName = obj.Name
wscript.echo "info: " & computerName
tcount = tcount + 1
CheckHosts computerName
End If
Next
Sub CheckHosts(cn)
Dim filename, objFile, strLine, found
filename = "\\" & cn & "\admin$\system32\drivers\etc\hosts"
wscript.echo "info: searching for: " & filename
If objFSO.FileExists(filename) Then
On Error Resume Next
Set objFile = objFSO.OpenTextFile(filename, ForReading)
If err.Number = 0 Then
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.Readline)
If Left(strLine,1) <> "#" And strLine <> "" Then
found = True
End If
Loop
objFile.Close
If found = True Then
wscript.echo "info: custom entry found!"
Else
wscript.echo "info: no custom entries found."
End If
Else
wscript.echo "fail: error (" & err.Number & ") = " & err.Description
End If
Else
wscript.echo "fail: unable to locate hosts file on " & cn
End If
End Sub
wscript.echo "info: " & tcount & " account objects found"
</pre>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-68076020827763319322011-10-20T08:43:00.000-07:002011-10-20T08:43:56.302-07:00Query Installed Apps a Different WayFor whatever reason, in some environments, a WMI query of Win32_Product is God-awful slow. I've seen this on Windows 7 and Windows 7 SP1 clients, as well as on Windows Server 2008 and 2008 R2. The symptom can be seen from WIM script, WBEM, and using WMIC from a command console with very similar results: The query hangs for 20-25 seconds and then begins executing in spurts. Other Win32 classes work fine, from what I've seen, it's just Win32_Product for some reason. One workaround is to dump a registry output file, and scrub it to make a "clean" output file. You can port this to PowerShell or KiXtart if you want (or whatever you prefer, I really don't care as long as you're happy and that makes me happy so we're all happy. yay!)<br />
<br />
<pre>'****************************************************************
' Filename..: installedApps.vbs
' Author....: David M. Stein aka Scriptzilla aka dipshit
' Date......: 10/20/2011
' Purpose...: save query of installed applications to local file
'****************************************************************
Const strInputFile = "c:\regoutput.txt"
Const strOutputFile = "c:\installedApps.txt"
Const ForReading = 1
Const ForWriting = 2
Const adVarChar = 200
cmd = "reg query hklm\software\microsoft\windows\currentversion\uninstall /s >" & strInputFile
On Error Resume Next
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
wscript.echo "info: executing shell command to create temp file..."
objShell.Run "cmd /c " & cmd, 7, True
wscript.echo "info: getting temp file for input..."
If objFSO.FileExists(strInputFile) Then
wscript.echo "info: reading temp file..."
Set objFile = objFSO.OpenTextFile(strInputFile, ForReading)
Set objFile2 = objFSO.CreateTextFile(strOutputFile, True)
Set rs = CreateObject("ADODB.RecordSet")
rs.CursorLocation = adUseClient
rs.Fields.Append "productname", adVarChar, 255
rs.Open
Do Until objFile.AtEndOfStream
strLine = objFile.Readline
If Left(strLine, 25) = " DisplayName REG_SZ" Then
strOutput = Trim(Mid(strLine, 30))
rs.AddNew
rs.Fields("productname").value = strOutput
rs.Update
End If
Loop
rs.Sort = "productname"
Do Until rs.EOF
objFile2.WriteLine(rs.Fields("productname").value)
rs.MoveNext
Loop
rs.CLose
Set rs = Nothing
objFile.Close
objFile2.Close
wscript.echo "info: finished scrubbing input to new output file"
Else
wscript.echo "fail: temp file not found"
End If
Set objFSO = Nothing
Set objShell = Nothing
'----------------------------------------------------------------
wscript.echo "info: processing complete!"
</pre>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-57392967602040451552011-05-13T13:46:00.001-07:002011-05-13T13:46:53.592-07:00Two Ways to Do String Matching in VBscript<p>The ugly but easier way (using InStr function)</p> <pre>Const strValue = "Microsoft Windows 7 Enterprise Edition"<br />If InStr(strValue, "Windows 7") > 0 Or InStr(strValue, "Windows XP") > 0 Then<br /> wscript.echo "match found"<br />End If</pre><br /><br /><p>The fancier but more irritating way (using the REGEX object):</p><br /><br /><pre>Function MatchString(strToSearch, strPattern)<br /> Dim objRegEx, colMatches, matchFound<br /> Set objRegEx = CreateObject("VBScript.RegExp")<br /> objRegEx.Global = True<br /> objRegEx.Pattern = strPattern<br /> Set colMatches = objRegEx.Execute(strToSearch)<br /> If colMatches.Count > 0 Then<br /> matchFound = True<br /> End If<br /> Set objRegEx = Nothing<br /> MatchString = matchFound<br />End Function<br /><br />If MatchString(strValue, "Windows XP|Windows 7") Then<br /> wscript.echo "match found"<br />End If</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-8934530029929138632011-02-15T06:04:00.000-08:002011-07-30T19:47:45.471-07:00Template CMD Script for App Installs<p>This is obviously bare-bones and will need to be modified to suit specific implementations.  There are dozens of ways to execute a software installation, including setup.exe files, extracting ZIP and other archive files, registering DLLs with REGSVR32, and the ugly-ass, vice-grip + screwdriver + duct tape approach where you manually build folder trees, copy files, register DLLs, add and modify registry keys and make or replace shortcuts.  Oh, the torture.</p> <p>EDIT: Updated 7/30/2011 to include errorlevel 3010 (reboot pending) <br /> <br /> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">@echo off</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem ****************************************************************</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem  Filename..: setup.cmd</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem  Author....: David M. Stein</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem  Date......: <span style="color: red" class="Apple-style-span">07/30/2011</span></span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem  Purpose...: install apps in controlled sequence</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem ****************************************************************</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem Additional Notes:</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem </span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem ****************************************************************</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">title Installing Applications</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">CLS</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo Installing Applications...</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">SETLOCAL</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">set APPNAME=<span style="color: red" class="Apple-style-span">MyApplicationSuite2011</span></span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">set LOG=%TMP%\%APPNAME%_install.log</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">set MSI=/quiet /norestart</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% installing... %APPNAME%... >%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% source....... %~dps0 >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% target....... %COMPUTERNAME% >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% windir....... %WINDIR% >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% progfiles.... %PROGRAMFILES% >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% temp......... %TMP% >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo INSTALL LOG: %LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo ----------------------------------------------- >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo *** <span style="color: red" class="Apple-style-span">APPLICATION NAME 1</span> >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% info: checking if application is already installed... >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">if exist <span style="color: red" class="Apple-style-span">"%ProgramFiles%\FolderName\filename.exe"</span> (</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span"><span style="white-space: pre" class="Apple-tab-span"> </span>echo %DATE% %TIME% info: ## application is already installed >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">) else (</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span"><span style="white-space: pre" class="Apple-tab-span"> </span>echo %DATE% %TIME% info: ## installing application... >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span"><span style="white-space: pre" class="Apple-tab-span"> </span>echo %DATE% %TIME% command = <span style="color: red" class="Apple-style-span">msiexec /i "%~dps0Folder\filename.msi" TRANSFORMS="%~dps0Folder\filename.MST"</span> >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span"><span style="color: red" class="Apple-style-span">   msiexec /i "%~dps0Folder\filename.msi" TRANSFORMS="%~dps0Folder\filename.MST"</span> %MSI%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">   if %errorlevel%==0 (</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">      echo %DATE% %TIME% info: installation SUCCESSFUL >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">   ) else ( <br />      if %errorlevel%==3010 ( <br />         echo %DATE% %TIME% info: installation SUCCESSFUL [reboot pending] >>%LOG% <br />      ) else ( <br />         echo %DATE% %TIME% file: exit code is %errorlevel% >>%LOG%<font size="2"> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">         rem Raise error to parent process!!</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span"><span style="white-space: pre" class="Apple-tab-span"></span>         exit %errorlevel% <br />      )</span></font> <br /></span><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">   )</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">)</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem ------------------------------------------------</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem echo *** APPLICATION NAME 2 >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem ------------------------------------------------</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem repeat code above with modifications as needed</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem ------------------------------------------------</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% info: adjusting application folder permissions... >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; color: red; font-size: x-small" class="Apple-style-span">cacls "%ProgramFiles%\FolderName" /T /E /C /G Users:C</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo ----------------------------------------------- >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% info: applying attachmate file association fix... >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; color: red; font-size: x-small" class="Apple-style-span">REG DEL HKCR\.xxx /f</span> <br /><span style="font-family: 'Courier New', courier, monospace; color: red; font-size: x-small" class="Apple-style-span">REG ADD HKCR\.xxx /ve /d "ApplicationClass.ProgName.1" /f</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo ----------------------------------------------- >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% info: adjusting registry permissions... >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; color: red; font-size: x-small" class="Apple-style-span">REGINI.exe %~dps0customregsettings.ini</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo ----------------------------------------------- >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">echo %DATE% %TIME% completed! result code: %errorlevel% >>%LOG%</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">ENDLOCAL</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">rem Raise error to parent process!!</span> <br /><span style="font-family: 'Courier New', courier, monospace; font-size: x-small" class="Apple-style-span">exit %errorlevel%</span> <br /></p> <div></div> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-19692561995289705062011-02-15T05:49:00.000-08:002011-02-15T05:49:43.850-08:00Querying Files Using Windows Search with VBScript<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'****************************************************************</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' Filename..: scanFiles.vbs</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' Author....: David M. Stein aka Skatterbrainz aka Scriptzilla aka goofy</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' Contact...: ds0934 (at) gmail (dot) com</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' Date......: 02/14/2011</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' Purpose...: generate file scan report using Windows Search queries</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'****************************************************************</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' COPYRIGHT (C) 2011 David M. Stein - All Rights Reserved.</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' No portion of this software code may be reproduced, shared,</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' transmitted, by any means, electronic or otherwise, for any</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' purposes whatsoever, without the explicit prior written consent </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' of the author. </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'****************************************************************</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' DESCRIPTION</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' This script invokes the built-in Windows Desktop Search service</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' to query the local index for files that contain specific string</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' phrases. The string phrases are defined in an external text</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' file which allows for customization of phrases as needed.</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' The output is echoed to the display screen unless redirected</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' to a log file. The associated .cmd file does just that. It is</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' used to execute this script, and redirect the output to a log</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' file which is saved to a central folder (network server share)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'****************************************************************</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Option Explicit</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Const verbose = False</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'----------------------------------------------------------------</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' comment: DO NOT CHANGE ANY CODE BELOW THIS POINT !!!</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' comment: ...or ye shall perish in the land of stupidity</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'----------------------------------------------------------------</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: script initialized " & Now</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Const ForReading = 1</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Const ForWriting = 2</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Dim objConnection, objRecordset, query, scriptPath, inputFile</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Dim objFSO, objFile, strLine, searchList, itemCount : itemCount = 0</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Dim filePath, phrase</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Set objFSO = CreateObject("Scripting.FileSystemObject")</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' comment: determine self-referential path location</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">scriptPath = Replace(wscript.ScriptFullName, "\" & wscript.ScriptName, "")</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">inputFile = "searchlist.txt"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' comment: determine input search list file location</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">filePath = scriptPath & "\" & inputFile</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">On Error Resume Next</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: searching for searchlist.txt input file..."</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">If objFSO.FileExists(filePath) Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> echo "info: reading search values..."</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> Set objFile = objFSO.OpenTextFile(filePath, ForReading)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> Do Until objFile.AtEndOfStream</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> strLine = objFile.Readline</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> If Left(strLine,1) <> ";" Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">If searchList <> "" Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">searchList = searchList & vbTab & strLine</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Else</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">searchList = strLine</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">End If</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">itemCount = itemCount + 1</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">End If</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> Loop</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> objFile.Close</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">End If</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: " & itemCount & " phrases were queued"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'----------------------------------------------------------------</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><span class="Apple-tab-span" style="white-space: pre;"> </span> </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: initializing window search interface..."</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Set objConnection = CreateObject("ADODB.Connection")</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: opening windows search data connection..."</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">If err.Number <> 0 Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> echo "fail: connection-open failure [" & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">err.Number & ":" & err.Description & "]"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> wscript.quit(2)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">End If</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: beginning windows search scan process..."</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">For each phrase in Split(searchList, vbTab)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> wscript.echo "PHRASE: " & phrase</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> ' comment: define search query expression to identify matching files</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> query = "SELECT System.FileName, System.ItemPathDisplay, " & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">"System.DateCreated, System.DateModified " & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">"FROM SYSTEMINDEX WHERE Contains(System.FileName, '" & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Chr(34) & phrase & Chr(34) & "')" & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">" OR Contains('" & Chr(34) & phrase & Chr(34) & "')"</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> ' comment: open connection to service and submit query request</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> Set objRecordSet = CreateObject("ADODB.Recordset")</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> objRecordSet.Open query, objConnection</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> If err.Number <> 0 Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "fail: recordset-open failure [" & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">err.Number & ":" & err.Description & "]"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">objConnection.Close</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Set objConnection = Nothing</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> wscript</span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">.quit(3)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> End If</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> ' comment: if results are not empty, iterate the dataset rows</span><br />
<span class="Apple-tab-span" style="white-space: pre;"><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span></span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> If Not (objRecordset.BOF and objRecordset.EOF) Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">objRecordSet.MoveFirst</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: iterating recordset results..."</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Do Until objRecordset.EOF</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">wscript.echo "MATCH: " & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">objRecordset.Fields.Item("System.ItemPathDisplay").value & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">vbTab & objRecordset.Fields.Item("System.DateCreated").value & _</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">vbTab & objRecordset.Fields.Item("System.DateModified").value</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">objRecordset.MoveNext</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Loop</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">wscript.echo</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> Else</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: no matching records were found"</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> End If</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> objRecordset.Close</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> Set objRecordset = Nothing</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Next</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' comment: close connection to service</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">objConnection.Close</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Set objConnection = Nothing</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">echo "info: processing completed " & Now</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'----------------------------------------------------------------</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">' function: verbose printing</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">'----------------------------------------------------------------</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">Sub Echo(s)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> If verbose = True Then</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">wscript.echo s</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;"> End If</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace; font-size: x-small;">End Sub</span><br />
<div><br />
</div><div><br />
</div><div>EXAMPLE of searchlist.txt</div><div><br />
</div><div>; disable lines by inserting a semi-colon in front of them</div><div>This is a phrase to search for</div><div>this is another phrase</div><div>phrases are case insensitive</div>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-13288746465753404152011-01-26T11:48:00.001-08:002011-02-28T12:45:06.945-08:00Recursively Delete Files by Extension Name<pre>Const strExtensionsToDelete = "wav,avi,mp3,aac,tmp,bak"
Const testMode = True
Sub RecursiveDeleteByExtension(ByVal strDirectory, strExtensionsToDelete)
Dim objFolder, objSubFolder, objFile
Dim strExt
Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile in objFolder.Files
For Each strExt in Split(Ucase(strExtensionsToDelete),",")
If Right(Ucase(objFile.Path), Len(strExt)+1) = "." & strExt Then
wscript.echo "Deleting:" & objFile.Path
If Not testMode = True Then
objFile.Delete
End If
'Exit For
End If
Next
Next
For Each objSubFolder in objFolder.SubFolders
RecursiveDeleteByExtension objSubFolder.Path, strExtensionsToDelete
Next
End Sub
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
RecursiveDeleteByExtension "d:\downloads", strExtensionsToDelete
wscript.echo "finished!"
</pre>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com1tag:blogger.com,1999:blog-5747639431463092648.post-62738286587866503052011-01-26T11:46:00.000-08:002011-01-26T11:46:16.413-08:00Recursively Delete Old Files and Empty FoldersWith a slight twist: The recursion to delete empty folders ignores folders with a name that begins with a tilde "~". You can modify the character or pattern to suit your demented whims.<br />
<br />
<pre>Const maxAge = 30
Const testMode = True
Sub RecursiveDeleteByAge(ByVal strDirectory, maxAge)
Dim objFolder, objSubFolder, objFile
Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile in objFolder.Files
dlm = objFile.DateLastModified
If DateDiff("d", dlm, Now) > maxAge Then
wscript.echo "Deleting:" & objFile.Path
If Not testMode = True Then
objFile.Delete
End If
Exit For
End If
Next
For Each objSubFolder in objFolder.SubFolders
RecursiveDeleteByAge objSubFolder.Path, maxAge
Next
End Sub
Sub RecursiveDeleteEmptyFolders(ByVal strDirectory)
Dim objFolder, objSubFolder
Set objFolder = objFSO.GetFolder(strDirectory)
If objFolder.Files.Count = 0 Then
If objFolder.SubFolders.Count = 0 Then
' no sub-folders beneath this folder...
If Left(objFolder.Name,1) <> "~" Then
wscript.echo "deleting " & objFolder.Path
If Not testMode = True Then
objFolder.Delete
End If
End If
Else
For Each objSubFolder in objFolder.SubFolders
RecursiveDeleteEmptyFolders objSubFolder.Path
Next
End If
Else
wscript.echo "folder is not empty"
For Each objSubFolder in objFolder.SubFolders
RecursiveDeleteEmptyFolders objSubFolder.Path
Next
End If
End Sub
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
RecursiveDeleteByAge "d:\downloads", maxAge
RecursiveDeleteEmptyFolders "d:\downloads"
wscript.echo "finished!"
</pre>skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com2tag:blogger.com,1999:blog-5747639431463092648.post-21461313006162185722010-12-12T04:42:00.001-08:002010-12-12T04:42:14.816-08:00Useful CMD Script Path Expansion Variables<pre><br />@echo off<br />rem http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true<br />echo displaying various path alias values...<br />echo %~nx0<br />echo %comspec%<br />echo %~d0<br />echo %~dp0<br />echo %~dps0<br />echo %~dpp0<br />echo %~f0<br />echo --------------------------------<br />echo %systemdrive%<br />echo %systemroot%<br />echo %windir%<br />echo %programdata%<br />echo %programfiles%<br />echo %userprofile%<br />echo %allusersprofile%<br />echo %temp%<br />echo %commonprogramfiles%<br />echo %commonprogramfiles(x86)%<br />echo %appdata%<br />echo %localappdata%<br />echo %public%<br /></pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-40925457903481428132010-12-12T04:39:00.001-08:002010-12-12T04:40:14.785-08:00Uninstall: 3DS Max 2010<p>CMD Script…</p> <pre><br />@echo off<br />CLS<br />SETLOCAL<br />set APPNAME=3dsMax2010<br />set LOG=%TMP%\%APPNAME%_uninstall.log<br />set MSI=/quiet /norestart<br />echo %DATE% %TIME% installing... %APPNAME%... >%LOG%<br />echo %DATE% %TIME% source....... %~dps0 >>%LOG%<br />echo %DATE% %TIME% target....... %COMPUTERNAME% >>%LOG%<br />echo %DATE% %TIME% windir....... %WINDIR% >>%LOG%<br />echo %DATE% %TIME% progfiles.... %PROGRAMFILES% >>%LOG%<br />echo %DATE% %TIME% temp......... %TMP% >>%LOG%<br />echo ----------------------------------------------- >>%LOG%<br />echo Checking if 3ds Max 2010 is installed >>%LOG%<br />if exist "%programfiles%\Autodesk\3ds Max 2010" GoTo REMOVE<br />goto NOTFOUND<br />goto END<br /> <br />:NOTFOUND<br />echo %DATE% %TIME% 3DS Max 2010 was not detected on this computer<br />goto END<br /> <br />:REMOVE<br />echo %DATE% %TIME% removing 3ds Max 2010 32-bit >>%TMP%\3dsmax2010uninst.log<br />echo command = msiexec.exe /x {317AC0C7-FEBF-0409-87A3-4FC70D0ED900} %MSI% >>%LOG%<br />msiexec.exe /x {317AC0C7-FEBF-0409-87A3-4FC70D0ED900} %MSI%<br />rem ------------------------------------------------------<br />echo %DATE% %TIME% removing 3ds Max 2010 32-bit Components >>%TMP%\3dsmax2010uninst.log<br />echo command = msiexec.exe /x {60A08432-00DD-0409-AC2C-143C75460878} %MSI% >>%LOG%<br />msiexec.exe /x {60A08432-00DD-0409-AC2C-143C75460878} %MSI%<br />rem ------------------------------------------------------<br />echo %DATE% %TIME% removing Backburner 2008.01 >>%TMP%\3dsmax2010uninst.log<br />echo command = msiexec.exe /x {3D347E6D-5A03-4342-B5BA-6A771885F379} %MSI% >>%LOG%<br />msiexec.exe /x {3D347E6D-5A03-4342-B5BA-6A771885F379} %MSI%<br />rem ------------------------------------------------------<br />echo %DATE% %TIME% removing Tutorials Files >>%TMP%\3dsmax2010uninst.log<br />echo command = msiexec.exe /x {E551D82D-4D56-4AF7-A2C9-8897D7A0CB00} %MSI% >>%LOG%<br />msiexec.exe /x {E551D82D-4D56-4AF7-A2C9-8897D7A0CB00} %MSI%<br />rem ------------------------------------------------------<br />echo %DATE% %TIME% removing FBX Plugin 2009.4 - 3ds Max 2010 >>%TMP%\3dsmax2010uninst.log<br />c:<br />cd "%programfiles%\Autodesk\"<br />echo command = "FBX\FBXPlugins\2009.4\3ds Max 2010\Uninstall.exe" /S >>%LOG%<br />"FBX\FBXPlugins\2009.4\3ds Max 2010\Uninstall.exe" /S<br />goto CLEANUP<br />goto END<br /> <br />:CLEANUP<br />echo %DATE% %TIME% cleaning up leftover folders and files >>%LOG%<br />c:<br />if exist "%programfiles%\Autodesk\3ds Max 2010" (<br /> echo Removing 3ds Max 2010 sub-folder >>%LOG%<br /> cd "%programfiles%\Autodesk"<br /> rd "3ds Max 2010" /s /q<br />)<br />if exist "%programfiles%\Autodesk\FBX" (<br /> echo Removing FBX sub-folder >>%LOG%<br /> cd "%programfiles%\Autodesk"<br /> rd "FBX" /s /q<br />)<br />rem ------------------------------------------------------<br />echo %DATE% %TIME% exit code %errorlevel% >>%LOG%<br />echo %DATE% %TIME% completed >>%LOG%<br />goto END<br /> <br />:END<br />exit %errorlevel%<br /></pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-73100586886797413672010-10-27T14:53:00.001-07:002010-10-27T14:53:15.814-07:00A WMI Search-Filter Thingy Script ThingyI worked on this for a couple of hours. It could use a lot more work and embellishment (such as specifying the namespaces in the data file, etc.), but here goes. It reads a text file to scan the local computer for matching WMI class properties (currently only within the CIMv2 namespace). I hope it helps someone. Enjoy! <pre style="font-family: courier,verdana; color: blue; font-size: 9pt">'****************************************************************<br />' Filename..: wmi_rule.vbs<br />' Author....: Scriptzilla / skatterbrainz.blogspot.com<br />' Date......: 10/27/2010<br />' Purpose...: evaluate wmi class property value matches using data file<br />'****************************************************************<br /> <br />scriptPath = Replace(wscript.ScriptFullName, "\" & wscript.ScriptName, "")<br /> <br />ruleFile = scriptPath & "\wmi_rules.txt"<br />Const ForReading = 1<br />Const ForWriting = 2<br />Set objFSO = CreateObject("Scripting.FileSystemObject")<br />On Error Resume Next<br />Set objFile = objFSO.OpenTextFile(ruleFile, ForReading)<br />If err.Number = 0 Then<br /> Do Until objFile.AtEndOfStream<br /> strLine = Trim(objFile.Readline)<br /> If strLine <> "" And Left(strLine,1) <> "'" And Left(strLine,1) <> ";" Then<br /> arrRule = Split(strLine, ",")<br /> For each item in arrRule<br /> arrPair = Split(item, "=")<br /> Select Case arrPair(0)<br /> Case "Class": wmi_class = arrPair(1)<br /> Case "Property": wmi_prop = arrPair(1)<br /> Case "Value": wmi_val = arrPair(1)<br /> Case "MatchType": wmi_match = arrPair(1)<br /> End Select<br /> Next<br /> <br /> If WMIRule(wmi_class, wmi_prop, wmi_val, wmi_match) = True Then<br /> wscript.echo "class....... " & wmi_class<br /> wscript.echo "property.... " & wmi_prop<br /> wscript.echo "value....... " & wmi_val<br /> wscript.echo "matchtype... " & wmi_match<br /> wscript.echo "result ----> TRUE"<br /> wscript.echo<br /> Else<br /> wscript.echo "class....... " & wmi_class<br /> wscript.echo "property.... " & wmi_prop<br /> wscript.echo "value....... " & wmi_val<br /> wscript.echo "matchtype... " & wmi_match<br /> wscript.echo "result ----> FALSE"<br /> wscript.echo<br /> End If<br /> End If<br /> Loop<br /> objFile.Close<br />Else<br /> wscript.echo "fail: unable to open rule file for input"<br />End If<br />Set objFSO = Nothing<br /><br />'----------------------------------------------------------------<br /> <br />Function WMIRule(w_class, w_prop, w_val, w_match)<br /> Dim objClass, objProp, strName, strValue, retval<br /> Dim objWMIService, wmiQuery, colItems, objItem<br /> <br /> strComputer = "."<br /> Set objWMIService = objWMI(strComputer, "classes")<br /> <br /> wmiQuery = "select * from " & w_class<br /> <br /> Set colItems = objWMI(strComputer, wmiQuery)<br /> <br /> For each objItem in colItems<br /> For Each objProp In objItem.Properties_<br /> strName = objProp.Name<br /> If Ucase(strName) = Ucase(w_prop) Then<br /> 'wscript.echo vbTab & "property --> " & w_prop<br /> If IsArray(objProp.Value) Then<br /> strValue = Join(objProp.Value, ";")<br /> Else<br /> strValue = Trim(objProp.Value)<br /> End If<br /> End If<br /> Next<br /> <br /> If strValue <> "" And Not IsNull(strValue) Then<br /> If w_match = "EQUAL" Then<br /> If strValue = w_val Then<br /> retval = True<br /> End If<br /> Else<br /> If InStr(strValue, w_val) > 0 Then<br /> retval = True<br /> End If<br /> End If<br /> End If<br /> Next<br /> WMIRule = retval<br />End Function<br /> <br /> <br />Function objWMI(strComputer, strWQL)<br /> 'On Error Resume Next<br /> Dim wmiNS, objWMIService, objSWbemLocator, objSWbemServices<br /> Dim strUID, strPwd<br /> <br /> wmiNS = "\root\cimv2"<br /> strUID = ""<br /> strPwd = ""<br /> <br /> Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")<br /> <br /> On Error Resume Next<br /> <br /> Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, wmiNS, strUID, strPwd)<br /> <br /> Select Case Err.Number<br /> Case -2147024891:<br /> wscript.echo "error: access denied!"<br /> Exit Function<br /> End Select<br /> <br /> On Error GoTo 0<br /> <br /> Select Case UCase(strWQL)<br /> Case "CLASSES":<br /> Set objWMI = objSWbemServices<br /> Case Else:<br /> Set objWMI = objSWbemServices.ExecQuery(strWQL)<br /> End Select<br /> <br /> Set objSWbemServices = Nothing<br /> Set objSWbemLocator = Nothing<br />End Function</pre><br />And here is the data "rules" file that spells out the class properties to search... <br /><br /><pre style="font-family: courier,verdana; color: blue; font-size: 9pt">'------------------------------------------------------------------------------<br />' wmi_rules.txt<br />'------------------------------------------------------------------------------<br />' each row denotes a rule to be tested within root\cimv2 namespace (for now)<br />' each row contains the WMI: class, property, value, and matchtype<br />' class = win32 class name<br />' property = the name of a given class property<br />' value = the value you wish to test for<br />' matchtype = EQUAL or LIKE<br />'------------------------------------------------------------------------------<br />Class=win32_ComputerSystem,Property=Model,Value=dc7900,MatchType=LIKE<br />Class=win32_OperatingSystem,Property=Caption,Value=Microsoft Windows 7 Enterprise,MatchType=LIKE<br />Class=win32_OperatingSystem,Property=OSArchitecture,Value=64-bit,MatchType=EQUAL<br />Class=win32_Service,Property=Name,Value=VMAuthdService,MatchType=EQUAL</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-52680796609601465632010-10-08T13:57:00.001-07:002010-10-08T13:57:40.016-07:00Making Folder Trees<p>I was asked to provide a script to help someone else with creating a folder tree during a software installation.  I explained that it’s “one line of code” when using a .BAT/.CMD script with “mkdir x:\folder\folder\folder”, but I got the hand in the face, followed by “we need it to be VBscript, not DOS”.  I said “whatever” and cranked out the following hamburger pile. I hope it’s of use to someone else…</p> <pre style="color: blue">rootPath = "c:\program files"<br />folderPath = "vendorname\appname\folder123"<br />fpath = rootPath & "\" & folderPath<br /> <br />Set fso = CreateObject("Scripting.FileSystemObject")<br /> <br />If fso.FolderExists(rootPath) Then<br /> tmp = rootPath<br /> For each s in Split(folderPath, "\")<br /> tmp = tmp & "\" & s<br /> If fso.FolderExists(tmp) Then<br /> wscript.echo "exists: " & tmp<br /> Else<br /> wscript.echo "creating: " & tmp<br /> fso.CreateFolder(tmp)<br /> End If<br /> Next<br />Else<br /> wscript.echo "fail: root path not found = " & rootPath<br />End If</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-20016827038435476082010-09-08T16:28:00.001-07:002010-09-08T16:28:58.013-07:00Stupid-Simple, Yet Useful VBScript Functions<p>Ok, technically some of these are not functions, they're subs (or subroutines), but who cares.  I don't know who invented them, but I've been using these for years and years.  I hope they're helpful to you as well…</p> <p><font color="#0000ff">Sub <strong>Echo</strong>(category, caption) <br />    wscript.echo Now & vbTab category & vbTab & caption <br />End Sub</font></p> <p>Examples…</p> <p><font color="#8064a2">Echo "info", "searching for wmi repository scope on remote computer…" <br />… <br />Echo "fail", "unable to connect to remote computer"</font></p> <p><font color="#0000ff">Function <strong>ScriptPath</strong>() <br />    ScriptPath = Replace(wscript.ScriptFullName, "\" & wscript.ScriptName, "") <br />End Function</font></p> <p><font color="#0000ff">Function <strong>IsWeekend</strong>(strDate) <br />    If Weekday(strDate)=1 Or Weekday(strDate)=2 Then <br />        IsWeekend = True <br />    End If <br />End Function</font></p> <p><font color="#0000ff">Function <strong>LeapYear</strong>(yr) <br />    Dim d1, d2 <br />    d1 = FormatDateTime("1/1/" & yr) <br />    d2 = DateAdd("yyyy", 1, d1) <br />    If DateDiff("d", d1, d2) = 366 Then <br />        LeapYear = True <br />    End If <br />End Function</font></p> <p><font color="#8064a2">For i = 2004 to 2012 <br />    If LeapYear(i) Then <br />        wscript.echo i & vbTab & "is a leap year" <br />    Else <br />        wscript.echo i & vbTab & "is not a leap year" <br />    End If <br />Next</font></p> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-61586058017232869362010-09-08T15:26:00.001-07:002010-09-08T15:26:44.655-07:00Script Tip: Determine the Script File Path<p>With BAT/CMD scripts you can simply prefix any path references with <font color="#ff0000">%~dps0</font> to get to things in the same location (folder/UNC) where the script itself resides.  So if you run..</p> <p><font color="#0000ff">start /wait <font color="#ff0000">%~dps0</font>setup.exe /silent /norestart</font></p> <p>It runs setup.exe from the same folder location. (that is not a typo, there should be NO space between the zero and the rest of the file path)</p> <p>It's just as easy with KiXtart using the <font color="#ff0000">@scriptdir</font> macro, but with VBscript it's not that simple.  But it's not difficult either…</p> <p><font color="#0000ff">scriptPath = Replace(Wscript.<font color="#9b00d3">ScriptFullName</font>, "\" & Wscript.<font color="#9b00d3">ScriptName</font>, "")</font></p> <p>An example of running setup.exe in the same folder with VBscript might be…</p> <p><font color="#0000ff">filePath = scriptPath & "\setup.exe" <br />result = objShell.Run(filePath & " /silent /norestart", 7, True)</font></p> <p>However, be careful to wrap paths in double-quotes if they contain spaces…</p> <p><font color="#0000ff">filePath = Chr(34) & scriptPath & "\setup.exe" & Chr(34) <br />result = objShell.Run(filePath & " /silent /norestart", 7, True)</font></p> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-3740250637375543802010-08-09T13:43:00.001-07:002010-08-09T13:43:05.511-07:00Comparing Processes: Before/After Launching an ApplicationI needed to capture a delta between running processes on my Windows 7 computer before and after launching a particular application.  I could have used some freeware and shareware apps for this, but I wanted something stupid simple (as far as output), not something I had to sift through and tinker with settings, etc.  I hope you find it useful. Beware of word-wrapping when copying this mess. <pre style="color: blue; font-size: 9pt">'****************************************************************<br />' Filename..: taskdump.vbs<br />' Author....: David M. Stein<br />' Date......: 07/27/2010<br />' Purpose...: display user contexts of running processes on remote computer<br />' Notes.....: run as admin (re: remote computer)<br />'****************************************************************<br />Option Explicit<br /><br />Const ForReading = 1<br />Const ForWriting = 2<br />Const offset = 78 ' start point on each row of dump file<br />Const offlen = 50 ' end point on each row of dump file<br /><br />Dim objFile, strLine, uid, ulist<br />Dim objArgs, objFSO, objShell, mode<br />Dim strComputer, temp, outf, retval<br /><br />'----------------------------------------------------------------<br />' comment: check if computer name was provided to script<br />'----------------------------------------------------------------<br /><br />Set objArgs = WScript.Arguments<br />If objArgs.Count = 0 Then<br /> strComputer = Trim(InputBox("Computer Name", "Computer Name"))<br /> mode = 2<br />Else<br /> strComputer = Trim(objArgs(0))<br /> mode = 1<br />End If<br /><br />If strComputer = "" Then<br /> ShowUsage()<br /> wscript.Quit(1)<br />End If<br /><br />wscript.echo "info: computer is " & strComputer<br /><br />'----------------------------------------------------------------<br />' comment: continue on<br />'----------------------------------------------------------------<br /><br />Set objShell = CreateObject("Wscript.Shell")<br />Set objFSO = CreateObject("Scripting.FileSystemObject")<br /><br />'----------------------------------------------------------------<br />' example dump...<br />'----------------------------------------------------------------<br />' Image Name PID Session Name Session# Mem Usage User Name CPU Time<br />' ========================= ======== ================ =========== ============ ================================================== ============<br />' System Idle Process 0 0 28 K N/A 0:43:57<br />' System 4 0 240 K NT AUTHORITY\SYSTEM 0:00:13<br />' smss.exe 548 0 388 K NT AUTHORITY\SYSTEM<br />'----------------------------------------------------------------<br /><br />'----------------------------------------------------------------<br />' comment: define output (dump) file path and name<br />'----------------------------------------------------------------<br /><br />temp = objShell.ExpandEnvironmentStrings("%temp%")<br />outf = temp & "\" & strComputer & ".tsk"<br />ulist = ""<br /><br />'----------------------------------------------------------------<br />' comment: run tasklist to produce dump file<br />'----------------------------------------------------------------<br /><br />retval = objShell.Run("cmd /c tasklist /s " & strComputer & " /v >" & outf, 7, True)<br />wscript.echo "info: exit code was " & retval<br /><br />'----------------------------------------------------------------<br />' comment: if dump file found, open and parse it<br />'----------------------------------------------------------------<br /><br />If objFSO.FileExists(outf) Then<br /> wscript.echo "info: reading dump file..."<br /> On Error Resume Next<br /> Set objFile = objFSO.OpenTextFile(outf, ForReading)<br /> If err.Number = 0 Then<br /> Do Until objFile.AtEndOfStream<br /> strLine = Trim(objFile.Readline)<br /> If strLine <> "" Then<br /> uid = Trim(Mid(strLine, offset, offlen))<br /> ' ignore user "N/A"<br /> If uid <> "N/A" And Left(uid, 3) <> "===" And Left(uid, 4) <> "User" Then<br /> If ulist = "" Then<br /> ulist = Ucase(uid)<br /> Else<br /> ' only collect unique names<br /> If InStr(ulist, Ucase(uid)) < 1 Then<br /> ulist = ulist & vbTab & uid<br /> End If<br /> End If<br /> End If<br /> End If<br /> Loop<br /> objFile.Close<br /> ' display results<br /> If mode = 1 Then<br /> wscript.echo Replace(ulist, vbTab, vbCRLF)<br /> Else<br /> MsgBox Replace(ulist, vbTab, vbCRLF), 64, "User Processes on " & Ucase(strComputer)<br /> End If<br /> Else<br /> wscript.echo "fail: error (" & err.Number & ") = " & err.Description<br /> End If<br />Else<br /> wscript.echo "fail: dump file not found"<br />End If<br /><br />Sub ShowUsage()<br /> wscript.echo<br /> wscript.echo "usage: taskdump.vbs computername"<br /> wscript.echo<br />End Sub</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-90960473075551128782010-07-29T19:20:00.001-07:002010-07-29T19:20:06.605-07:00Script to Auto-Update Your Sysinternals Tools<p><a href="http://twitter.com/faulkner132" target="_blank">Jason Faulkner</a> posted a very cool Batch script to freshen your library of Sysinternals tools over at SysAdminGeek.com.  It queries the live.sysinternals.com\tools repository and downloads matching tools in your library.  If you haven't already checked it out, you should (<a href="http://sysadmingeek.com/articles/batch-script-to-auto-update-sysinternals-tools/" target="_blank">click here</a>).</p> <p>Here's a version I've done in VBScript.  You can tweak it for your needs and even schedule it to run periodically to keep your tools up to date.</p> <pre style="color: blue; font-size: 9pt">Const livetools = "\\live.sysinternals.com\tools"<br />Const localtools = "c:\sysinternals"<br /><br />Set objFSO = CreateObject("Scripting.FileSystemObject")<br /><br />On Error Resume Next<br />wscript.echo "info: connecting to live.sysinternal.com..."<br />Set objLiveFolder = objFSO.GetFolder(livetools)<br />If err.Number <> 0 Then<br /> wscript.echo "fail: unable to connect"<br /> wscript.quit(1)<br />End If<br /><br />If objFSO.FolderExists(localTools) = False Then<br /> wscript.echo "fail: local folder not found"<br /> wscript.quit(2)<br />End If<br /><br />For each objFile in objLiveFolder.Files<br /> fileName = objFile.Name<br /> If objFSO.FileExists(localtools & "\" & fileName) Then<br /> wscript.echo "info: updating file " & fileName & "..."<br /> sourceFile = liveTools & "\" & fileName<br /> targetFile = localTools & "\" & fileName<br /> objFSO.CopyFile sourceFile, targetFile, True<br /> End If<br />Next<br /><br />Set objFolder = Nothing<br />Set objFSO = Nothing</pre><br /><br /><p>If I had more time I'd do this in KiXtart and Powershell, but I'll leave that to someone else.  Enjoy!</p> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-28476092191801576492010-07-20T19:18:00.001-07:002010-07-20T19:18:39.314-07:00MS Security Essentials vNext Silent Install<p>The new Microsoft Security Essentials is <a href="https://connect.microsoft.com/securityessentials" target="_blank">out in beta</a>, and here's how to install it silently using a VBscript.  You still need to tweak it for UAC unless you adapt the command string for use within something else like ConfigMgr.  Enjoy…</p> <pre style="color: blue">const x86 = "mseinstall_en_us_x86.exe"<br />const x64 = "mseinstall_en_us_amd64.exe"<br /><br />Function ScriptPath()<br /> ScriptPath = Replace(wscript.ScriptFullName, "\" & wscript.ScriptName, "")<br />End Function<br /><br />Set fso = createobject("Scripting.FileSystemObject")<br />If fso.FolderExists("c:\Program Files (x86)") Then<br /> cmd = x64<br />Else<br /> cmd = x86<br />End If<br /><br />cmdpath = ScriptPath() & "\" & cmd<br /><br />If fso.FileExists(cmdpath) Then<br /> Set objShell = CreateObject("Wscript.Shell")<br /> objShell.Run cmdpath & " /s /runwgacheck /o", 1, True<br /> Set objShell = Nothing<br />Else<br /> wscript.echo cmdpath & " not found"<br />End If<br /><br />Set fso = Nothing</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0tag:blogger.com,1999:blog-5747639431463092648.post-29842263226730113302010-06-10T16:19:00.001-07:002010-06-10T16:19:02.745-07:00Automating Domain Controller Diagnostics, Version 2.0, Part 2 of 2<p>This is the follow-up script to part 1 (see "Automating Domain Controller Diagnostics, Version 2.0").  This script runs on the member server which has the "Logs$" share, using a scheduled task.  Make sure the scheduled task runs AFTER the individual scheduled tasks on each domain controller are all completed.  I strongly suggest you stagger the individual scheduled tasks a little to avoid impacting all domain controllers at the same time, so the task that runs this script should be run a few minutes or an hour AFTER the last of those is completed.</p> <p>Configure the scheduled task to run this script as the local SYSTEM account.</p> <p>As always: This script is provided as-is without any warranties, implied or explicit.  Use at YOUR OWN RISK.  Edit and test in a safe environment before using in a production environment.</p> <pre style="color: blue; font-size: 9pt">'**************************************************************<br />' Filename: dc_diagnostics_report.vbs<br />' Author: David Stein<br />' Date: 11/20/07<br />' Purpose: Open and Parse report files to produce final report<br />'**************************************************************<br />Const collectionServer = "\\memberserver"<br />Const DebugMode = True<br />Const SendAlerts = True<br />Const mailServer = "mailserver.mydomain.local"<br />Const alertList = "Server Admins <it_server_admins@MYDOMAIN.LOCAL>"<br />Const alertFrom = "IT REPORTS <donotreply @MYDOMAIN.LOCAL>"<br />Const ForReading = 1<br />Const ForWriting = 2<br />Const Verbosity = False<br />Const threshold = 1<br />Const scriptVer = "11.20.07"<br /><br />'--------------------------------------------------------------<br />' declare variables<br />'--------------------------------------------------------------<br /><br />Dim fso, filename, filedate, totalcount, s<br />Dim dcdiag_status, dcdiag_list, collectionFolder<br />Dim netdiag_status, netdiag_list<br />Dim repadmin_status, repadmin_list<br />Dim errorsFound, dcdiag_errors, netdiag_errors, repadmin_errors<br />Dim dclist, ndlist, rplist, strServer<br />Dim listd, listn, listr, shortdate, currenttime<br /><br />shortdate = FormatDateTime(Now,vbShortDate)<br />currentTime = FormatDateTime(Now,vbLongTime)<br />collectionFolder = collectionServer & "\logs$"<br /><br />'--------------------------------------------------------------<br />' initialize list and counter variables<br />'--------------------------------------------------------------<br /><br />dclist = ""<br />ndlist = ""<br />rplist = ""<br /><br />totalcount = 0<br />errorsFound = 0<br />dcdiag_errors = 0<br />netdiag_errors = 0<br />repadmin_errors = 0<br /><br />dcdiag_list = ""<br />netdiag_list = ""<br />repadmin_list = ""<br /><br />'--------------------------------------------------------------<br />' diagnostics printer<br />'--------------------------------------------------------------<br /><br />Sub DebugPrint(s)<br /> If DebugMode Then<br /> wscript.echo s<br /> End If<br />End Sub<br /><br />'--------------------------------------------------------------<br />' main process<br />'--------------------------------------------------------------<br /><br />Sub Main()<br /> Set fso = CreateObject("Scripting.FileSystemObject")<br /><br /> If fso.FolderExists(collectionFolder) Then<br /> dcdiag_status = CountReportFiles("dcdiag")<br /> netdiag_status = CountReportFiles("netdiag")<br /> repadmin_status = CountReportFiles("repadmin")<br /><br /> totalcount = (dcdiag_status + netdiag_status + repadmin_status)<br /><br /> debugprint "info: " & dcdiag_status & " dcdiag report files"<br /><br /> For each s in Split(dcdiag_list,",")<br /> If Trim(s) <> "" Then<br /> debugprint " " & Trim(s)<br /> End If<br /> Next<br /><br /> debugprint "info: " & netdiag_status & " netdiag report files"<br /><br /> For each s in Split(netdiag_list,",")<br /> If Trim(s) <> "" Then<br /> debugprint " " & Trim(s)<br /> End If<br /> Next<br /><br /> debugprint "info: " & repadmin_status & " repadmin report files"<br /><br /> For each s in Split(repadmin_list,",")<br /> If Trim(s) <> "" Then<br /> debugprint " " & Trim(s)<br /> End If<br /> Next<br /><br /> debugprint "info: " & totalcount & " total report files"<br /><br /> listd = IterateReportFiles("dcdiag")<br /> listn = IterateReportFiles("netdiag")<br /> listr = IterateReportFiles("repadmin")<br /><br /> debugprint "-------------------------------------------" & _<br /> vbCRLF & "detail results..." & _<br /> vbCRLF & "-------------------------------------------"<br /> debugprint listd & _<br /> vbCRLF & "-------------------------------------------"<br /> debugprint listn & _<br /> vbCRLF & "-------------------------------------------"<br /> debugprint listr & _<br /> vbCRLF & "-------------------------------------------"<br /> If SendAlerts Then<br /> Dim msgBody, msgSub<br /> If errorsFound > 0 Then<br /> msgSub = "Domain Controller Status Alert"<br /> Else<br /> msgSub = "Domain Controller Status Report"<br /> End If<br /> msgBody = msgSub & _<br /> vbCRLF & "----------------------------------" & _<br /> vbCRLF & "Errors/Warnings: " & errorsFound & _<br /> vbCRLF & "Processed: " & shortdate & " at " & currentTime & _<br /> vbCRLF & "----------------------------------" & vbCRLF<br /><br /> For each s in Split(dcdiag_list,",")<br /> If Trim(s) <> "" Then<br /> msgBody = msgBody & Trim(s) & vbCRLF<br /> End If<br /> Next<br /> msgBody = msgBody & _<br /> vbCRLF & "----------------------------------" & _<br /> vbCRLF & "Details Follow..." & _<br /> vbCRLF & "----------------------------------"<br /> msgBody = msgBody & _<br /> vbCRLF & "DCDIAG Results: " & _<br /> vbCRLF & listd & _<br /> vbCRLF & vbCRLF & "NETDIAG Results: " & _<br /> vbCRLF & listn & _<br /> vbCRLF & vbCRLF & "REPADMIN Results: " & _<br /> vbCRLF & listr & _<br /> vbCRLF & "----------------------------------" & _<br /> vbCRLF & "Note: log report files are collected at" & _<br /> vbCRLF & "the following UNC location and may be" & _<br /> vbCRLF & "accessed there for diagnostics review..." & _<br /> vbCRLF & collectionFolder & _<br /> vbCRLF & "script: dc_diagnostics_report.vbs, version: " & scriptVer<br /><br /> SendMail alertList, alertFrom, msgSub, msgBody, "TEXT"<br /> End If<br /> Else<br /> ' folder not found<br /> End If<br /> Set fso = Nothing<br />End Sub<br /><br />'----------------------------------------------------------------<br />' description:<br />'----------------------------------------------------------------<br /><br />Function ServerFileName(sFilename)<br /> Dim tmp, retval<br /> tmp = Split(sFilename, "_")<br /> On Error Resume Next<br /> retval = tmp(0)<br /> If err.Number <> 0 Then<br /> retval = Left(sFilename,9)<br /> End If<br /> ServerFileName = retval<br />End Function<br /><br />'--------------------------------------------------------------<br />' count, separate and process log files<br />'--------------------------------------------------------------<br /><br />Function CountReportFiles(reportClass)<br /> Dim fld, f, filename, filedate, counter, retval, age<br /> counter = 0<br /> retval = "Server" & vbTab & "Reported" & vbCRLF<br /> Set fld = fso.GetFolder(collectionFolder)<br /> For each f in fld.Files<br /> filename = f.Name<br /> filedate = f.DateLastModified<br /> If InStr(1, filename, reportClass) > 0 Then<br /> age = DateDiff("d", filedate, shortdate)<br /> If Abs(age) > threshold Then<br /> retval = retval & ServerFileName(filename) & vbTab & filedate & " **,"<br /> Else<br /> retval = retval & ServerFileName(filename) & vbTab & filedate & ","<br /> End If<br /> counter = counter + 1<br /> End If<br /> Next<br /> Set fld = Nothing<br /> Select Case reportClass<br /> Case "dcdiag":<br /> dcdiag_list = retval<br /> Case "netdiag":<br /> netdiag_list = retval<br /> Case "repadmin":<br /> repadmin_list = retval<br /> End Select<br /> CountReportFiles = counter<br />End Function<br /><br />'--------------------------------------------------------------<br />' loop through log files<br />'--------------------------------------------------------------<br /><br />Function IterateReportFiles(rType)<br /> Dim fld, f, filename, filepath, retval<br /> retval = ""<br /> Set fld = fso.GetFolder(collectionFolder)<br /> For each f in fld.Files<br /> filename = f.Name<br /> filepath = collectionFolder & "\" & filename<br /> If InStr(1, filename, rType) > 0 Then<br /> retval = retval & AnalyzeReportFile(filepath, rType, ServerFileName(filename))<br /> End If<br /> Next<br /> Set fld = Nothing<br /> IterateReportFiles = retval<br />End Function<br /><br />'----------------------------------------------------------------<br />' description:<br />'----------------------------------------------------------------<br /><br />Function CompareFileDates(d1, d2)<br /> Dim retval<br /> retval = DateDiff("d", d1, d2)<br /> CompareFileDates = retval<br />End Function<br /><br />'--------------------------------------------------------------<br />' open, parse and return result from log file<br />'--------------------------------------------------------------<br /><br />Function AnalyzeReportFile(filespec, reportClass, strServer)<br /> Dim theFile, retval, ln<br /> retval = ""<br /> Set theFile = fso.OpenTextFile(filespec, ForReading, False)<br /><br /> Do While theFile.AtEndOfStream <> True<br /> ln = Trim(theFile.ReadLine)<br /><br /> Select Case reportClass<br /> '----------------------------------------------<br /> ' DCDIAG analysis<br /> '----------------------------------------------<br /><br /> Case "dcdiag":<br /> If InStr(1,ln,"Failed") > 0 Then<br /> retval = retval & strServer & " ... ERROR: " & ln & _<br /> vbCRLF & "....log: " & filespec & vbCRLF<br /> dcdiag_errors = dcdiag_errors + 1<br /> dclist = dclist & strServer & ","<br /> errorsFound = errorsFound + 1<br /> ElseIf InStr(1,ln,"Warning") > 0 Then<br /> retval = retval & strServer & " ... WARNING: " & ln & _<br /> vbCRLF & "....log: " & filespec & vbCRLF<br /> dcdiag_errors = dcdiag_errors + 1<br /> dclist = dclist & strServer & ","<br /> errorsFound = errorsFound + 1<br /> End If<br /><br /> '----------------------------------------------<br /> ' NETDIAG analysis<br /> '----------------------------------------------<br /><br /> Case "netdiag":<br /> Select Case Left(ln,36)<br /> Case "REPLICATION-RECEIVED LATENCY WARNING":<br /> retval = retval & strServer & " ... WARNING: " & ln & _<br /> vbCRLF & "....log: " & filespec & vbCRLF<br /> errorsFound = errorsFound + 1<br /> netdiag_errors = netdiag_errors + 1<br /> ndlist = ndlist & strServer & ","<br /> End Select<br /><br /> Select Case Left(ln,25)<br /> Case ".........................":<br /> If InStr(1,ln,"fail") > 0 Then<br /> netdiag_errors = netdiag_errors + 1<br /> errorsFound = errorsFound + 1<br /> ndlist = ndlist & strServer & ","<br /> retval = retval & strServer & " ... ERROR: " & Mid(ln,27) & _<br /> vbCRLF & "....log: " & filespec & vbCRLF<br /> ElseIf InStr(1,ln,"FATAL") > 0 Then<br /> netdiag_errors = netdiag_errors + 1<br /> errorsFound = errorsFound + 1<br /> ndlist = ndlist & strServer & ","<br /> retval = retval & strServer & " ... FATAL: " & Mid(ln,27) & _<br /> vbCRLF & "....log: " & filespec & vbCRLF<br /> End If<br /> End Select<br /><br /> '----------------------------------------------<br /> ' REPADMIN analysis<br /> '----------------------------------------------<br /><br /> Case "repadmin":<br /> Select Case Left(ln,14)<br /> Case "Last attempt @":<br /> If InStr(1,ln,"fail") > 0 Then<br /> errorsFound = errorsFound + 1<br /> repadmin_errors = repadmin_errors + 1<br /> rplist = rplist & strServer & ","<br /> retval = retval & strServer & " ... ERROR: " & Mid(ln,16) & _<br /> vbCRLF & "....log: " & filespec & vbCRLF<br /> End If<br /> End Select<br /> End Select<br /> Loop<br /> If retval = "" Then<br /> retval = strServer & " ... OK" & vbCRLF<br /> End If<br /> theFile.Close<br /> Set theFile = Nothing<br /> AnalyzeReportFile = retval<br />End Function<br /><br />'--------------------------------------------------------------<br />' send email<br />'--------------------------------------------------------------<br /><br />Sub SendMail(sTo, sFrom, sSubject, sBody, sFormat)<br /> Dim objMessage<br /> Set objMessage = CreateObject("CDO.Message")<br /> objMessage.Subject = sSubject<br /> objMessage.Sender = sFrom<br /> objMessage.To = sTo<br /> If sFormat = "TEXT" Then<br /> objMessage.TextBody = sBody<br /> Else<br /> objMessage.HTMLBody = sBody<br /> End If<br /> objMessage.Configuration.Fields.Item _<br /> ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2<br /> objMessage.Configuration.Fields.Item _<br /> ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer<br /> objMessage.Configuration.Fields.Item _<br /> ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25<br /> objMessage.Configuration.Fields.Update<br /> objMessage.Send<br /> Set objMessage = Nothing<br /> debugprint "info: (sendmail) message sent to " & sTo<br />End Sub<br /><br />Main()<br /><br />Wscript.Quit</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com6tag:blogger.com,1999:blog-5747639431463092648.post-63879564685893035872010-06-10T15:15:00.001-07:002010-06-10T15:17:34.597-07:00Automating Domain Controller Diagnostics, Version 2.0<p>I posted a portion of this some time ago, but this week I received the fourth inquiry about the full version, so I guess it’s time to post it.  Here goes…</p> <p>Create a shared folder on each domain controller named "Data$" and assign permissions to only the "Domain Admins" security group.  Remove all others from the permissions set.</p> <p>Install the Support Tools and latest versions of DCDIAG.exe, NETDIAG.exe, and REPADMIN.exe on each domain controller.  Make SURE they are the same versions on all of them.</p> <p>Create a share named "Scripts$" on a central domain <em>member</em> server.  Assign permissions to allow "Domain Controllers" security group to have Read permissions.  Assign "Domain Admins" to have full control permissions.</p> <p>Create a share named "Logs$" on a central domain <em>member </em>server (can be the same <em>member</em> server as the one above).  Assign permissions to allow "Domain Controllers" security group to have Change permissions (read/write/modify/delete).  Assign "Domain Admins" group full control.</p> <p>Put the script below into the "Scripts$" share.</p> <p>On each domain controller, create a scheduled task to run the script from the "Scripts$" UNC path at a chosen interval (daily, weekly, monthly, quarterly, whatever) using the local "SYSTEM" account.  The "SYSTEM" account operates in the context of the computer (the domain controller on which it is executed) and therefore becomes a member of the "Domain Controllers" group when it attempts to access remote resources (across the LAN/WAN).</p> <p>When the scheduled task executes the script, it should dump the output files into the local "Data$ share.  Another script will be posted soon which crawls through the collected files to produce a summary report of how your domain controllers are doing (with respect to the diagnostics reports for each).</p> <pre style="color: blue; font-size: 9pt">Option Explicit<br />'**************************************************************<br />' Filename: dc_diagnostics.vbs<br />' Author: David Stein<br />' Date: 11/19/07<br />' Purpose: Run and Report Diagnostics on Domain Controllers<br /><br />'**************************************************************<br />' copyright: free for derivative use without any warranties<br />' provided, explicit or implicit, provided that the above info<br />' with author name is included (provide attribution)<br /><br />'**************************************************************<br />Const DebugMode = True<br />Const collectionFolder = "\\memberserver\logs$\"<br />Const alertList = "EMAIL_ADDRESS@mydomain.local"<br />Const alertFrom = "IT REPORTS <donotreply@MYDOMAIN.LOCAL>"<br />Const mailServer = "mail.mydomain.local"<br />Const localShare = "Data$"<br /><br />Const bRunDCDIAG = True<br />Const bRunNETDIAG = True<br />Const bRunREPADMIN = True<br />Const DeleteTempFiles = False<br />Const SendAlerts = True<br />Const SendOnErrorsOnly = True<br /><br />Const bVerbose = False<br /><br />'--------------------------------------------------------------<br />' declare variables<br />'--------------------------------------------------------------<br /><br />Dim objShell, objFSO, strServerName, strServerData<br />Dim strMonthNum, strDayNum, strYear<br />Dim datestamp, dcDiagReport, netDiagReport, repAdminReport<br />Dim statlog, errorCount<br />errorCount = 0<br /><br />'--------------------------------------------------------------<br />' diagnostics status display<br />'--------------------------------------------------------------<br /><br />Sub DebugPrint(code, strval)<br /> If DebugMode Then<br /> wscript.echo Now & vbTab & code & vbTab & strval<br /> End If<br />End Sub<br /><br />'--------------------------------------------------------------<br />' run DCDIAG report<br />'--------------------------------------------------------------<br /><br />Sub RunDCDiag()<br /> Dim cmdstr<br /> cmdstr = "%comspec% /c dcdiag >" & dcDiagReport<br /> DebugPrint "info", "" & cmdstr<br /> objShell.Run cmdstr, 1, True<br /> DebugPrint "info", "dcdiag process completed."<br />End Sub<br /><br />'--------------------------------------------------------------<br />' run NETDIAG report<br />'--------------------------------------------------------------<br /><br />Sub RunNetDiag()<br /> Dim cmdstr<br /> cmdstr = "%comspec% /c netdiag >" & netDiagReport<br /> DebugPrint "info", "" & cmdstr<br /> objShell.Run cmdstr, 1, True<br /> DebugPrint "info", "netdiag process completed."<br />End Sub<br /><br />'--------------------------------------------------------------<br />' run REPADMIN /SHOWREPS report<br />'--------------------------------------------------------------<br /><br />Sub RunRepAdmin()<br /> Dim cmdstr<br /> cmdstr = "%comspec% /c repadmin /showreps >" & repAdminReport<br /> DebugPrint "info", "" & cmdstr<br /> objShell.Run cmdstr, 1, True<br /> DebugPrint "info", "repadmin process completed."<br />End Sub<br /><br />'--------------------------------------------------------------<br />' upload report files to remote collection point<br />'--------------------------------------------------------------<br /><br />Sub CollectReports()<br /> DebugPrint "info", "uploading reports to remote collection point..."<br /> <br /> If bRunDCDIAG Then<br /> If objFSO.FileExists(dcDiagReport) Then<br /> DebugPrint "info", "uploading dcdiag report to collection point..."<br /> 'debugprint "*** " & dcDiagReport<br /> objFSO.CopyFile dcDiagReport, collectionFolder, True<br /> If DeleteTempFiles = True Then<br /> DebugPrint "info", "deleting local dcdiag report file..."<br /> objFSO.DeleteFile dcDiagReport<br /> End If<br /> DebugPrint "info", "dcdiag report uploaded successfully."<br /> DebugPrint "info", "collection-point: " & collectionFolder<br /> statlog = statlog & vbCRLF & "dcdiag report uploaded successfully."<br /> Else<br /> statlog = statlog & vbCRLF & "error: dcdiag report failure!"<br /> DebugPrint "error", "dcdiag report file not found."<br /> errorCount = errorCount + 1<br /> End If<br /> End If<br /><br /> If bRunNETDIAG Then<br /> If objFSO.FileExists(netDiagReport) Then<br /> DebugPrint "info", "uploading netdiag report to collection point..."<br /> objFSO.CopyFile netDiagReport, collectionFolder, True<br /> If DeleteTempFiles = True Then<br /> DebugPrint "info", "deleting local netdiag report file..."<br /> objFSO.DeleteFile netDiagReport<br /> End If<br /> DebugPrint "info", "netdiag report uploaded successfully."<br /> DebugPrint "info", "collection-point: " & collectionFolder<br /> statlog = statlog & vbCRLF & "netdiag report uploaded successfully."<br /> Else<br /> statlog = statlog & vbCRLF & "error: netdiag report failure!"<br /> DebugPrint "error", "netdiag report file not found."<br /> errorCount = errorCount + 1<br /> End If<br /> End If<br /><br /> If bRunREPADMIN Then<br /> If objFSO.FileExists(repAdminReport) Then<br /> DebugPrint "info", "uploading repadmin report to collection point..."<br /> objFSO.CopyFile repAdminReport, collectionFolder, True<br /> If DeleteTempFiles = True Then<br /> DebugPrint "info", "deleting local repadmin report file..."<br /> objFSO.DeleteFile repAdminReport<br /> End If<br /> DebugPrint "info", "repadmin report uploaded successfully."<br /> DebugPrint "info", "collection-point: " & collectionFolder<br /> statlog = statlog & vbCRLF & "repadmin report uploaded successfully."<br /> Else<br /> statlog = statlog & vbCRLF & "error: repadmin report failure!"<br /> DebugPrint "error", "repadmin report file not found."<br /> errorCount = errorCount + 1<br /> End If<br /> End If<br />End Sub<br /><br />'--------------------------------------------------------------<br />' send email<br />'--------------------------------------------------------------<br /><br />Sub SendMail(sTo, sFrom, sSubject, sBody, sFormat)<br /> Dim objMessage<br /> Set objMessage = CreateObject("CDO.Message")<br /> objMessage.Subject = sSubject<br /> objMessage.Sender = sFrom<br /> objMessage.To = sTo<br /> If sFormat = "TEXT" Then<br /> objMessage.TextBody = sBody<br /> Else<br /> objMessage.HTMLBody = sBody<br /> End If<br /> objMessage.Configuration.Fields.Item _<br /> ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2<br /> objMessage.Configuration.Fields.Item _<br /> ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer<br /> objMessage.Configuration.Fields.Item _<br /> ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25<br /> objMessage.Configuration.Fields.Update<br /> objMessage.Send<br /> Set objMessage = Nothing<br /> DebugPrint "info", "(sendmail) message sent to " & sTo<br />End Sub<br /><br />'--------------------------------------------------------------<br />' function: return padded string using parameters<br />' arg: strval (string - value being padded)<br />' arg: intLen (integer - string length to meet)<br />' arg: sChar (string - value to append or prefix to string)<br />' arg: sSide (string - side of string to pad, "L" or "R")<br />'--------------------------------------------------------------<br /><br />Function PadString(strval, intLen, sChar, sSide)<br /> Dim retval<br /> retval = Trim(strval)<br /> Do While Len(retval) < intLen<br /> If Ucase(sSide) = "L" Then<br /> retval = sChar & retval<br /> Else<br /> retval = retval & sChar<br /> End If<br /> Loop<br /> PadString = retval<br />End Function<br /><br />'--------------------------------------------------------------<br />' main subroutine<br />'--------------------------------------------------------------<br /><br />Sub Main()<br /> Dim msgSub, msgBody<br /> Set objShell = Wscript.CreateObject("Wscript.Shell")<br /> Set objFSO = CreateObject("Scripting.FileSystemObject")<br /><br /> strServerName = objShell.ExpandEnvironmentStrings("%computername%")<br /> strServerData = "\\" & strServerName & "\" & localShare & "\"<br /><br /> statlog = ""<br /><br /> strMonthNum = DatePart("m", Now)<br /> If Len(strMonthNum) = 1 Then<br /> strMonthNum = "0" & strMonthNum<br /> End If<br /><br /> strDayNum = DatePart("d", Now)<br /> If Len(strDayNum) = 1 Then<br /> strDayNum = "0" & strDayNum<br /> End If<br /><br /> strYear = DatePart("yyyy", Now)<br /> datestamp = strMonthNum & strDayNum & Right(strYear,2)<br /><br /> DebugPrint "info", "datestamp = " & datestamp<br /> DebugPrint "info", "servername = " & strServerName<br /><br /> If bRunDCDIAG Then<br /> dcDiagReport = strServerData & strServerName & "_dcdiag.txt"<br /> RunDCDiag()<br /> End If<br /><br /> If bRunNETDIAG Then<br /> netDiagReport = strServerData & strServerName & "_netdiag.txt"<br /> RunNetDiag()<br /> End If<br /><br /> If bRunREPADMIN Then<br /> repAdminReport = strServerData & strServerName & "_repadmin.txt"<br /> RunRepAdmin()<br /> End If<br /><br /> CollectReports()<br /><br /> If SendAlerts = True Then<br /> If SendOnErrorsOnly = True Then<br /> ' send alert only when errors occur...<br /><br /> If errorCount > 0 Then<br /> msgSub = "DC Status Check: ERROR - " & strServerName<br /> msgBody = "DC Status Check: ERROR - " & strServerName & vbCRLF & _<br /> "------------------------------" & vbCRLF & _<br /> "One or more diagnostic reports could not" & vbCRLF & _<br /> "be generated or collected from " & strServerName & vbCRLF & _<br /> "------------------------------"<br /> SendMail alertList, alertFrom, msgSub, msgBody, "TEXT"<br /> End If<br /> Else<br /> ' send alert for any status, not just errors...<br /><br /> msgsub = "DC Status Check: SUCCESS - " & strServerName<br /> If bVerbose Then<br /> msgbody = strServerName & " Diagnostics Process Report" & vbCRLF & _<br /> "----------------------------" & vbCRLF & _<br /> "Diagnostics reports have been processed on this " & _<br /> "domain controller with the following results. " & _<br /> "Reports have been uploaded to the central collection " & _<br /> "point for further processing." & vbCRLF & _<br /> "----------------------------" & vbCRLF & statlog<br /> Else<br /> msgbody = strServerName & " Diagnostics Process Report" & vbCRLF & _<br /> "----------------------------" & vbCRLF & _<br /> "Diagnostics reports were uploaded successfully."<br /> End If<br /> SendMail alertList, alertFrom, msgSub, msgBody, "TEXT"<br /> End If<br /> End If<br /><br /> Set objFSO = Nothing<br /> Set objShell = Nothing<br />End Sub<br /><br />'--------------------------------------------------------------<br /><br />Call Main()<br /><br />wscript.Quit</pre> skatterbrainzhttp://www.blogger.com/profile/01375101979702884965noreply@blogger.com0