Saturday, November 28, 2009

SQL Script Exports

Until the day comes when Microsoft SQL Server SSMS provides the same info export features that PHPMyAdmin provides, we’re stuck with the right-click “Script-To…” feature (unless you buy a third-party utility, or write your own SMO or ADOX/ADO scripts, etc.), I tripped and fell over a rather simple feature that I didn’t know existed.  You can export multiple table schemas in one operation by simply clicking on the “Tables” object in the Object Explorer panel, then right-click and choose “Script-To: CREATE” and save to the clipboard/file/query window.  It works the same as individual exports, only that this does all of the Tables at once.  You can do the same by clicking on the “Views” object as well.

If you already knew this, just chuckle, shrug and blow it off and move on.  No harm done.  I wasn’t aware of this, so I figured at least one other person might not know this as well.

Thursday, November 19, 2009

KiXtart: Using FSO File.ReadAll()

Another example of making use of one scripting language or API from another.  In this case, I’m calling on the FileSystemObject API to invoke the ReadAll() function on a file from a KiXtart script.  Just to show that there are all sorts of things you can do like this…

function ReadFile($filepath)
$fso = CreateObject("Scripting.FileSystemObject")
if exist($filepath)
$file = $fso.OpenTextFile($filepath)
$text = $file.ReadAll()
$file.Close()
$file = 0
else
? "error: file not found"
endif
$readfile = $text
endfunction

; example...

? readfile("c:\myfile.txt")

Saturday, November 14, 2009

Restarting a Remote Service (VBScript vs PowerShell)

This kind of goes along with my rants about PowerShell examples being pretty much reformatted VBScript WMI code examples, but regardless, PowerShell does provide a more abbreviated code environment.  The PowerShell code example was derived from a posting by Thomas Lee on the PowerShell blog on restarting a DNS service on a remote computer.  I had a VBScript example which does the same with the Spooler service, so I simply adapted Thomas’ code to support and apples-to-apples comparison.  As you can see, they are very similar.  In fact, the KiXtart version looks almost identical to both as well.

VBScript Example

strComputer = "server1" 
wscript.echo "Restarting Spooler Service on: ",strComputer

Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set ret = wmi.ExecMethod("Win32_Service.Name='Spooler'", "StopService")
If ret.ReturnValue = 0 Then
wscript.echo "Spooler Service stopped"
Else
wscript.echo "Spooler Service not stopped"
End If

Set ret = wmi.ExecMethod("Win32_Service.Name='Spooler '", "StartService")
If ret.ReturnValue = 0 Then
wscript.echo "Spooler Service started"
Else
wscript.echo "Spooler Service not started"
End If


PowerShell Example



$strComputer = "server1" 
"Restarting Spooler Service on: $strComputer"

# get the service object
$svc = gwmi Win32_Service -computer $strComputer | where {$_.name -eq "Spooler"}

# stop the service
$ret = $svc.StopService()
if ($ret.ReturnValue -eq 0) {"Spooler Service stopped"}
else {"Spooler Service not stopped"}

# start the service
$ret = $svc.StartService()
if ($ret.ReturnValue -eq 0) {"Spooler Service started"}
else {"Spooler Service not started"}

Sunday, November 1, 2009

Am I a VMWare Guest Machine or Not?

VBScript Version
Function IsVmWareClient()
Dim objWMIService, colBIOS, objBIOS, retval
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colBIOS = objWMIService.ExecQuery("Select SerialNumber from Win32_BIOS")
For each objBIOS in colBIOS
If Left(objBIOS.SerialNumber, 6) = "VMware" Then
retval = True
Exit For
End If
Next
IsVmWareClient = retval
End Function

' test

If IsVmWareClient() Then
wscript.echo "yes - this is a vmware guest machine"
Else
wscript.echo "not a vmware guest machine"
End If

KiXtart Version

Function IsVmWareClient()
Dim $wmiService, $colBIOS, $objBIOS, $retval
$wmiService = GetObject("winmgmts:\\.\root\CIMV2")
$colBIOS = $wmiService.ExecQuery("select SerialNumber from Win32_BIOS")
For Each $objBIOS in $colBIOS
If Left($objBIOS.SerialNumber, 6) = "VMware"
$retval = 1
EndIf
Next
$IsVmWareClient = $retval
EndFunction

; test

If IsVmWareClient()
? "yes - this is a vmware guest machine"
Else
? "not a vmware guest machine"
EndIf