PowerShell for Windows Admins


June 1, 2012  1:07 PM

Working with profiles: part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

A question came up on the forum for PowerShell and WMI – how do I delete profiles. I’m going to work up to answering that by looking at using WMI to work with profiles.

So to start how can we find the profiles available on our system

Get-WmiObject -Class Win32_UserProfile |
select LocalPath, SID, @{N="LastUseTime"; E={$_.ConvertToDateTime($_.LastUseTime)}}

LocalPath                   SID                         LastUseTime              
———                   —                         ———–              
C:\Users\Richard            S-1-5-21-2542198769-1191… 01/06/2012 19:56:28      
C:\Windows\ServiceProfil… S-1-5-20                                             
C:\Windows\ServiceProfil… S-1-5-19                                             
C:\Windows\system32\conf… S-1-5-18        

or if you prefer the CIM cmdlets in PowerShell v3

Get-CimInstance -ClassName Win32_UserProfile |
select LocalPath, SID, LastUseTime

C:\Users\Richard            S-1-5-21-2542198769-1191… 01/06/2012 19:56:28      
C:\Windows\ServiceProfil… S-1-5-20                                             
C:\Windows\ServiceProfil… S-1-5-19                                             
C:\Windows\system32\conf… S-1-5-18
   

Notice that with the CIM cmdlet we don’t have to perform any date conversions – worth switching just for that alone.

But the data above doesn’t show the user account.

Unfortunately there isn’t an association between profile and user account so we need to do the filtering ourselves

Get-WmiObject -Class Win32_UserProfile |
select LocalPath, SID,
@{N="LastUseTime"; E={$_.ConvertToDateTime($_.LastUseTime)}},
@{N="User"; E={Get-WmiObject -Class Win32_UserAccount -Filter "SID = ‘$($_.SID)’" | select -ExpandProperty Caption}}

LocalPath            SID                  LastUseTime          User              
———            —                  ———–          —-              
C:\Users\Richard     S-1-5-21-25421987… 01/06/2012 20:01:56  RSLAPTOP01\Richard
C:\Windows\Servic… S-1-5-20                                                    
C:\Windows\Servic… S-1-5-19                                                    
C:\Windows\system… S-1-5-18   

The alternative with the CIM cmdlets

Get-CimInstance -ClassName Win32_UserProfile |
select LocalPath, SID, LastUseTime,
@{N="User"; E={Get-CimInstance -Class Win32_UserAccount -Filter "SID = ‘$($_.SID)’" |
select -ExpandProperty Caption}}

C:\Users\Richard     S-1-5-21-25421987… 01/06/2012 20:01:56  RSLAPTOP01\Richard
C:\Windows\Servic… S-1-5-20                                                    
C:\Windows\Servic… S-1-5-19                                                    
C:\Windows\system… S-1-5-18  

The final part is to filter out any of the well known special accounts such as Local service

Get-WmiObject -Class Win32_UserProfile -Filter "Special = ‘$false’" |
select LocalPath, SID,
@{N="LastUseTime"; E={$_.ConvertToDateTime($_.LastUseTime)}},
@{N="User"; E={Get-WmiObject -Class Win32_UserAccount -Filter "SID = ‘$($_.SID)’" | select -ExpandProperty Caption}}

or

Get-CimInstance -ClassName Win32_UserProfile -Filter "Special = ‘$false’" |
select LocalPath, SID, LastUseTime,
@{N="User"; E={Get-CimInstance -Class Win32_UserAccount -Filter "SID = ‘$($_.SID)’" | select -ExpandProperty Caption}}

either of these will just return the top line in the output above.

Now we can identify our profiles & relate them to user accounts – how do we delete them

May 30, 2012  1:03 PM

May 2012 Powershell group recording and slides

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

The recording, slides and demo scripts from last nights session  – Powershell in Windows Server 8 part 2  – is available from

https://skydrive.live.com/?cid=43cfa46a74cf3e96#cid=43CFA46A74CF3E96&id=43CFA46A74CF3E96%212966

The full set of historical recordings can be found here:

https://skydrive.live.com/#cid=43CFA46A74CF3E96&id=43CFA46A74CF3E96%212469

2011 09 PowerShell remoting and end point customisation
2011 11 Whats new in PowerCLI 5
2011 12 Intro to WMI
2011 12 WSMAN_WMI_and_CIM
2012 January PowerShell v3 CTP 2 overview
2012 February PowerShell and SQL Server
2012 March CIM cmdlets
2012 April Powershell in Windows Server 8
2012 May Powershell in Windows Server 8 part 2 

Older folders just have the slides


May 27, 2012  10:56 AM

May UG meeting reminder

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Quick reminder that the UK PowerShell group session on PowerShell in Windows 2012 is on Tuesday 29 May. Details from

http://msmvps.com/blogs/richardsiddaway/archive/2012/05/08/uk-powershell-group-may-2012.aspx


May 25, 2012  11:58 AM

Working with WMI methods

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Many WMI classes have methods. Methods allow us to perform some action on the object. A recent question on the forum about using methods made me realise that there are still a lot of people following the old VBScript way of doing things.

We will experiment with the BITS service as it is safe for these purposes.

PS> Get-WmiObject -Class Win32_Service -Filter "Name = ‘BITS’"

ExitCode  : 0
Name      : BITS
ProcessId : 928
StartMode : Auto
State     : Running
Status    : OK

Lets have a look at the service and its methods

$service = Get-WmiObject -Class Win32_Service -Filter "Name = ‘BITS’"

$service | Get-Member -MemberType method

TypeName: System.Management.ManagementObject#root\cimv2\Win32_Service

Name
—-
Change
ChangeStartMode
Delete
GetSecurityDescriptor
InterrogateService
PauseService
ResumeService
SetSecurityDescriptor
StartService
StopService
UserControlService

So we see methods to stop and start the service

$service.StopService()

PS> Get-Service BITS

Status   Name               DisplayName
——   —-               ———–
Stopped  BITS               Background Intelligent Transfer Ser…

$service.StartService()

PS> Get-Service BITS

Status   Name               DisplayName
——   —-               ———–
Running  BITS               Background Intelligent Transfer Ser…

The person asking the question was trying to use the InvokeMethod method

$service.InvokeMethod(‘StopService’,$Null)

and wondering why it was failing. If you look back to the list of methods above you won’t see InvokeMethod. That’s because it is on the base object. PowerShell, in many instances, doesn’t return the pure .NET object. There are methods and properties added or removed to create the object we normally see.

We can get back to the base object

$service.psbase | gm

And in that list you will see a method called InvokeMethod

PS> $service.psbase.InvokeMethod

OverloadDefinitions
——————-
System.Object InvokeMethod(string methodName, System.Object[] args)
void InvokeMethod(System.Management.ManagementOperationObserver watcher, string methodName, System.Object[] args)
System.Management.ManagementBaseObject InvokeMethod(string methodName, System.Management.ManagementBaseObject
inParameters, System.Management.InvokeMethodOptions options)
void InvokeMethod(System.Management.ManagementOperationObserver watcher, string methodName,
System.Management.ManagementBaseObject inParameters, System.Management.InvokeMethodOptions options)

Shows us how to use it.

Going for the simplest option

PS> $service.psbase.InvokeMethod("StopService", $null)
0
PS> Get-Service BITS

Status   Name               DisplayName
——   —-               ———–
Stopped  BITS               Background Intelligent Transfer Ser…

PS> $service.psbase.InvokeMethod("StartService", $null)
0
PS> Get-Service BITS

Status   Name               DisplayName
——   —-               ———–
Running  BITS               Background Intelligent Transfer Ser…

PS> Get-WmiObject -Class Win32_Service -Filter "Name = ‘BITS’" | Invoke-WmiMethod -Name StopService

PS> Get-Service BITS

Status   Name               DisplayName
——   —-               ———–
Stopped  BITS               Background Intelligent Transfer Ser…

PS> Get-WmiObject -Class Win32_Service -Filter "Name = ‘BITS’" | Invoke-WmiMethod -Name StartService

PS> Get-Service BITS

Status   Name               DisplayName
——   —-               ———–
Running  BITS               Background Intelligent Transfer Ser…

These options will work as well

$service | Invoke-WmiMethod -Name StopService
$service | Invoke-WmiMethod -Name StartService

Invoke-WmiMethod -InputObject $service -Name StopService
Invoke-WmiMethod -InputObject $service -Name StartService

My preference is to use the get-wmiobject | invoke-wmimethod pairing as when I am developing I can easily test any filters before I start affecting the service.

Of all of the options using the  InvokeMethod method is the one to avoid as it is more cumbersome and involves more typing

In PowerShell v3 we can use the CIM cmdlets but that is a post for another day

More information on using WMI with PowerShell in PowerShell and WMI – www.manning.com/powershellandwmi


May 23, 2012  12:46 PM

WMI providers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I found a class new to me – Msft_Providers and this got me interested in WMI providers.

PS> Get-CimInstance -Class Msft_Providers | select -ExpandProperty provider
Msft_ProviderSubSystem
SCM Event Provider
WmiPerfClass

 

That seems a bit low. Digging a bit more I got back to the old favourite __provider.

Get-CimInstance -Class __provider | Measure-Object

produces an answer of 43 – not quite the answer to life, the universe and everything but close.

Is there any overlap between the two groups of providers?

 

$providers = Get-CimInstance -Class Msft_Providers | select -ExpandProperty provider           
Get-CimInstance -Class __provider | where Name -in $providers | select Name

 

provides the answer

Msft_ProviderSubSystem  

SCM Event Provider

 

In case you were wondering – “Starting with Windows Vista, the WMIPerfClass Provider and the WMIPerfInst Provider dynamically provide performance counter data for the WMI Performance Counter Classes.”

see http://msdn.microsoft.com/en-us/library/windows/desktop/aa392740(v=vs.85).aspx

 

One interesting property is the Hosting Model

Get-CimInstance -Class __provider | select HostingModel -Unique

Decoupled:NonCOM
NetworkServiceHost
WmiCore
LocalSystemHost
LocalServiceHost

NetworkServiceHost:[ReliabilityMetricsProvider]

 

But what do these mean

Full explanations for these and the other hosting models can be found at

http://msdn.microsoft.com/en-us/library/aa392509(VS.85).aspx

WmiCore – Activate provider in host to the WMI service. This hosting model is only supported for operating system components.

WmiCoreOrSelfHost – Activate provider in host to the WMI service or as local server. This hosting model is only supported for operating system components.

SelfHost – Activate provider as a local server implementation.

Decoupled:Com – Activate provider as a decoupled COM provider. See http://msdn.microsoft.com/en-us/library/aa390882(v=vs.85).aspx
 
Decoupled:NonCom – Activate provider as a non-COM event provider.
 
LocalSystemHost – Activate provider in the provider host process that is running under the LocalSystem account.

LocalSystemHostOrSelfHost – The provider is self-hosted or loaded into the Wmiprvse.exe process running under the LocalSystem account.

NetworkServiceHost – Activate provider in the provider host process that is running under the NetworkService account.

LocalServiceHost – Activate provider in the provider host process that is running under the LocalService account.

NetworkServiceHostOrSelfHost – The provider is self-hosted or loaded into the WmiPrvse.exe process running under the NetworkService account. NetworkServiceHostOrSelfHost is the default configuration when the HostingModel property in __Win32Provider is NULL. Because NetworkServiceHostOrSelfHost is the default, providers from earlier operating systems can continue to work in Windows Vista, Windows Server 2008, and later operating systems.

Get-CIMInstance is a new cmdlet in PowerShell v3. It is part of the new API for working with WMI. I will be blogging about these in greater detail over the next weeks and months as Powershell v3 is released.

More information on providers and the CIM cmdlets can be found in PowerShell and WMIwww.manning.com/powershellandwmi


May 21, 2012  1:56 PM

UK PowerShell group–next two meetings

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

29 May 2012

PowerShell and Windows server 2012 – new functionality pt 2

http://msmvps.com/blogs/richardsiddaway/archive/2012/05/08/uk-powershell-group-may-2012.aspx

 

4 July

Jonathan Medd

XenDesktop and PowerShell

This will be at the slightly later time of 8.30 BST.  Details to follow


May 20, 2012  12:40 PM

Using Invoke-WmiMethod to set the DNS servers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In the last post I showed that there was an issue with the way the SetDNSServerSearchOrder of the Win32_NetworkAdapterConfiguration class worked

This would work

$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=7"
$nic.SetDNSServerSearchOrder("10.10.54.201")

but using Invoke-WmiMethod failed

After discussions with Bartek Bielawski (PowerShell MVP) and a bit more digging I found that for multiple DNS servers this would work

$dnsserver = "10.10.54.201", "10.10.54.98"
Get-WmiObject -Class Win32_NetworkAdapterConfiguration  -Filter "Index=7" | Invoke-WmiMethod -Name SetDNSServerSearchOrder -ArgumentList (, $dnsserver)

Its necessary to create an array as the input argument  (, $variable) – its a unary array ie one element array

if you want to use just a single DNS server then you need to use the unary array trick twice – once when you create the variable and again when you use Invoke-wmimethod.  Messy but it works

$dnsserver = (,"10.10.54.201")
Get-WmiObject -Class Win32_NetworkAdapterConfiguration  -Filter "Index=7" | Invoke-WmiMethod -Name SetDNSServerSearchOrder -ArgumentList (, $dnsserver)

 

If you want to use the new CIM cmdlets in PowerShell v3 – its easy if you have multiple DNS servers

$dnsserver = "10.10.54.201", "10.10.54.98"
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index=7" | Invoke-CimMethod -MethodName SetDNSServerSearchOrder -Arguments @{DNSServerSearchOrder = $dnsserver}

 

for a single one we just need to create a unary array on the Arguments parameter
$dnsserver = "10.10.54.201"
Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter "Index=7" | Invoke-CimMethod -MethodName SetDNSServerSearchOrder -Arguments @{DNSServerSearchOrder = (,$dnsserver)}

 

This is not satisfactory because we have to adopt different techniques depending on the number of DNS servers we need to put into NIC property. This is NOT a PowerShell issue – it has to be a WMI issue because the IP address that we saw last time also takes an array and it was very happy with a single value.

Hopefully this is not something that will come up too often but be aware of these options when working with WMI methods


May 20, 2012  4:34 AM

TCP/IP Alternative Configurations: pt IV reset to static address

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

At some stage we may need to reset our NIC back to having a static address

$index = 7            
            
$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration `
-Filter "Index=$index"             
            
$ipaddress = @("10.10.54.202")            
$subnet = @("255.255.255.0")            
Invoke-WmiMethod -InputObject $nic -Name EnableStatic -ArgumentList $ipaddress, $subnet            
            
$dnsserver = "10.10.54.201"            
$nic.SetDNSServerSearchOrder($dnsserver)            
            
#Invoke-WmiMethod -InputObject $nic -Name SetDNSServerSearchOrder -ArgumentList $dnsserver

We get the configuration of the NIC and use the EnableStatic method to set the address and subnet

The SetDNSServerSearchOrder method is used to set the DNS server.

Notice I haven’t been able to use Invoke-WmiMethod at this point – I’ve had to call the method directly on the object. There appears to be an issue with the formatting of the DNS server addresses as Invoke-WmiMethod complains that the argument has to be an array.

This is under investigation.

More on using WMI with PowerShell can be found in PowerShell and WMI. Chapter 11 covers network adapters in detail.  More details from www.manning.com/powershellandwmi


May 18, 2012  2:00 PM

TCP/IP Alternative Configurations: pt III set the alternative configuration

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

 

We have seen how to set the NIC to use DHCP to get its address. This post shows how to set the alternative configuration on the NIC. If you just  want APIPA then do nothing – other wise use this script

$HKLM = 2147483650 #HKEY_LOCAL_MACHINE            
            
$index = 7            
$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=$index"            
            
$key = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$($nic.SettingID)"            
            
Invoke-WmiMethod -Class StdRegprov -Name SetDWORDvalue -ArgumentList $hklm, $key, "AddressType", 2            
            
Invoke-WmiMethod -Class StdRegprov -Name SetMULTISTRINGvalue -ArgumentList $hklm, $key, "Alternate_$($nic.SettingID)", "ActiveConfigurations"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "10.10.54.202", "DhcpIpAddress"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "10.10.54.201", "DhcpNameServer"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "255.255.255.0", "DhcpSubnetMask"

Again I’m cheating by defining the NIC in terms of its Index number

The registry key is derived from the SettingID property of the NIC

We then need to set a number of registry values. The AddressType sets the alternative configuration to use our informations rather than APIPA. The ActiveCinfigurations value is set using the SettingID property of the NIC

The address, subnetmask, and names server are set.

If you look carefully at the lines where we use the SetMULTISTRINGvalue and SetSTRINGvalue methods you will notice that we give the hive, key, value and then registry value name  whereas the SetDWORDvalue method we give hive, key, value name and then value.

This is a quirk of Invoke-WmiMethod

The WMI documentation for SetMULTISTRINGvalue and SetSTRINGvalue methods state the parameters should be:

  • hive
  • registry key
  • registry value name
  • value

This order is constant across the Set* methods of the StdRegProv class -  see http://msdn.microsoft.com/en-us/library/windows/desktop/aa393600(v=vs.85).aspx

If we do some investigation

PS> ([wmiclass]"StdRegprov").GetMethodParameters(‘SetSTRINGvalue’)


__GENUS          : 2

__CLASS          : __PARAMETERS

__SUPERCLASS     :

__DYNASTY        : __PARAMETERS

__RELPATH        :

__PROPERTY_COUNT : 4

__DERIVATION     : {}

__SERVER         :

__NAMESPACE      :

__PATH           :

hDefKey          : 2147483650

sSubKeyName      :

sValue           : hello

sValueName       :

This clearly shows we need to give the value before the value name.

the same holds true if we investigate using Get-CimClass in PowerShell v3

Get-CimClass -ClassName StdRegProv |

select -ExpandProperty CimClassMethods |

where Name -eq "SetStringValue" |

select -ExpandProperty Parameters

produces

Name                                         CimType Qualifiers

—-                                         ——- ———-

hDefKey                                      UInt32 {ID, IN}

sSubKeyName                                  String {ID, IN}

sValue                                       String {ID, in}

sValueName                                   String {ID, in}

if we use Invoke-CimMethod its not to much of a problem as we have to provide the value name and value pairs as a hash table we are not relying on argument order.

I’ll post an alternative listing using the CIM cmdlets another time


May 18, 2012  12:07 PM

Advanced PowerShell v3 book

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Three new chapters are added to the MEAP – Manning Early Access Program

Chapter 3 – Using the PowerShell help system – includes PowerShell v3 Updateable help

Chapter 5 – Working with PSsnapins and Modules

Chapter 9 – Formatting

 

www.manning.com/powershellindepth


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: