PowerShell for Windows Admins


September 9, 2014  1:22 PM

OneNote and XML–finding sections

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
office 2013, Powershell

I recently showed how to find the names of your OneNote notebooks.  The next level down is the section.  You can find these sections in a notebook like this:

 

$onenote = New-Object -ComObject OneNote.Application
$scope = [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsPages
[ref]$xml = ”

 

$onenote.GetHierarchy($null, $scope, $xml)

 

$schema = @{one=”http://schemas.microsoft.com/office/onenote/2013/onenote”}

 

$xpath = “//one:Notebook/one:Section”
Select-Xml -Xml ($xml.Value) -Namespace $schema -XPath $xpath |
foreach {
$node = $psitem.Node

$npath = Split-Path -Path $node.Path -Parent

$props = [ordered]@{
Workbook =  Split-Path -Path $npath -Leaf
Section = $node.Name
}
New-Object -TypeName PSObject -Property $props
}

 

The first part of the script where the application object is created, the scope set and you get the hierarchy is the same as before.  The two scripts diverge when you get to the Xpath you’re going to use.  To find the notebooks you used:

 

$xpath = “//one:Notebook”

 

which means get me any Nodes called one:Notebook

 

To find the section you use:

 

$xpath = “//one:Notebook/one:Section”

 

which means any nodes called one;Section that are children of a one:Notebook node.

 

Remember XML is case sensitive.

 

Once you have the section nodes – which look like this:

 

name             : Quick Notes
ID               : {9EFAE9AC-0388-424A-8211-02E8FFE50666}{1}{B0}
path             : https://d.docs.live.net/43cfa46a74cf3e96/Documents/Personal (Web)/Quick Notes.one
lastModifiedTime : 2014-09-04T17:48:07.000Z
color            : #B7C997
Page             : {OneNote: one place for all of your notes, OneNote Basics}

 

You can extract the data you want. The path property can be used to extract the name of the note book with a little bit of Split-Path magic.

 

Next time you’ll see how to get down to the individual pages

September 8, 2014  1:36 PM

CimInstanceProperties

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, WMI

If you use Get-CimInstance and examine the object returnd – you’ll find some meta-data at the end of the object:

CimClass                                  : root/cimv2:Win32_OperatingSystem
CimInstanceProperties                     : {Caption, Description, InstallDate, Name…}
CimSystemProperties                       : Microsoft.Management.Infrastructure.CimSystemProperties

 

Expand CimInstanceProperties

 

£> Get-CimInstance Win32_OperatingSystem | select -ExpandProperty CimInstanceProperties | fl *

 

And you’ll see an entry like this for each property
Name            : Caption
Value           : Microsoft Windows 8.1 Pro
CimType         : String
Flags           : Property, ReadOnly, NotModified
IsValueModified : False

 

The CimType and Flags attributes are especially useful

 

For instance you can use them to discover which properties can be modified:

 

Get-CimInstance Win32_OperatingSystem |
select -ExpandProperty CimInstanceProperties |
foreach {
$flags =   ($psitem | select -ExpandProperty Flags)  -split “, ”

if ($flags -notcontains ‘ReadOnly’){$psitem}
}

 

Remember that other properties may be modifiable via a method on the class.

 

You can find similar information via Get-CimClass

 

$class = Get-CimClass -ClassName Win32_OperatingSystem

$class.CimClassProperties |
foreach {
$flags =   ($psitem | select -ExpandProperty Flags)  -split “, ”

if ($flags -notcontains ‘ReadOnly’){$psitem}
}

 

Get-CimClass gives you the qualifiers as well as the flags. Get-CimInstance gives you the value of the property if set.

Two options with slightly different results. All the better to investigate CIM classes


September 7, 2014  5:42 AM

Authorising a DHCP server and creating a scope

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DHCP, Windows 2012

I recently re-built my test lab and now need to authorise the DHCP server.

You can test the DHCP server:

£> Get-DhcpServerSetting
IsDomainJoined            : True
IsAuthorized              : False
DynamicBootp              : True
RestoreStatus             : False
ConflictDetectionAttempts : 0
NpsUnreachableAction      : Full
NapEnabled                : False
ActivatePolicies          : True

Authorisation is straightforward

Add-DhcpServerInDC -DnsName server02.manticore.org -IPAddress 10.10.54.201

If you only have a single NIC in your DHCP server you don’t need to worry about the IP address

£> Get-DhcpServerSetting
IsDomainJoined            : True
IsAuthorized              : True
DynamicBootp              : True
RestoreStatus             : False
ConflictDetectionAttempts : 0
NpsUnreachableAction      : Full
NapEnabled                : False
ActivatePolicies          : True

Create a scope and activate it

Add-DhcpServerv4Scope -ComputerName server02 -Name “Manticore Scope”  -StartRange 10.10.54.2 -EndRange 10.10.54.30 -Description “Scope for Manticore domain” -Type DHCP -State Active -SubnetMask 255.255.255.0 -LeaseDuration (New-TimeSpan -Days 2)

ComputerName is the name of the DHCP server otherwise the parameters are self explanatory

To view all scopes

Get-DhcpServerv4Scope -ComputerName server02

To view specific scope

Get-DhcpServerv4Scope -ComputerName server02 -ScopeId 10.10.54.0 | fl

ScopeId          : 10.10.54.0
Name             : Manticore Scope
Description      : Scope for Manticore domain
SuperscopeName   :
SubnetMask       : 255.255.255.0
StartRange       : 10.10.54.2
EndRange         : 10.10.54.30
LeaseDuration    : 2.00:00:00
NapProfile       :
NapEnable        : False
Delay(ms)        : 0
State            : Active
Type             : Dhcp
MaxBootpClients  : 4294967295
ActivatePolicies : True


September 6, 2014  2:57 AM

LAST CALL for European PowerShell Summit 2014

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Security

This is the last call for attendee registration for the European PowerShell Summit 2014.

The Summit is in Amsterdam – 29 September to 1 October 2014. Details from the events page http://powershell.org/wp/community-events/summit/.

 

Due to a change in circumstances beyond our control  we have to close public registration on 10 September 2014.

 

If you contact us by 10 September and ask to be able to perform a funds transfer rather than paying on line you have until 15 September 2014 to complete that transaction. No monies or registrations will be accepted after 15 September. We will not accept any new request for paying by money transfer after 10 September.

 

Apologies for the change in dates (the web site states registration is open until 15 September) but our hands have been forced on this.

 

There are still a number of places available so please register quickly if you want to attend.  The more attendees we have the better chance we have of staging a European PowerShell Summit in 2015.


September 5, 2014  3:03 PM

Finding a file version

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
WMI

Interesting question on the forum – how to find the file version of IE on remote machines?

 

Get-CimInstance -ClassName CIM_DataFile -Filter “Name = ‘C:\\Program Files\\Internet Explorer\\iexplore.exe'”  | select -ExpandProperty Version

 

Use the CIM_dataFile class.  Its one of the few CIM_ classes that doesn’t have a Win32_ equivalent.

 

In this case you know the path to the file – note the \\ as you have to escape a single \ in WMI filters


September 5, 2014  1:48 AM

WMF 5.0 September 2014 preview now available

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Security

Another preview for WMF 5.0, and PowerShell 5.0, is available for download.  Details from

 

http://blogs.msdn.com/b/powershell/archive/2014/09/04/windows-management-framework-5-0-preview-september-2014-is-now-available.aspx


September 4, 2014  1:07 PM

OneNote and XML–finding notebooks

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

When OneNote first came out there wasn’t an API for it as you get for Word or Excel. A community module enabled you to work with the XML that formed the OneNote note books but it wasn’t updated after Office 2007 and doesn’t really work with later versions of OneNote.

 

I was looking at the Office developer site and noticed that there was some information on OneNote http://msdn.microsoft.com/en-us/library/office/jj680118(v=office.15).aspx.  This series will investigate how to script against OneNote and also expalin how to use Select-XML and XPath on the way.

 

A good starting point would be to discover the OneNote notebooks:

$onenote = New-Object -ComObject OneNote.Application
$scope = [Microsoft.Office.Interop.OneNote.HierarchyScope]::hsPages
[ref]$xml = ”

$onenote.GetHierarchy($null, $scope, $xml)

$schema = @{one=”http://schemas.microsoft.com/office/onenote/2013/onenote”}
$xpath = “//one:Notebook”
Select-Xml -Xml ($xml.Value) -Namespace $schema -XPath $xpath |
foreach {
$psitem.Node.Name
}

 

The starting point – like all good Office applications – is a COM object that exposes the OneNote object model.  As an aside isn’t time office moved away from COM and we had a proper .NET API or even better a PowerShell module.

 

You also need to define the scope – in this case get all pages. The enumeration is described here http://msdn.microsoft.com/en-us/library/office/jj680119(v=office.15).aspx

 

You also need to create a [ref] object to hold the output

 

The GetHierarchy() method is used to read through the notebooks. The $null argument means start at the top

 

The schema can be found inside the XML produced so to avoid a circular argument I’ll set that in a variable – it has to be a hash table as shown

 

Define the XPath – in this case get the nodes labelled one:Notebook

 

Select-XML will extract the required nodes – notice how the object has to be presented.

 

A simple foreacch iterates over the nodes which look like this

name             : Personal (Web)
nickname         : Personal (Web)
ID               : {F8CC78D5-9CC3-40C8-847B-96B15E3D6AD2}{1}{B0}
path             :
https://<a path>  (Web)/
lastModifiedTime : 2014-09-04T17:48:07.000Z
color            : #FFD869
Section          : {Quick Notes, Unfiled Notes, PowerShell Summit}
SectionGroup     : SectionGroup

 

And you can select the name of the notebook.


August 29, 2014  2:29 PM

Grains of rice on a chess board

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

There is a story about the inventor of chess being rewarded by putting 1 grain of rice on the first square of the board; 2 on the second and so on.  How much rice does that come to?

 

The total number of grains is 1.84467440737096E+19

 

At 25mg per grain thats 461168601842739 kilogrammes of rice

 

Which in more understandable terms is:

 

461,168,601,842.739 metric tonnes

 

or
453,886,749,619.642 tons

 

That’s a lot of rice

 

If you want to play around with the numbers the calculations are:

 

[double]$result = 0

1..64 |
foreach {

$square = [math]::Pow(2, ($psitem -1))
$result += $square

}

$wt = 0.025

$totalweight = ($wt * $result)/1000
$totalweight

$mtwt = $totalweight /1000
$mtwt

$tons = $totalweight * 0.00098421
$tons


August 29, 2014  1:00 PM

Event Log Providers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

An event log provider is writes to an event log.  I’ve used WMI in the past to get these but while looking for somethign else discovered that Get-WinEvent can also find this information

 

Get-WinEvent -ListProvider * | ft Name, LogLinks -AutoSize –Wrap

 

Provides a nice long list of all of the providers and the event logs they write to.

 

Usually I’m only interested in what’s writing to a particular event log. And that’s where things get a bit more messy.

 

The loglinks are supplied as a System.Collections.Generic.IList[System.Diagnostics.Eventing.Reader.EventLogLink] LogLinks  object that doesn’t play nicely with –in or –contains

 

So we need a bit of PowerShell manipulation to get what we want

 

$log = ‘System’

Get-WinEvent -ListProvider * |
foreach {

if ($log -in ($psitem | select -ExpandProperty Loglinks | select -ExpandProperty Logname)){
New-Object -TypeName psobject -Property @{
Name = $psitem.Name
Log = $log
}
}
}

 

The trick here is that the loglinks are a collection of objects so you need to expand them twice to get to the name.  Not pretty but it works


August 28, 2014  12:56 PM

Patching Server Core

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve been rebuilding my test lab after installing a SSD into the machine running it in place of the SATA drive.  Huge improvement in load speed of virtual machines – well worth the cost.

I usually have a number of server core machines in the lab and use WSUS for patching.  One issue I’d never really resolved was patching those server core machines  – the control panel fro Windows Update isn’t available!

Finally found a solution in the Windows Update PowerShell module from

http://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc

Install the module and then you can install your patches using

Get-WUInstall –AcceptAll

I’m running Windows 2012 R2 on all my servers so the modules auto load


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: