Tuesday, September 29, 2009
TextPad Syntax and Clip Libraries for Scripting
I've also updated many of them recently, so if you ever tried them, you might try them again. Then again, if you don't care, well...
Friday, September 25, 2009
Poor Mans Nested Array Splitting
Some will laugh. Some will scoff. Some will snort and then realize they accidentally blew a booger on their shirt and say “oh damn”. Oh well, here goes…
<%
Const myList = "1=RED,2=YELLOW,3=GREEN,4=CYAN,5=BLUE,6=MAGENTA"
Sub ColorNumList(default)
For each pair in Split(myList, ",")
x = Split(pair, "=")
If Cstr(default) = x(0) Then
Response.Write "<option value=" & x(0) & _
" selected>" & x(1) & "</option>"
Else
Response.Write "<option value=" & x(0) & _
">" & x(1) & "</option>"
End If
Next
End Sub
%>
To use this, you simply insert it within a matching set of ASP code tags inside of a <select></select> form object tag set:
...
<select name="colornum" size="1">
<% ColorNumList 3 %>
</select>
...
Thursday, September 24, 2009
Group Policy: Hide Locked User Display on Windows 7
Computer Configuration
...Policies
...Windows Settings
...Security Settings
...Local Policies
...Security Options:
Interactive logon: Display user information when the session is locked
Enable --> "Do not display user information"
Registry: Hide Locked User Name on Windows 7
CMD console using REG.exe...
REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DontDisplayLockedUserId /t REG_DWORD /d 3 /f
VBScript using Registry object...
Const HKEY_LOCAL_MACHINE = &H80000002 ' more at http://msdn2.microsoft.com/en-us/library/aa394600.aspx Sub AddKey(strComputer, strKeyPath) Set objReg=GetObject( _ "winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\default:StdRegProv") objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath End Sub Post Options Sub AddDWValue(strComputer, strKeyPath, strValueName, iValue) Set objReg=GetObject( _ "winmgmts:{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\default:StdRegProv") objReg.SetDWordValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, iValue End Sub Const k = "SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" AddKey "Computer1", k AddDWValue "Computer1", k, "DontDisplayLockedUserId", 3
KiXtart...
$k = "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System" $=WriteValue($k, "DontDisplayLockedUserId", 3, "REG_DWORD")
This was thrown together pretty quick so it might need tweaking.
VBScript - Get Previous / Next Month Names
Function NextMonthName(dateval)
Dim tmp : tmp = DateAdd("m", 1, dateval)
NextMonthName = MonthName(Month(tmp))
End Function
Function PrevMonthName(dateval)
Dim tmp : tmp = DateAdd("m", -1, dateval)
PrevMonthName = MonthName(Month(tmp))
End Function
VBScript - Checking for a Valid Date
Function DateValid(dv)
Dim retval : retval = ""
On Error Resume Next
retval = FormatDateTime(dv, vbShortDate)
If err.Number = 0 Then
DateValid = True
End If
End Function
Tuesday, September 22, 2009
VBScript CDOsys Message Sending
'------------------------------------------------------
' Send CDONTS email in ASCII text or HTML format
'------------------------------------------------------
Const mailServer = "mailserver.domain.com"
Sub SendMail(sendto, sendfrom, subjectline, msgBody, msgFormat)
If (sendto <> "") and (sendfrom <> "") and (subjectline <> "") and (msgBody <> "") Then
Dim objMessage
Set objMessage = Server.CreateObject("CDO.Message")
objMessage.Subject = subjectline
objMessage.From = sendfrom
objMessage.To = sendto
If msgFormat = "TEXT" Then
objMessage.TextBody = msgBody
Else
objMessage.HTMLBody = msgBody
End If
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
Set objMessage = Nothing
Else
wscript.echo "error: insufficient parameters (sendmail)"
End If
End Sub
Windows Defrag From a Script to a Script and Back Again
Sometimes you just have to unscrew your brain, take it out, set it down on the toilet lid and say “what the hell”. Let’s just say, let’s… that we want to run the DEFRAG command using the /A option to just analyze a volume and somehow we’d like to be able to programmatically (that’s a big word) capture that output and automate something with that mess. Let’s just say.
The command line might look like defrag c: /a /v >defrag.txt. But we’d like to be able to schedule this to run at a certain after-hours time and maybe do something like send an email if the results meet some weird crack-infested condition we dreamed up while eating shrimp cocktail and downing four or five cold ones.
The output of this command might look like this…
Microsoft Disk Defragmenter
Copyright (c) 2007 Microsoft Corp.
Invoking analysis on (C:)...
The operation completed successfully.
Post Defragmentation Report:
Volume Information:
Volume size = 74.40 GB
Cluster size = 4 KB
Used space = 24.78 GB
Free space = 49.61 GB
Fragmentation:
Total fragmented space = 2%
Average fragments per file = 1.08
Movable files and folders = 75092
Unmovable files and folders = 45
Files:
Fragmented files = 2197
Total file fragments = 4809
Folders:
Total folders = 15288
Fragmented folders = 14
Total folder fragments = 43
Free space:
Free space count = 1805
Average free space size = 28.14 MB
Largest free space size = 30.62 GB
Master File Table (MFT):
MFT size = 76.50 MB
MFT record count = 78335
MFT usage = 100%
Total MFT fragments = 2
Note: File fragments larger than 64MB are not included in the fragmentation statistics.
You do not need to defragment this volume.
So, how can we do this automation gyration prognostication? Easy. Using VBScript, FileSystemObject, the Shell object and some coffee, duct tape and a slice of cold pizza, we might arrive at this…
Const inputFile = "c:\windows\temp\defrag.txt"
Const cmd = "cmd /c defrag c: /a /v"
Const ForReading = 1
Const ForWriting = 2
Dim objFSO, objFile, strLine, objShell, parse
Dim label, data
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
wscript.echo "info: running defrag analysis on c: drive..."
objShell.Run cmd & " >" & inputFile, 1, True
wscript.echo "info: defrag completed"
wscript.echo "info: checking if input file was created..."
If objFSO.FileExists(inputFile) Then
wscript.echo "info: file was found, opening for input..."
Set objFile = objFSO.OpenTextFile(inputFile, ForReading)
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.Readline)
If InStr(1, strLine, "=") <> 0 Then
strLine = Replace(strLine, vbTab, "")
parse = Split(strLine, "=")
label = Trim(parse(0))
data = Trim(parse(1))
wscript.echo label & vbTab & data
End If
Loop
objFile.Close
Else
wscript.echo "fail: input file not found"
End If
Set objFSO = Nothing
Set objShell = Nothing
wscript.echo "info: processing complete"
The output might look like this…
info: running defrag analysis on c: drive...
info: defrag completed
info: checking if input file was created...
info: file was found, opening for input...
Volume size 74.40 GB
Cluster size 4 KB
Used space 24.78 GB
Free space 49.61 GB
Total fragmented space 2%
Average fragments per file 1.08
Movable files and folders 75096
Unmovable files and folders 43
Fragmented files 2197
Total file fragments 4810
Total folders 15288
Fragmented folders 14
Total folder fragments 43
Free space count 1802
Average free space size 28.19 MB
Largest free space size 30.62 GB
MFT size 76.50 MB
MFT record count 78335
MFT usage 100%
Total MFT fragments 2
info: processing complete
Now you can insert some code to send email using CDOsys (or whatever you prefer) or send some info to a database table. Then wrap all that up with a tuna sandwich, some fries and a cold drink and you’re off. Sure, you can do a lot of this with WMI directly and that works too. But sometimes there are command console utilities that do things WMI doesn’t do as easily (or simply), so you have options like this (capture the output and then parse back into your script to process further).
Options: You can modify this easily to output XML, INI or just about any text format you desire. You can also pipe it into another script, or pump it into a database. You can also wrap it into an email and send it. Or, because you can capture each line and check the “label” string value, you can check for “Total fragmented space” and if it’s greater than some percentage value, fire an email alert. The take away here is that you can do just about anything you want.
Saturday, September 19, 2009
Back from the Brink of something
Web dictionaries describe it as a "region marking a boundary". But what is a "boundary"? For that matter, what is a "region"? I could go on, but who cares. This is where I'll be posting script stuff. Again. And for those who got pissed off at me last time: Too Fkking bad.