Thursday, September 25, 2008

Calling Web Services through PowerShell

I do believe that in V2, this will be something that can be done natively within PowerShell. In the meantime, there are a few well documented ways to do this, check out Lee Holmes' example.

I have approached this from a slightly different angle. If you have access to Visual Studio, try something like this:

  1. Create a console app
  2. Set a web reference to your predefined web service
  3. Create your method
  4. Compile
  5. Move the exe to the appropriate location and call as needed from PowerShell

A specific example: I have a paging web service that I can call from my automated monitoring PowerShell scripts when certain conditions are met.

If ($cnt -gt 500) { D:\Script:\Paging.exe "9773" "Import Count: $cnt" }

Works like a charm!

Getting Screen Resolution with WMI and PowerShell

Getting the screen resolution with PowerShell is quite simple:

param( [string]$strComputer = "." )
$displays
= Get-WmiObject `
-class "Win32_DisplayConfiguration" `
-computername $strComputer

foreach ($display in $displays) {
$obj
= New-Object psObject
$obj
Add-Member NoteProperty DeviceName $display.DeviceName
$obj
Add-Member NoteProperty PelsWidth $display.PelsWidth
$obj
Add-Member NoteProperty PelsHeight $display.PelsHeight
$obj
Add-Member NoteProperty BitsPerPel $display.BitsPerPel
$obj
Add-Member NoteProperty DisplayFrequency $display.DisplayFrequency
Write
-Output $obj
}


Setting the resolution is not so simple. There are a few ways to do this, none of them completely native to PowerShell. You can pass parameters to a compiled executable like this one listed on CodeProject or Qres (Thanks Hal for the links). If warranted, and ambition wasn't an issue, you could write inline C#. Check out Lee Holmes' Invoke-Inline for a wrapper for this.

Enjoy!

Thursday, September 4, 2008

Off Topic - Google Chrome's About pages

Many tech (and probably non-tech) folks have been testing Google Chrome.  While my experience has been favorable, minus a couple vendor based black box apps, I am most impressed with the "hidden" about pages.  Check out the following post http://lifehacker.com/5045164/google-chromes-full-list-of-special-about-pages.  Some great stuff here!  I like the fact that about:memory shows the utilization of other browsers.

In addition to the cool "about:" pages, here is a link to the Google Chrome list of shortcuts: http://www.google.com/support/chrome/bin/answer.py?answer=95743

Enjoy!

Wednesday, September 3, 2008

Print Queue Analysis - Revisited

A few weeks ago, I mentioned that I was asked to assist with the monitoring of a print queue. I have now included that PowerGadget chart. Enjoy!

function Get-PrintQueue {
$Printers
= Get-WmiObject `
-Class Win32_PerfFormattedData_Spooler_PrintQueue `
-ComputerName 'PrintServer'`
-Filter 'Name <> "_Total"'

foreach ($Printer in $Printers) {
if($Printer.Jobs -gt 1) {
$obj
= New-Object psObject
$obj
| Add-Member NoteProperty Printer $Printer.Name
$obj
| Add-Member NoteProperty JobCount $Printer.Jobs
Write
-Output $obj}
}
}
$dt
= Get-Date -Format g
Get
-PrintQueue | Out-Chart `
-Title "PrintServer printer queues as of $dt" `
-Size 800,400 `
-gallery bar `
-LegendBox_Visible false `
-View3D_Enabled true `
-AllSeries_BarShape Cylinder `
-AllSeries_PointLabels_Visible true `
-AllSeries_Color Yellow `
-Output "\\WebServer\e$\Inetpub\Extranet\Departments\InfoSys\ClinApps\HIS-Print.png"

Resulting chart:

HISPrint

Monitor Citrix Licenses

Was tasked with monitoring Citrix Licenses. We needed a way to gauge when demand was the highest and overall utilization. Following is a PowerShell script that runs every 15 minutes and stores the result in a SQL Express DB. Will be using PowerGadgets at a later point to present a graphical representation of the data.

First the function to grab the Citrix info:

Function Get-CitrixLicenses {
$Licenses
= Get-WmiObject `
-class 'Citrix_GT_License_Pool' `
-Namespace "ROOT\CitrixLicensing" `
-ComputerName $_ Select __Server, Count, InUseCount
$dt
= Get-Date -Format g
foreach ($License in $Licenses) {
$obj
= New-Object psObject
$obj
Add-Member NoteProperty Server $License.__Server
$obj
Add-Member NoteProperty Total $License.Count
$obj
Add-Member NoteProperty InUse $License.InUseCount
$obj
Add-Member NoteProperty Date $dt
Write
-Output $obj }
}


Second the DB insert function:

function Write-CitrixLicense {

BEGIN {
# Open the DB Connection
$conn
= New-Object System.Data.OleDb.OleDbConnection
$connstr
= "Your connection string"
$conn.connectionstring
= $connstr
$conn.open()

# create DB command
$cmd
= New-Object system.Data.OleDb.OleDbCommand
$cmd.connection
= $conn }

PROCESS {
# create the INSERT statement
using object properties
$now
= Get-Date -form g
$sql
= "INSERT INTO tblCitrixLicense (Server,Count,InUse,
SubmitDate) VALUES ("
$sql
+= "'" + $_.Server + "',"
$sql
+= "'" + $_.Total + "',"
$sql
+= "'" + $_.InUse + "',"
$sql
+= "'" + $now + "')"
$cmd.commandtext
= $sql
$cmd.executenonquery()
Out-Null }

END {$conn.close()}
}


Finally the call....

'Server1','Server2' %{Get-CitrixLicenses} Write-CitrixLicense