PowerShell for Windows Admins


September 20, 2011  3:25 PM

PowerShell and DHCP: 1 – servers

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Remember that yesterday I loaded the RSAT tools on to my Windows 8 server instance. This includes a PowerShell module that auto loads – like all Windows 8 modules on the PS module path

Start by looking at the GET commands

Get-Command -Module DhcpServer   get*

Get-DhcpServerAuditLog
Get-DhcpServerDatabase
Get-DhcpServerInDC
Get-DhcpServerSetting
Get-DhcpServerv4Binding
Get-DhcpServerv4Class
Get-DhcpServerv4DnsSetting
Get-DhcpServerv4ExclusionRange
Get-DhcpServerv4Failover
Get-DhcpServerv4Filter
Get-DhcpServerv4FilterList
Get-DhcpServerv4FreeIPAddress
Get-DhcpServerv4Lease
Get-DhcpServerv4OptionDefinition
Get-DhcpServerv4OptionValue
Get-DhcpServerv4Policy
Get-DhcpServerv4PolicyIPRange
Get-DhcpServerv4Reservation
Get-DhcpServerv4Scope
Get-DhcpServerv4ScopeStatistics
Get-DhcpServerv4Statistics
Get-DhcpServerv4Superscope
Get-DhcpServerv6Binding
Get-DhcpServerv6Class
Get-DhcpServerv6DnsSetting
Get-DhcpServerv6ExclusionRange
Get-DhcpServerv6FreeIPAddress
Get-DhcpServerv6Lease
Get-DhcpServerv6OptionDefinition
Get-DhcpServerv6OptionValue
Get-DhcpServerv6Reservation
Get-DhcpServerv6Scope
Get-DhcpServerv6ScopeStatistics
Get-DhcpServerv6StatelessStatis…
Get-DhcpServerv6StatelessStore
Get-DhcpServerv6Statistics
Get-DhcpServerVersion

 

This will keep us busy for a while and we haven’t looked at the other verbs!

Notice we don’t have a get-dhcpserver as such but we do have Get-DhcpServerVersion

 

PS> Get-DhcpServerVersion -ComputerName server02

MajorVersion : 6
MinorVersion : 1

 

PS> Get-WmiObject Win32_OperatingSystem -ComputerName server02 | fl Caption, version

Caption : Microsoft Windows Server 2008 R2 Datacenter
version : 6.1.7601

 

All we need is a bit of a wrapper function to decode the version numbers

 

also server settings

PS> Get-DhcpServerSetting -ComputerName server02

IsDomainJoined            : True
IsAuthorized              : True
DynamicBootp              : True
RestoreStatus             : False
ConflictDetectionAttempts : 0
NpsUnreachableAction      : Full
NapEnabled                : True
ActivatePolicies          :

 

Scopes are fairly important

 

PS> Get-DhcpServerv4Scope -ComputerName server02 | fl *

ScopeId          : 10.10.54.0
SubnetMask       : 255.255.255.0
StartRange       : 10.10.54.246
EndRange         : 10.10.54.250
ActivatePolicies :
Delay            : 0
Description      : Only used for new servers – before a static address is defined
LeaseDuration    : 04:00:00
MaxBootpClients  : 4294967295
Name             : NewServers
NapEnable        : True
NapProfile       :
State            : Active
SuperscopeName   :
Type             : Dhcp
ComputerName     :

 

There are IPv6 equivalent cmdlets as well

 

checking on free addresses

PS> Get-DhcpServerv4FreeIPAddress -ComputerName server02 -ScopeId "10.10.54.0"
10.10.54.246

 

PS> Get-DhcpServerv4FreeIPAddress -ComputerName server02 -ScopeId "10.10.54.0" -NumAddress 10
WARNING: The requested number of free IP addresses could not be found.
10.10.54.246
10.10.54.247
10.10.54.248
10.10.54.249
10.10.54.250

 

PS> Get-DhcpServerv4ScopeStatistics -ScopeId "10.10.54.0" -ComputerName server02 | fl *

ScopeId                       : 10.10.54.0
AddressesFree                 : 5
AddressesFreeOnPartnerServer  :
AddressesFreeOnThisServer     :
AddressesInUse                : 0
AddressesInUseOnPartnerServer :
AddressesInUseOnThisServer    :
PendingOffers                 : 0
PercentageInUse               : 0
ReservedAddress               : 0
SuperscopeName                :
ComputerName                  :

 

and finally

Get-DhcpServerv4Lease -ComputerName server02 -ScopeId "10.10.54.0"

will return the leases

September 20, 2011  12:34 PM

PowerShell 3 CTP1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Today’s big news is the release of CTP1 for PowerShell 3

http://www.microsoft.com/download/en/details.aspx?id=27548

It only runs on Windows 7 SP1 and Windows 2008 R2 SP1


September 19, 2011  12:54 PM

PowerShell 3 discovery

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Having a quick look at the numbers of commands in PowerShell 3 on Windows 8 I get this

 

PS> (get-command -CommandType cmdlet).count
376
PS> (get-command -CommandType function).count
524

 

The numbers are even higher on Server 8 depending on what  features/roles you have loaded.

 

How do you keep track of all those commands – memorising 900 names is not going to happen never mind the reported 2000+ on the server.

The answer is to step up a level to the module. Remember the modules that are available or use

PS> Get-Module -ListAvailable

    Directory: C:\Scripts\Modules

ModuleType Name                      ExportedCommands
———- —-                      —————-
Manifest   SystemInfo                {Get-ComputerSystem, Get-OperatingSystem}

    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules

ModuleType Name                      ExportedCommands
———- —-                      —————-
Manifest   AppLocker                 {Set-AppLockerPolicy, Get-AppLockerPolicy, Test-AppLockerPolicy…
Manifest   Appx                      {Add-AppxPackage, Get-AppxPackageManifest, Get-AppxPackage…
Manifest   BitLocker                 {Get-EncryptableVolume, Get-EncryptableVolumes, Get-Protectors..
Manifest   BitsTransfer              {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer…
Manifest   BranchCache               {Add-BCDataCacheExtension, Clear-BCCache, Disable-BC…

 

Notice that the folder the module is contained is displayed – useful.

Alternatively refine the search

Get-Module -ListAvailable *dns*

One point to note is that Get-Module by default only shows the modules you’ve used!  A new PowerShell console shows

Microsoft.PowerShell.Core
Microsoft.PowerShell.Management

but

PS> get-module | select name

Name
—-
Microsoft.PowerShell.Core
Microsoft.PowerShell.Management
Microsoft.PowerShell.Utility

because we’ve used select-object


September 18, 2011  8:51 AM

DNS cmdlets in PowerShell 3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

There are a number of modules related to DNS

ModuleType Name
———- —-
Manifest DnsClient
Manifest DnsConfig
Binary DnsLookup
Manifest DnsNrpt

If we start with the DnsClient module we get these functions and cmdlets

Add-DnsClientNrptRule
Clear-DNSClientCache
Get-DNSClient
Get-DNSClientCache
Get-DnsClientEffectiveNrptPolicy
Get-DnsClientNrptGlobal
Get-DnsClientNrptRule
Get-DNSGlobalSettings
Get-DNSServerAddress
Remove-DnsClientNrptRule
Set-DNSClient
Set-DnsClientNrptGlobal
Set-DnsClientNrptRule
Set-DNSGlobalSettings
Resolve-DnsName

Knowing which DNS server the client is using

PS> Get-DNSServerAddress | select ElementName, Name

ElementName Name
———– —-
Virtual Wireless 192.168.2.1
isatap.{E962BF88-1194-44A8-B30B-A65A4772C812} 192.168.2.1
Virtual LAN 10.10.54.201
isatap.{EA0AB201-1381-4643-A67D-72C9C8860860} 10.10.54.201
Loopback Pseudo-Interface 1 fec0:0:0:ffff::1
Loopback Pseudo-Interface 1 fec0:0:0:ffff::2
Loopback Pseudo-Interface 1 fec0:0:0:ffff::3

and what the client has cached

Get-DNSClientCache | select Name, data

Name data
—- —-
server02 192.168.2.1
server02 10.10.54.201
server02 192.168.1.6
server02.manticore.org 192.168.2.1
server02.manticore.org 10.10.54.201
server02.manticore.org 192.168.1.6
watson.telemetry.microsoft.com

final one for the moment – is a replacement for nslookup

PS> Resolve-DnsName exch07

IP4Address : 10.10.54.130
Name : exch07.Manticore.org
Type : A
CharacterSet : Unicode
Section : Answer
DataLength : 4
TTL : 1200


September 17, 2011  9:44 AM

UK User Group–Alexsandar

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Quick reminder about the UK User group Live Meeting on remoting and end points presented by PowerShell MVP Alexsandar Nikolic.

Details and link to join from http://msmvps.com/blogs/richardsiddaway/archive/2011/09/08/powershell-user-group-20-september-2011.aspx

Aleksandar talked about this at the recent PowerShell Deep Dive. This will be good!


September 14, 2011  1:57 PM

The Experts Conference April 2011–My session

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Dmitry has just posted the video of the session I did at the PowerShell deep dive @ TEC in April

http://dmitrysotnikov.wordpress.com/2011/09/14/video-richard-siddaway-wmi-gems-and-gotchas/

 

The session was entitled WMI: Hidden Gems and Gotchas

Links to the slides and demo scripts are also available on the post


September 14, 2011  1:12 PM

Windows 8 and PowerShell 3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You will have started to see blogs about Windows 8 and PowerShell 3 due to Microsoft releasing a developers preview version on MSDN.  I’m currently building Windows 8 and Server 8 machines – more news when they are up and running


September 8, 2011  2:46 PM

Quirks of Get-member and WMI objects

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I have been asked about discovering the methods available on a WMI object. I’ve mentioned the GetMethodParameters method a few times but it can be difficult to find.  Normally if we want to discover the methods on an object we would do this

 

Get-WmiObject Win32_Service | Get-Member -MemberType method

 

which gives this list

Change

ChangeStartMode

Delete

GetSecurityDescriptor

InterrogateService

PauseService

ResumeService

SetSecurityDescriptor

StartService

StopService

UserControlService

we will ignore the terminal services object that also appears

 

To dig into the underlying object we use .psbase (or Get-Member -MemberType method –View base)

 

(Get-WmiObject Win32_Service).psbase | Get-Member -MemberType method

 

gives us this list

Address

Clone

CopyTo

Equals

Get

GetEnumerator

GetHashCode

GetLength

GetLongLength

GetLowerBound

GetType

GetUpperBound

GetValue

Initialize

Set

SetValue

ToString

 

Note that the object type is System.Management.Automation.PSMemberSet

 

When we use Get-WmiObject we are looking at an instance of the WMI class. To look at the class itself

 

[wmiclass]"Win32_Service" | Get-Member -MemberType method

 

but this just shows a Create method

 

If we drop into the base object now

 

([wmiclass]"Win32_Service").psbase | Get-Member -MemberType method

 

we get

Clone

CompareTo

CopyTo

CreateInstance

CreateObjRef

Delete

Derive

Dispose

Equals

Get

GetHashCode

GetInstances

GetLifetimeService

GetMethodParameters

GetPropertyQualifierValue

GetPropertyValue

GetQualifierValue

GetRelated

GetRelatedClasses

GetRelationshipClasses

GetRelationships

GetStronglyTypedClassCode

GetSubclasses

GetText

GetType

InitializeLifetimeService

InvokeMethod

Put

SetPropertyQualifierValue

SetPropertyValue

SetQualifierValue

ToString

 

Which shows the Method we want and a few other potentially interesting methods to test out.


September 8, 2011  1:40 PM

PowerShell User Group–20 September 2011

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


When: Tuesday, Sep 20, 2011 7:30 PM (BST)


Where:

*~*~*~*~*~*~*~*~*~*

Aleksandar Nikolic presents on PowerShell remoting and the customisation of remoting end points.
Aleksandar’s presentation at the PowerShell Deep Dive was excellent – don’t miss this one

Notes


Richard Siddaway has invited you to attend an online meeting using Live Meeting.
Join the meeting.
Audio Information
Computer Audio
To use computer audio, you need speakers and microphone, or a headset.
First Time Users:
To save time before the meeting, check your system to make sure it is ready to use Microsoft Office Live Meeting.
Troubleshooting
Unable to join the meeting? Follow these steps:

  1. Copy this address and paste it into your web browser:
    https://www.livemeeting.com/cc/usergroups/join
  2. Copy and paste the required information:
    Meeting ID: 3GJGF5
    Entry Code: d6`S<^zjM
    Location: https://www.livemeeting.com/cc/usergroups

If you still cannot enter the meeting, contact support

Notice
Microsoft Office Live Meeting can be used to record meetings. By participating in this meeting, you agree that your communications may be monitored or recorded at any time during the meeting.


September 6, 2011  11:29 AM

Test for domain membership

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Quick function to determine if a given machine is in a domain or workgroup

function test-domain{             
[CmdletBinding()]             
param (             
[parameter(Position=0,            
   Mandatory=$true,            
   ValueFromPipeline=$true,             
   ValueFromPipelineByPropertyName=$true)]            
   [string]$computer="."             
)             
BEGIN{}#begin             
PROCESS{            
 Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer |            
 select Name, Domain            
            
}#process             
END{}#end            
            
}

Feed the function a computer name or IP address and it will return the name and domain. If the computer is in the domain we get the full domain name – if its in a workgroup we get the workgroup name (WORKGROUP by default)


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: