PowerShell for Windows Admins


January 26, 2016  11:28 AM

Rescuing IE favourites

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I received the new Windows Insider Windows 10 build over the wekend and have just discovered that installing it wiped out my IE favourites – or at least those in folders.

I’d copied my favourites to Microsoft Edge when installing Windows 10 so I can copy everything back

IE favourites are stored at

Get-ChildItem -Path ‘C:\Users\<user>\Favorites’

Yoy can clean out the favourites:

Get-ChildItem -Path ‘C:\Users\<user>\Favorites’ | Remove-Item –Force

You will be asked to confirm the action.

Microsft Edge favourites are at

Get-ChildItem -Path ‘C:\Users\<user>\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\
User\Default\Favorites’ –Recurse

Copy them into the IE favourites folder

Get-ChildItem -Path ‘C:\Users\<user>\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\MicrosoftEdge\
User\Default\Favorites’ | Copy-Item -Destination ‘C:\Users\<user>\Favorites’ -Force -Recurse

Job done

January 26, 2016  8:21 AM

FQDN

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

How do you find the FQDN of the machine you’re using.  The simplest way is to combine a couple of environmental variables:

PS> “$env:COMPUTERNAME.$env:USERDNSDOMAIN”
SERVER02.MANTICORE.ORG

If you like using CIM (and who doesn’t) you can try this

PS> Get-CimInstance -ClassName Win32_ComputerSystem |
>> select @{N=’FQDN’; E={“$($_.DNSHostName).$($_.Domain)”}}
>>

FQDN
—-
server02.Manticore.org

This could easily be used for remote machines as well by adding the –ComputerName parameter to Get-CimInstance

If you want to go down the .NET route you have:

PS> [System.Net.Dns]::GetHostByName(”).HostName
server02.Manticore.org


January 24, 2016  4:56 AM

CDXML filter parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

I was recently asked about adding a filter parameter to a cmdlet created through CDXML. If you’ve not seen it before (see PowerShell and WMI Chapters 18 & 19 from www.manning.com) CDXML allows you to creat ecmdlets by wrapping a WMI class in some simple XML.

The resultaing CDXML (Cmdlet Definition XML) is thn published as a module.  Here’s a simple example using the Win32_NetworkAdapterConfiguration class

<?xml version=’1.0′ encoding=’utf-8′?>
<PowerShellMetadata xmlns=’http://schemas.microsoft.com/cmdlets-over-objects/2009/11′>
<Class ClassName=’ROOT\cimv2\Win32_NetworkAdapterConfiguration’>
<Version>1.0</Version>
<DefaultNoun>NetworkAdapterConfiguration</DefaultNoun>
<InstanceCmdlets>
<GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>
</GetCmdletParameters>
</InstanceCmdlets>
</Class>
</PowerShellMetadata>

The first 2 lines are boilerplate. The NameSpace and WMI class are defined on line 3, follwoed by a version number (arbitary) and a default noun for you cmdlet to use.  Instance cmdlets defines how you’ll pull the data for existing instances of the class – in other words the Get-NetworkAdapterConfiguration cmdlet.

Save as a CDXML file and import as a module

Import-Module .\NetworkAdapterConfiguration.cdxml

Get-Module will sjow it as a Cim module with a single exported command.   Use it like any other cmdlet

PS> Get-NetworkAdapterConfiguration | ft -a

ServiceName  DHCPEnabled Index Description
———–  ———– —– ———–
kdnic        True        0     Microsoft Kernel Debug Network Adapter
mwlu97w8     True        1     Marvell AVASTAR Wireless Composite Device
msu64w8      False       2     Surface Ethernet Adapter
mwlu97w8     True        3     Marvell AVASTAR 350N Wireless Network Controller
RFCOMM       False       4     Bluetooth Device (RFCOMM Protocol TDI)
BthPan       True        5     Bluetooth Device (Personal Area Network)
vwifimp      True        6     Microsoft Wi-Fi Direct Virtual Adapter
vwifimp      True        7     Microsoft Wi-Fi Direct Virtual Adapter
RasSstp      False       8     WAN Miniport (SSTP)
RasAgileVpn  False       9     WAN Miniport (IKEv2)
Rasl2tp      False       10    WAN Miniport (L2TP)
PptpMiniport False       11    WAN Miniport (PPTP)
RasPppoe     False       12    WAN Miniport (PPPOE)
NdisWan      False       13    WAN Miniport (IP)
NdisWan      False       14    WAN Miniport (IPv6)
NdisWan      False       15    WAN Miniport (Network Monitor)

Using the cmdlet is equivalent to

Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration

but is easier and requires less typing.

Very often you’ll want to pick a specific adapter – for instance

Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter ‘Index=3′

You can implement the same kind of filters using CDXML. You add a queryable properties section as shown below:

<?xml version=’1.0′ encoding=’utf-8′?>
<PowerShellMetadata xmlns=’http://schemas.microsoft.com/cmdlets-over-objects/2009/11′>
<Class ClassName=’ROOT\cimv2\Win32_NetworkAdapterConfiguration’>
<Version>1.0</Version>
<DefaultNoun>NetworkAdapterConfiguration</DefaultNoun>
<InstanceCmdlets>
<GetCmdletParameters DefaultCmdletParameterSet=’DefaultSet’>

<QueryableProperties>
<Property PropertyName=’Index’>
<Type PSType =’UInt32’/>
<RegularQuery AllowGlobbing=’true’>
<CmdletParameterMetadata PSName=’Index’  ValueFromPipelineByPropertyName=’true’ CmdletParameterSets=’DefaultSet’ />
</RegularQuery>
</Property>
</QueryableProperties>

</GetCmdletParameters>
</InstanceCmdlets>
</Class>
</PowerShellMetadata>

Set the paraemter name – same as property to use here – and the type (unsigned integer). Decide whether pipeline input and wildcards (globbing) are allowed and save the file.

Re-import the module (use the Force) and your new parameter is available

Get-NetworkAdapterConfiguration -Index 3

Its important to understand CDXML – even if you never create a CDXML module – because 2/3 of the cmdlets in Windows Server 2012 and later are created this way.


January 20, 2016  4:37 AM

WMF 4.0 Updates available downlevel

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The WMF 4.0 Updates that were released in November 2014 for Windows 8.1 and Windows Server 2012 R2 are now available for:

Windows Server 2012

Windows Server 2008 R2 SP1

Windows 7 SP1

You need WMF 4.0 installed to install the update

Details from http://blogs.msdn.com/b/powershell/archive/2016/01/19/windows-management-framework-wmf-4-0-update-now-available-for-windows-server-2012-windows-server-2008-r2-sp1-and-windows-7-sp1.aspx


January 18, 2016  1:44 PM

Setting external time source in AD

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

The PDC emaulator in the root domain of your AD forest should point to an external time source. For some odd reason the PDC emulator in my lab wasn’t doing that. Easily remedied:
##
## set external time source
## set server type to NTP
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -Name Type -Value ‘NTP’
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name AnnounceFlags -Value 5
## Enable NTP server
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer -Name Enabled -Value 1
## Specify Time source
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -Name NtpServer -Value ‘time.windows.com,0x1’
## Set poll interval in seconds – every 30 minutes
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient -Name SpecialPollInterval -Value 1800
## set max +/- time corrections in seconds – 24 hours
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name MaxPosPhaseCorrection -Value 86400
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name MaxnegPhaseCorrection -Value 86400

Stop-Service -Name W32Time
Start-Service -Name W32Time

Wait a minute or so and the time will be set correctly.

Not a job you have to do very often but having the code to do the job reduces the probability of errors

You can view the settings

Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters
Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config
Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer
Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient


January 17, 2016  2:27 PM

WMF 5.0

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell
If you follow the PowerShell Team blog – http://blogs.msdn.com/b/powershell/ (and if you don’t you should) you’ll know that the WMF 5.0 RTM downloads were pulled just before Christmas. This was due to a bug that reset the module environment.
A comment on the blog – http://blogs.msdn.com/b/powershell/archive/2015/12/23/windows-management-framework-wmf-5-0-currently-removed-from-download-center.aspx – from the team indicates that it’s likely to be a few weeks before the bug is resolved and WMF 5.0 is available again for download.


January 16, 2016  8:40 AM

PowerShell conferences 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

April is going to be a very busy month. It starts with the PowerShell plus DevOps Global Summit in Bellevue, Washington – https://eventloom.com/event/login/PSNA16

The PowerShell Conference EU 2016 follows later in the month – http://www.psconf.eu/

I’ll be speaking and running pre-conference workshops at both events. Hope to see you at one or the other.


January 12, 2016  4:45 AM

European PowerShell conference 2016

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

PowerShell Conference EU combines the former “Deutsche PowerShell Konferenz” and “PowerShell Summit EU” into one great big 3-day PowerShell event for Admins and DevOps in Europe and takes place April 20-22 in Hannover/Germany. With more than 40 international speakers including PowerShell inventor Jeffrey Snover, and more than 60 sessions, you are cordially invited to join this massive European PowerShell event. The agenda is up on www.psconf.eu, and registration is open. Seats and hotel capacity are limited so don’t wait and register!

Psconf.eu is partnering with powershell.org and psconf.asia, and together we want to ensure that you have a comprehensive local PowerShell conference in your region of the


January 11, 2016  10:34 AM

Testing against an arrays contents

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

You may need to test if a value is a member of an array. PowerShell provides 2 operators for testing array membership  – –   -in and –contains.

Simple usage is like this

PS> $colours = ‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘violet’

PS> ‘blue’ -in $colours
True
PS> $colours -contains ‘blue’
True

Note the order of value and array changes between the operators.

You  can also use these operators in Where-Object

PS> $testcolours = ‘blue’, ‘pink’, ‘yellow’

PS> $testcolours | where {$_ -in $colours}
blue
yellow
PS> $testcolours | where {$colours -contains $_}
blue
yellow

Often the value we want is a property of an object

PS> $to = New-Object -TypeName PSObject -Property @{Colour = ‘green’}
PS> $to1 = New-Object -TypeName PSObject -Property @{Colour = ‘pink’}

PS> $to, $to1 | where Colour -in $colours

Colour
——
green
PS> $to, $to1 | where {$colours -contains $_.Colour}

Colour
——
green

The –in operator can be used in the simplified Where-Object syntax (it was introduced for that purpose) but –contains has to use the full, original syntax

For testing non-membership you also get –notin and –notcontains

PS> $to, $to1 | where Colour -notin $colours

Colour
——
pink
PS> $to, $to1 | where {$colours -notcontains $_.Colour}

Colour
——
pink


January 8, 2016  1:49 PM

WMI discovery

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

I’ve been working with System Center Configuration Manager this week. SCCM uses a lot of WMI. However, WMI discovery is still a bit of an art.

I needed to find the SMS_SCI_Component class.

If you know the namespace you can easily find the classes in that namespace but namespaces are hierarchical so you need to work down the tree.

The root namespace is at the top of the namespace tree so

C:\Scripts> Get-CimInstance -ClassName __NAMESPACE -Namespace root

Name
—-
subscription
DEFAULT
SCCMDP
CIMV2
msdtc
Cli
nap
MicrosoftIISv2
SECURITY
CCMVDI
NetworkModel
RSOP
SMS
ccm
StandardCimv2
WMI
AccessLogging
directory
Policy
InventoryLogging
Interop
Hardware
ServiceModel
Microsoft
aspnet

After a bit of digging I found I needed the sms namespace

PS C:\Scripts> Get-CimInstance -ClassName __NAMESPACE -Namespace ‘root\sms’

Name
—-
site_PS1

which leads to:

Get-CimInstance -ClassName SMS_SCI_Component -Namespace ‘root\sms\site_PS1’

 

What if you only know the class?

There’s a little trick using Get-WmiObject

Get-WmiObject -Namespace root -Recurse -List -Class SMS_SCI_Component
NameSpace: ROOT\SMS\site_PS1

Name
—-
SMS_SCI_Component

The trick is to start with the root namespace and use the –Recurse (search through all child namespaces) and –List (just give class details) parameters.  You’ll see a listing by namespace for each instance of the class discovered.


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: