PowerShell for Windows Admins


June 16, 2011  11:03 AM

New MEAP release for PowerShell and WMI

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Chapters 10 and 11 have been added to the PowerShell and WMI early access release – www.manning.com/siddaway2

Chapter 10 is about printers and it covers:

  • discovering printer configurations;
  • testing printer status;
  • discovering and comparing printer drivers;
  • managing printers;
  • administering print jobs; and
  • testing printers.

Chapter 11 dives into the details of networking:

  • discovering network adapters and their configuration;
  • discovering active protocols;
  • configuring static IP addresses and other TCP/IP settings;
  • enabling DHCP on the adapter;
  • managing DNS and WINS settings in the TCP/IP properties; and
  • displaying the IPv4 routing table.

The code for these and all earlier chapters is available for download.
What’s Next?
In Chapter 12, we will look at managing IIS.

June 15, 2011  12:53 PM

root\wmi–Monitor information

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Continuing our intermittent browse through the root\wmi namespace we find a set of classes that seem to be linked to monitors

Get-WmiObject -Namespace root\wmi -List WmiMonitor*

WmiMonitorBrightnessEvent
WmiMonitorConnectionParams
WmiMonitorRawEEdidV1Block
WmiMonitorListedFrequencyRanges
WmiMonitorDigitalVideoInputParams
WmiMonitorAnalogVideoInputParams
WmiMonitorID
WmiMonitorBrightnessMethods
WmiMonitorBasicDisplayParams
WmiMonitorColorCharacteristics
WmiMonitorDescriptorMethods
WmiMonitorListedSupportedSourceM…
WmiMonitorBrightness
WmiMonitorColorXYZinCIE
WmiMonitorSupportedDisplayFeatures

 

WmiMonitorBasicDisplayParams   seems like a good place to start

Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams

produces

Active                        : True
DisplayTransferCharacteristic : 120
InstanceName                  : DISPLAY\LGD6301\5&21e3487a&0&UID33554704_0
MaxHorizontalImageSize        : 34
MaxVerticalImageSize          : 19
SupportedDisplayFeatures      : System.Management.ManagementBaseObject
VideoInputType                : 1

 

The supporteddisplayfeatures we need to dig into a bit

$monitor = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams
$monitor.SupportedDisplayFeatures

ActiveOffSupported     : False
DisplayType            : 1
GTFSupported           : False
HasPreferredTimingMode : True
sRGBSupported          : False
StandbySupported       : False
SuspendSupported       : False

 

For interest sake the screen resolution of this monitor is 1366 x 768

We can calculate the approximate monitor size – which is the diagonal size – the max horizontal and vertical image sizes are truncated to the nearest centimeter. I think in inches so I want to convert

function get-monitorsize {
Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams |
select  @{N="Computer"; E={$_.__SERVER}},
InstanceName,
@{N="Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)}},
@{N="Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)}},
@{N="Size";
E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) `
+ [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)}}
}

A little bit of pythagoras theorem and division by 2.54 gives me results I can understand.


June 14, 2011  11:12 AM

Scripting Guy Blog- PowerShell and WMI

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Scripting Guy is running a series of posts this week featuring articles taken from PowerShell books published by Manning (www.manning.com). Yesterday was Don Jones’ PowerShell Lunches, today is my PowerShell and WMI

Read the articles and get the code for a substantial reduction in the price of the books

See http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/14/use-powershell-to-simplify-access-to-wmi-data.aspx

for the article.


June 13, 2011  3:06 PM

Calculated Fields

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You have probably seen something like this many times

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
select @{Name="FreeSpace";Expression={$_.Freespace / 1GB}}

We take an object and perform a calculation to in effect make a new property – a calculated field. Label can be used instead of Name if desired in PowerShell 2

This can also be done in Format-Table

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}}

There some other parameters we can use

We can control the alignment in the field

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Alignment="Left"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Alignment="Center"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Alignment="Right"}

We can format the string

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Format="F2"; Alignment="Left"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Format="F2"; Alignment="Center"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Format="F2"; Alignment="Right"}

And we can control the width of the string

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Width=11; Format="F2"; Alignment="Left"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Width=11; Format="F2"; Alignment="Center"}

Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Format-Table @{Name="FreeSpace";Expression={$_.Freespace / 1GB}; Width=11; Format="F2"; Alignment="Right"}

 

Width, format and alignment don’t work with select-object


June 12, 2011  1:45 PM

PowerShell User Group–21 June 2011

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


When: Tuesday, Jun 21, 2011 7:30 PM (BST)


Where: Virtual

*~*~*~*~*~*~*~*~*~*

Session will look at using PowerShell to automate Microsoft Office – Word, Excel, Visio, Access and more

Notes


Richard Siddaway has invited you to attend an online meeting using Live Meeting.
Join the meeting.
Audio Information
Computer Audio
To use computer audio, you need speakers and microphone, or a headset.
First Time Users:
To save time before the meeting, check your system to make sure it is ready to use Microsoft Office Live Meeting.
Troubleshooting
Unable to join the meeting? Follow these steps:

  1. Copy this address and paste it into your web browser:
    https://www.livemeeting.com/cc/usergroups/join
  2. Copy and paste the required information:
    Meeting ID: Z7FFBT
    Entry Code: TC%f8)D(2
    Location: https://www.livemeeting.com/cc/usergroups

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting can be used to record meetings. By participating in this meeting, you agree that your communications may be monitored or recorded at any time during the meeting.


June 9, 2011  3:14 PM

Date Formats–custom formats

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve added examples of practically all of the custom elements that can be used in a date format

"d ShortDatePattern                                                :  {0} " -f (get-date -Format d )
"D LongDatePattern                                                 :  {0} " -f (get-date -Format D )
"f Full date and time (long date and short time)                   :  {0} " -f (get-date -Format f )
"F FullDateTimePattern (long date and long time)                   :  {0} " -f (get-date -Format F )
"g General (short date and short time)                             :  {0} " -f (get-date -Format g )
"G General (short date and long time)                              :  {0} " -f (get-date -Format G )
"m MonthDayPattern                                                 :  {0} " -f (get-date -Format m )
"M MonthDayPattern                                                 :  {0} " -f (get-date -Format M )
"o Round-trip date/time pattern always uses the invariant culture  :  {0} " -f (get-date -Format o )
"O Round-trip date/time pattern always uses the invariant culture  :  {0} " -f (get-date -Format O )
"r RFC1123Pattern always uses the invariant culture                :  {0} " -f (get-date -Format r )
"R RFC1123Pattern always uses the invariant culture                :  {0} " -f (get-date -Format R )
"s SortableDateTimePattern always uses the invariant culture       :  {0} " -f (get-date -Format s )
"t ShortTimePattern                                                :  {0} " -f (get-date -Format t )
"T LongTimePattern                                                 :  {0} " -f (get-date -Format T )
"u UniversalSortableDateTimePattern                                :  {0} " -f (get-date -Format u )
"U Full date and time – universal time                             :  {0} " -f (get-date -Format u )
"y YearMonthPattern                                                :  {0} " -f (get-date -Format y )
"Y YearMonthPattern                                                :  {0} " -f (get-date -Format Y )

"`nCustom Formats"
"d/M/y                                  :  {0} " -f (get-date -Format d/M/y )
"%d/%M/yy                               :  {0} " -f (get-date -Format d/M/yy )
"dd/MM/yyyy                             :  {0} " -f (get-date -Format dd/MM/yyyyy )
"dd/MM/yyyy %g                          :  {0} " -f (get-date -Format ‘dd/MM/yyyyy %g’)
"dd/MM/yyyy gg                          :  {0} " -f (get-date -Format ‘dd/MM/yyyyy gg’)
"dddd dd/MM/yyyy gg                     :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy gg’)
"dddd dd/MM/yyyy %h:m:s tt gg           :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy %h:m:s tt gg’)
"dddd dd/MM/yyyy hh:mm:s tt gg          :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy hh:mm:s tt gg’)
"dddd dd/MM/yyyy HH:mm:s gg             :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy HH:mm:s gg’)
"dddd dd/MM/yyyy HH:mm:s.ffff gg        :  {0} " -f (get-date -Format ‘dddd dd/MM/yyyyy HH:mm:s.ffff gg’)
"dddd dd MMM yyyy HH:mm:s.ffff gg       :  {0} " -f (get-date -Format ‘dddd dd MMM yyyyy HH:mm:s.ffff gg’)
"dddd dd MMMM yyyy HH:mm:s.ffff gg      :  {0} " -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff gg’)
"dddd dd MMMM yyyy HH:mm:s.ffff zz gg   :  {0} " -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff zz gg’)
"dddd dd MMMM yyyy HH:mm:s.ffff zzz gg  :  {0} " -f (get-date -Format ‘dddd dd MMMM yyyyy HH:mm:s.ffff zzz gg’)

The various custom elements can be combined in numerous other ways but these examples should show you how they all work.


June 8, 2011  1:34 PM

PowerShell and WMI forum

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I will be moderating a new forum for PowerShell on powershell.com  http://powershell.com/cs/forums/217.aspx

The forum is slanted towards PowerShell and WMI but happy to take questions on any aspect of PowerShell


June 6, 2011  1:57 PM

Get-WmiObject property parameter

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This is a fairly obvious piece of PowerShell

Get-WmiObject Win32_Volume

We would probably restrict the properties we displayed like this

Get-WmiObject Win32_Volume | select DriveLetter, DriveType, Freespace

or possibly

Get-WmiObject Win32_Volume |
format-table DriveLetter, DriveType, Freespace –AutoSize

If we are dealing with a large number of remote machines we may want to restrict the properties we return – this is where we use the –property parameter

Get-WmiObject Win32_Volume -property @("DriveLetter", "DriveType", "Freespace")

NOTE THIS STILL RETURNS THE SYSTEM PROPERTIES

so to get a good display we would want to still use format-table (or select)

Get-WmiObject Win32_Volume -property @("DriveLetter", "DriveType", "Freespace") |
format-table DriveLetter, DriveType, Freespace –AutoSize

The –property parameter gives us another opportunity to restrict the data returned to reduce network traffic but we still need to format the returned data


June 3, 2011  12:54 PM

Invoke-WmiMethod parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Invoke-WmiMethod cmdlet was new with PowerShell 2. 

In PowerShell v1 we would do something like this

(gwmi win32_Process -Filter "Name=’Notepad.exe’").Terminate()

With the cmdlet we can do this

gwmi win32_Process -Filter "Name=’Notepad.exe’" |
Invoke-WmiMethod -Name Terminate

This works OK when the method doesn’t need any arguments. When arguments are necessary you may find that using the method directly on the object works BUT it doesn’t if you use Invike-WmiMethod with the –Arguments  parameter.

One possibility is that the cmdlet needs the parameters in a different order.

If you test the arguments needed by a method like this

(gwmi win32_volume -Filter "DriveLetter = ‘c:’" ).Chkdsk.OverLoadDefinitions

You will get the order shown in the documentation

if you use

(gwmi win32_volume -Filter "DriveLetter = ‘c:’" ).GetMethodParameters("Chkdsk")

You may find you get different results for the order of the parameters. Use the order given by this technique with the Invoke-WmiMethod cmdlet


June 3, 2011  5:13 AM

Powers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Many numbers in computing are based on powers of 2. I need to calculate some powers of 2 and realised that PowerShell doesn’t have  an operator for raising  a number  to a power.  In many languages ** or ^ supply this functionality.

In PowerShell we drop back to .NET and use the System.Math class (still think that should be Maths!)

[math]::Pow(2,2) raises 2 to the power 2 and gives us the answer of four.

1..30 | foreach {[math]::Pow(2,$_)}

prints out the first 30 powers of two

Just out of interest 1GB is 2 to the power 30

We can make this a bit prettier

1..30 | foreach {
"{0,3} {1,15}" -f $($_), $([math]::Pow(2,$_))
}

Check the values for 1kb, 1mb and 1gb which are

1024
1048576
1073741824

respectively.

I’ll convert this to a function and add it to the PAM maths module.


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: