PowerShell for Windows Admins


February 22, 2016  11:18 AM

IP Default Gateways by cmdlet

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Network Adapter, Powershell

Following my recent post on setting the default gateway by using the Win32_NetworkAdapterConfiguration CIM class here’s how you do it using the networking cmdlets

Discover your adapters

Get-NetAdapter

Check the default gateway for an adapter

Get-NetIPConfiguration -InterfaceIndex 12 | select InterfaceIndex, IPv4Address, IPv4DefaultGateway

Set the default gateway

New-NetRoute -InterfaceIndex 12  -DestinationPrefix ‘0.0.0.0/0’ -NextHop ‘10.10.54.1’

This isn’t as intuitive as using the CIM class

Check the setting

£> Get-NetIPConfiguration -InterfaceIndex 12
InterfaceAlias       : Ethernet
InterfaceIndex       : 12
InterfaceDescription : Microsoft Hyper-V Network Adapter
NetProfile.Name      : Manticore.org
IPv4Address          : 10.10.54.100
IPv6DefaultGateway   :
IPv4DefaultGateway   : 10.10.54.1
DNSServer            : 10.10.54.201

To remove the default gateway

Remove-NetRoute -InterfaceIndex 12 -NextHop ‘10.10.54.1’ -Confirm:$false

If you leave off –Confirm you’ll be prompted to confirm the action on the active and persistent stores i.e. twice.

All of the above cmdlets are part of the NetTCPIP module available on Windows 8/Server 2012 and later.

 

February 22, 2016  9:34 AM

Quick test for updates that aren’t installed

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I want a quick test to determine if any of the updates I’d approved hadn’t been installed on all of the relevant machines.

Get-WsusUpdate -Approval Approved | where InstalledOrNotApplicablePercentage -ne 100

Get the list of approved updates and filter on the InstalledOrNotApplicablePercentage

If anything does show up you can dig further to determine what hasn’t installed where


February 21, 2016  7:39 AM

IP Default Gateways

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Network Adapter, Powershell

When you configure the IP Address on a network adapter you also have to set the default gateway if you want the machine to communicate with machines on other subnets.

One way you can do this is through the SetGateways method of Win32_NetworkAdapterConfiguration

$class = Get-CimClass -ClassName Win32_NetworkAdapterConfiguration $class.CimClassMethods[‘SetGateWays’].Parameters

Name                  CimType Qualifiers                         ReferenceClassName
—-                  ——- ———-                         ——————
DefaultIPGateway  StringArray {ID, In, MappingStrings}
GatewayCostMetric UInt16Array {ID, In, MappingStrings, Optional}

Notice you need to supply the information as arrays so:

$dgw = @(‘10.10.54.1’)
$gcm = @([uint16]2)

The default for the metric is 1. The metric is supplied as an unsigned 16 bit integer

Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter “Index=2”  | Invoke-CimMethod –MethodName  SetGateWays -Arguments @{DefaultIPGateway = $dgw; GatewayCostMetric = $gcm}

To change the gateway simply overwrite the values via another call to SetGateways()

To completely remove the gateway information you need to set the value of the gateway to be the same as the IP address on the adapter

$dgw = @(‘10.10.54.200’)
$gcm = @([uint16]1)
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter “Index=2”  | Invoke-CimMethod –MethodName  SetGateWays -Arguments @{DefaultIPGateway = $dgw; GatewayCostMetric = $gcm}

Leaving the metric as 1 will cause no harm

Next time I’ll show how to perform the same actions with the networking cmdlets


February 15, 2016  10:50 AM

PowerShell Conference EU 2016–things to look forward to

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

With 64 days to the European PowerShell conference (and less to the PowerShell Summit) I’m busy getting my sessions prepared.

At the European conference I’m presenting pre-conference workshops on DSC and CIM and then two sessions on managing the new Container feature in Server 2016 with PowerShell.

As i’m not organising this one I get to enjoy the conference.

There’s a number of sessions I’m really lookign forward to:

The sessions by Jeffery Snover and Bruce Payette haven’t been announced yet but they are bound to be good. Jeffery is a great speaker and Bruce knows so much about the inner workings of PowerShell that you always learn something new.

other sessions I’m lookign forward to include:

DSC resource creation by Ben Gelens

PowerShell and SQL Server: Journey to 200,000 rows per second by Chrissie LeMaire. if you ever wanted to discover how really optimise a process this session is for you

Test-driven developement with Pester by June Blender

If you’re looking for a PowerShell conference in Europe – this is the one where you’ll find great content. There’ll be a bunch of PowerShell MVPs there including

Tobias Weltner

Ravikanth Chaganti

Jeff Wouters

Steven Murawski

and the speakers of the sessions I mentioned earlier.  if you can’t get your PowerShell questions answered at the conference then there isn’t an answer


February 10, 2016  8:59 AM

PowerShell articles – – February 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I’ve had 2 articles published this week.

The first one is on the Scripting Guy blog

https://blogs.technet.microsoft.com/heyscriptingguy/2016/02/08/should-i-use-cim-or-wmi-with-windows-powershell/

And looks at the WMI and CIM cmdlets showing why 99.99% you should be using the CIM cmdlets.

The second article is on the UK TechNet blog

http://blogs.technet.com/b/uktechnet/archive/2016/02/10/powershell-and-server-core.aspx

and discusses how to configure and administer a Windows Server Core instance. All of the techniques in the article use built in cmdlets – no scripting required


February 8, 2016  1:59 PM

WMF 5.0 download latest news

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

According to the PowerShell Team blog – https://blogs.msdn.microsoft.com/powershell/2015/12/23/windows-management-framework-wmf-5-0-currently-removed-from-download-center/

The issue has been resolved and a WMF 5.0 RTM build should be available around the end of February


February 6, 2016  6:11 AM

Not the comma!

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

There is a habit among some AD administrators to create their users so that the name is surname, firstname   – Note the comma between the two names. As an example the name would be

Brown, Bill

instaead of

Bill Brown

If you’re just using the GUI tools it doesn’t matter too much and has the arguable advantage of ordering the users by surname. But when it comes to scripting against AD this practice is a complete pain.

Compare these 2 distinguished names

CN=Brown, Bill,OU=Testing,DC=Manticore,DC=org

CN=Dave Green,OU=Testing,DC=Manticore,DC=org

Notice the extra comma in the first one. That destroys any chance of splitting the distinguished name on commas – which are the element separators in distinguished names.

You have to escape the comma in the name with a \

The GUI tools (at least in Windows server 2012 R2) do this for you so the distinguished name looks like this:

CN=Brown\, Bill,OU=Testing,DC=Manticore,DC=org

If you want to get a user by distinguished name this will work:

Get-ADUser -Identity ‘CN=Dave Green,OU=Testing,DC=Manticore,DC=org’

This won’t

Get-ADUser -Identity ‘CN=Brown, Bill,OU=Testing,DC=Manticore,DC=org’

You have to use the escaped version:

Get-ADUser -Identity ‘CN=Brown\, Bill,OU=Testing,DC=Manticore,DC=org’

In my last post I showed how to extract the users OU from the distinguished name

Get-ADUser -Filter * -Properties DisplayName |
select Name, DisplayName, UserPrincipalname, @{N= “Organanisational Unit” ;
E = {($_.DistinguishedName -split ‘,’, 2)[1]}}

That code breaks down if you have a comma in the name and you get

Bill,OU=Testing,DC=Manticore,DC=org

for the OU instead of

OU=Testing,DC=Manticore,DC=org

Its probably possible to do some regex voodoo to deal with this but as the Universe doesn’t have enough life left in it for me to figure this out I’ll resort to a brute force approach:

Get-ADUser -Filter * -Properties DisplayName |
foreach {
$ouf = ($_.DistinguishedName -split ‘,’, 2)[1]
if (-not ($ouf.StartsWith(‘OU’) -or $ouf.StartsWith(‘CN’) )){
$ou = ($ouf -split ‘,’, 2)[1]
}
else {
$ou = $ouf
}
$psitem | select Name, DisplayName, UserPrincipalname, @{N= “Organanizational Unit” ;E = {$ou}}
}

Do the inital split as previously but then test the reasults to see if it starts with CN= or OU=. If it doesn’t then split again.

Its not elegant but it works.

It sa lot easier if you don’t use the comma in the first place Smile

 


February 4, 2016  7:47 AM

Some thoughts on finding a users OU

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

Back in this post http://itknowledgeexchange.techtarget.com/powershell/discovering-a-users-ou/

I showed how to get the OU of a user from the distinguished name of the user object. if you want to display that as part of your output you can create a calculated field

Get-ADUser -Filter * -Properties DisplayName |
select Name, DisplayName, UserPrincipalname, @{N= “Organanisational Unit” ;
E = {($_.DistinguishedName -split ‘,’, 2)[1]}}

In your select statement take the Distinguishedname and split it on the comma – make sure you split it into 2 parts – the second is the OU

Don’t rely on the Displayname alone as its not present for some built in accounts such as administrator

 


February 2, 2016  8:15 AM

AD Management MoL Deal – – 3 February 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Books, Powershell

My Learn Active Directory Management in a Month of Lunches will be part of Manning’s Deal of the Day on 3 February 2016.

Half off my book Learn Active Directory Management in a Month of Lunches. Use code dotd020316au at https://www.manning.com/books/learn-active-directory-management-in-a-month-of-lunches

As usual the deal starts at midnight US ET and is usually active for about 48 hours

 


February 1, 2016  4:41 AM

PowerShell Summit 2016 – – 3 day registration open

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Three day registration is now open

https://eventloom.com/event/login/PSNA16


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: