Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Wednesday, August 27, 2014

Function: 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).

Note that PowerShell requires defining the function before invoking it, with regards to single-file, sequential ("top-down") processing order.

[powershell]
function Get-TimeWindowStatus {
    Param(
        [parameter(Mandatory=$true)]$Start,
        [parameter(Mandatory=$true)]$End
    )
    $now = Get-Date
    $dif1 = $(New-TimeSpan -Start $Start -End $now).Minutes
    $dif2 = $(New-TimeSpan -Start $End -End $now).Minutes
    if ($dif1 -lt 0) {
        return -1
    }
    elseif ($dif2 -gt 0) {
        return 1
    }
    else {
        return 0
    }
}

$d1 = "8/27/2014 9:30:00"
$d2 = "8/27/2014 10:00:00"

switch (Get-TimeWindowStatus -Start $d1 -End $d2) {
    -1 {write-host "Maintenance window has not begun."}
    1  {write-hsot "Maintenance window has expired."}
    default {Write-Host "Maintenance window is active."}
}
[/powershell]

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).

[asp]
d1 = "8/26/2014 16:00"
d2 = "8/27/2014 9:00"

Select Case TIME_WINDOW_STATUS (d1, d2)
    Case 0:  Response.Write "Maintenance window is in effect."
    Case 1:  Response.Write "Maintenance window has expired."
    Case -1: Response.Write "Maintenance window has not begun."
End Select

Function TIME_WINDOW_STATUS (startDT, endDT)
    Dim dd1, dd2, result
    dd1 = DateDiff("n", d1, NOW)
    dd2 = DateDiff("n", d2, NOW)
    If dd1 > 0 And dd2 < 0 Then 
        result = 0
    ElseIf dd1 < 0 Then
        result = -1
    ElseIf dd2 > 0 Then
        result = 1
    End If
    TIME_WINDOW_STATUS = result
    End Function
[/asp]

[vbscript]
d1 = "8/26/2014 16:00"
d2 = "8/27/2014 9:00"

Select Case TIME_WINDOW_STATUS (d1, d2)
    Case 0:  wscript.echo "Maintenance window is in effect."
    Case 1:  wscript.echo "Maintenance window has expired."
    Case -1: wscript.echo "Maintenance window has not begun."
End Select

Function TIME_WINDOW_STATUS (startDT, endDT)
    Dim dd1, dd2, result
    dd1 = DateDiff("n", d1, NOW)
    dd2 = DateDiff("n", d2, NOW)
    If dd1 > 0 And dd2 < 0 Then 
        result = True
    ElseIf dd1 < 0 Then
        result = False
    ElseIf dd2 > 0 Then
        result = False
    End If
    TIME_WINDOW_STATUS = result
End Function
[/vbscript]

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"}

Wednesday, October 14, 2009

So, what do you get with that PowerShell Combo?

Fries? A drink? A kids toy?  As it turns out, not much.  This is just one example of what has been a trend that really bothers me about the so-called “examples” of PowerShell being published on the net lately.  Most are focused on WMI queries (WQL), or COM marshalling using the New-Object –COM method.  This really doesn’t do anything innovative or beneficial beyond learning a “different” way to code.  All of the WQL examples are simply search/replace code chunks which look almost identical to VBscript examples.

VBScript

Set objOutlook = CreateObject("Outlook.Application")
Set Namespace = objOutlook.GetNamespace("MAPI")

For each f1 in NameSpace.Folders
wscript.echo f1.Name
Next

For each objFolder in NameSpace.Folders
For each f1 in objFolder.Folders
wscript.echo vbTab & f1.Name
Next
Next


PowerShell



$Outlook = New-Object -com Outlook.Application
$Namespace = $outlook.GetNamespace("MAPI")

"{0} Root Folders: " -f $Namespace.folders.count
foreach ($Fl in $namespace.Folders) {
" {0}" -f $Fl.Name}
""
foreach ($Folder in $Namespace.Folders) {
"`'{0}`' subfolders: " -f $Folder.Name
foreach ($fl in $Folder.Folders) {
" {0}" -f $Fl.Name
}
}


I’m not at all trying to “bash” PowerShell.  It’s a cool language, but it has it’s issues that bother me.  I don’t consider it a classic or “true” scripting language as much as programmatic macro language.  Most of the benefits it provides via brevity of code are derived from cmdlets, which are similar to making DLL’s for COM extensibility, or UDF files for KiXtart, and so on.  Blah blah blah.  whatever.  I’m no pundit, so what I have to say doesn’t really matter that much really.  I forgot what point I was trying to make.

Thursday, July 9, 2009

PowerShell: Update Desktop Shortcut Targets


# iterate shortcuts to find matching target paths and
# if found, replace with new target path and save link

$desktop = $home+"\desktop"
$oldPath = "\\server1\sharename1"
$newPath = "\\server2\sharename1"

$shell = New-Object -ComObject WScript.Shell

$Dir = get-childitem $desktop -recurse

foreach($file in $Dir) {
$fname = $file.Name
# write-host $fname
$lnk = $shell.CreateShortcut($desktop+"\"+$fname)
$target = $lnk.TargetPath
if ($target -eq $oldPath) {
write-host "Updating shortcut target..."
$lnk.TargetPath = $newPath
$lnk.Save()
}
}