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 = "." )
= Get-WmiObject `
-class "Win32_DisplayConfiguration" `
-computername $strComputer

foreach ($display in $displays) {
= New-Object psObject
Add-Member NoteProperty DeviceName $display.DeviceName
Add-Member NoteProperty PelsWidth $display.PelsWidth
Add-Member NoteProperty PelsHeight $display.PelsHeight
Add-Member NoteProperty BitsPerPel $display.BitsPerPel
Add-Member NoteProperty DisplayFrequency $display.DisplayFrequency
-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.


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  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:


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 {
= Get-WmiObject `
-Class Win32_PerfFormattedData_Spooler_PrintQueue `
-ComputerName 'PrintServer'`
-Filter 'Name <> "_Total"'

foreach ($Printer in $Printers) {
if($Printer.Jobs -gt 1) {
= New-Object psObject
| Add-Member NoteProperty Printer $Printer.Name
| Add-Member NoteProperty JobCount $Printer.Jobs
-Output $obj}
= Get-Date -Format g
-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:


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

Second the DB insert function:

function Write-CitrixLicense {

# Open the DB Connection
= New-Object System.Data.OleDb.OleDbConnection
= "Your connection string"
= $connstr

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

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

END {$conn.close()}

Finally the call....

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