PowerShell for Windows Admins


June 7, 2016  4:03 AM

Local Administrators

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Finding the local administrators on a system is a not infrequent action.  There are a number of ways to do this.

The oldest method is to use the ADSI WinNT provider

$group =[ADSI]”WinNT://$($env:COMPUTERNAME)/Administrators, group”
$members = @($group.psbase.Invoke(“Members”))
$members | Foreach {$_.GetType().InvokeMember(“Name”, ‘GetProperty’, $null, $_, $null)}

NOTE – this doesn’t work on my Windows 10 system – build 14352

I’d recommend avoiding the WinNT provider if you can

WMI provides this option

$group = Get-CimInstance -ClassName Win32_Group -Filter “Name=’Administrators'”
Get-CimAssociatedInstance -InputObject $group -ResultClassName Win32_UserAccount

You can also use a .NET based approach with the System.DirectoryServices.AccountManagement  namespace

using assembly System.DirectoryServices.AccountManagement
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $($env:COMPUTERNAME)

$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::Name
$grp = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, “Administrators”)
$grp.Members | select SamAccountName

This is a bit more complicated as you have to load the assembly (using is new to PowerShell v5 – use Add-Type in earlier versions)

Set the context to the local machine and the identity type to Name

You can then use FindByIdentity() to get the local adminsitrators groups and look at the Members property to find the group members.

PowerShell v5 brings a Local Accounts module – Microsoft.PowerShell.LocalAccounts

Add-LocalGroupMember
Disable-LocalUser
Enable-LocalUser
Get-LocalGroup
Get-LocalGroupMember
Get-LocalUser
New-LocalGroup
New-LocalUser
Remove-LocalGroup
Remove-LocalGroupMember
Remove-LocalUser
Rename-LocalGroup
Rename-LocalUser
Set-LocalGroup
Set-LocalUser

NOTE – depending on your version of PowerShell v5 you may, or may not have this module. Its present in the later Windows 10 builds (on Insider Preview) and in Windows server 2016 TP 5. Eventually it’ll become available on all Windows 10 systems through Windows updates.

PS>  Get-LocalGroupMember -Group Administrators | select Name

Name
—-
RSsurfacePro2\Administrator
RSSURFACEPRO2\Richard

June 6, 2016  3:39 AM

PowerShell Summit 2017–request for topics

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The PowerShell Summit is a community event. PowerShell.orh may organise it but we’re very aware that it is the PowerShell community’s event.

We’ve been planning the  2017 Summit for a while and we’ve reached a point in the process where we need your help.

What topics would you like to see covered at the 2017 Summit. We’ve published a request for topics at –http://powershell.org/request-for-topics/

Please let us know the topic areas you’d like covered and we can build that information into our agenda planning to ensure we have the Summit the community wants


June 1, 2016  1:54 PM

Dates in file and folder

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

If you want to incorporate the date in a file or folder name you can’t use Get-Date directly

PS>  Get-Date

01 June 2016 20:52:03

The simplest answer is to use the –Format or –Uformat parameters:

PS>  Get-Date -Format yyyyMMdd
20160601
PS>  Get-Date -UFormat %Y%m%d
20160601


May 31, 2016  3:19 PM

Counting members

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

If you have a collection of objects

$proc = get-process

you can get the number of members using the Length property

PS>  $proc.Length
71

$proc is of type System.Array

PS>  $proc.GetType()

IsPublic IsSerial Name                                     BaseType
——– ——– —-                                     ——–
True     True     Object[]                                 System.Array

MSDN documenattion is at https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx

PowerShell also adds a Count property that is an alias of Length

if you are after the number of members in a collection via the pipeline you need to use the Measure-Object cmdlet

PS>  $proc | Measure-Object
Count    : 71
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

or measure directly

PS>  Get-Process | Measure-Object
Count    : 71
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Notice that Measure-Object can also be used to discover other statistics – the Average, Sum, Maximum and Minimum. These only work for numeric properties.


May 30, 2016  4:20 PM

Windows Server eras

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Windows Server 2016

This is worth a read

https://blogs.technet.microsoft.com/windowsserver/2016/05/26/moving-forward-with-powershell-and-windows-server-2016/

It discusses the evolution of Windows server and PowerShell. Read it in conjunction with the videos of Jeffrey Snover’s talks at WinOps and the European PowerShell conference this year.


May 30, 2016  10:02 AM

WMF 5.0 preview support ending

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Support for the WMF 5.0 preview is ending – https://blogs.msdn.microsoft.com/powershell/2016/05/23/end-of-support-for-windows-management-framework-wmf-5-0-production-preview-is-approaching-upgrade-to-rtm-bits/

Upgrade now!


May 29, 2016  3:58 AM

PowerShell 5.1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Want to see what PowerShell 5.1 looks like?

Install Windows Server 2016 TP 5 or get on the fast ring for Windows 10 Insider Previews. The latest build – 14352 shows a PowerShell version of  5.1.14352.1002 with Powershell 1.0 through 5.0 as compatible versions

The WSMAN stack is still version 3.0 so no issues with remoting or CIM sessions between PowerShell 3.0, 4.0, 5.0 and 5.1

PowerShell 2.0 will have the CIM session issue as it uses WSMAN stack 2.0 and PowerShell 1.0 didn’t have remoting


May 28, 2016  8:14 AM

Slides and Code from my WinOps 2016 talk

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DevOps

The slides and code from my WinOps 2016 talk can be found here:

https://onedrive.live.com/redir?resid=43CFA46A74CF3E96!79154&authkey=!AKIt7btEZsahTf8&ithint=file%2czip


May 26, 2016  1:15 PM

DHCP scope

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DHCP

After rebuilding my test machine for Server 2016 (with mixed results) I needed to add a DHCP scope to the environment. I blogged about the DHCP PowerShell module several years ago.

To quickly add a DHCP scope to the current server

Add-DhcpServerv4Scope -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)

Set-DhcpServerv4OptionValue  -ScopeId 10.10.54.0 -DnsServer 10.10.54.201

I’ll save this as I’ll have to rebuild the machine when Windows 2016 RTM’s and possibly again if I can’t get the wireless adapter to work then.


May 26, 2016  11:52 AM

Windows server 2016 TP 5 activation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Windows Server 2016

Having finished the current round of conference speaking I needed to upgrade my system to Windows 2016 TP5. I’m using a 6 year old Lenovo W510 system.

First problem is that I couldn’t get the wireless adapter working – after a lot of searching I saw a post with a similar issue due to the Wireless LAN Service which you need doesn’t install correctly. Server Manager and Get-WindowsFeature both show it as installed but the wlansvc service isn’t available – doesn’t show in in the services GUI or through Get-Service.

Telephone activation is a pain but works BUT if an internet activation fails you don’t get the telephone option.

This post http://www.urtech.ca/2016/02/solved-how-to-activate-windows-10-server-2016-through-command-line/

saved the day.

Basically type SLUI 4 in the run dialog to open the manual activation  dialog.

Hope this is fixed for RTM otherwise I’ll need a new machine.


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: