Monthly Archive

Categories

Get-Counter

The Get-Counter cmdlet returns in PowerShell v7 preview 6. Its only Get-Counter though the Import/Export-Counter cmdlets aren’t available. Get-Counter isn’t experimental feature so its available as soon as you install preview 6.

PS> Get-Command Get-Counter -Syntax

Get-Counter [[-Counter] <string[]>] [-SampleInterval <int>] [-MaxSamples <long>] [-Continuous] [-ComputerName <string[]>] [<CommonParameters>]

Get-Counter [-ListSet] <string[]> [-ComputerName <string[]>] [<CommonParameters>]

 

There’s a default set of counters available:

PS> Get-Counter

Timestamp CounterSamples
--------- --------------
30/11/2019 11:44:52 \\w510w10\network interface(intel[r] 82577lm gigabit network connection)\bytes total/sec : 0

\\w510w10\network interface(11b_g_n wireless lan mini-pci express adapter ii)\bytes total/sec : 150.691579544147

\\w510w10\processor(_total)\% processor time :
0.9798448232225421

\\w510w10\memory\% committed bytes in use :
25.227649888309568

\\w510w10\memory\cache faults/sec :
18.961192128071477

\\w510w10\physicaldisk(_total)\% disk time :
0.30062706236372333

\\w510w10\physicaldisk(_total)\current disk queue length :
0

 

To see the available list of counter sets

PS> Get-Counter -ListSet * | select CounterSetName

 

To see the counters in a set

PS> Get-Counter -ListSet Processor | select -ExpandProperty Counter

 

To view a counter

PS> Get-Counter -Counter '\Processor(*)\% Processor Time'

 

The SampleInterval, MaxSamples and Continuous parameters can be used to control the number of samples and the time of recording.

Clipboard cmdlets

The clipboard cmdlets return in PowerShell v7 preview 6

Get-Clipboard and Set-Clipboard are back and according to the release notes work cross platform.

PS> Get-Command Get-Clipboard -Syntax

Get-Clipboard [-Raw] [<CommonParameters>]

 

PS> Get-Clipboard
Clipboard test

 

The Raw parameter makes Get-Clipboard ignore new line characters and get the entire contents of the clipboard.

PS> Get-Clipboard -Raw
Clipboard test

Clipboard test2

 

Putting values onto the clipboard:

PS> Get-Command Set-Clipboard -Syntax

Set-Clipboard [-Value] <string[]> [-Append] [-WhatIf] [-Confirm] [<CommonParameters>]

 

PS> Set-Clipboard -Value 'new clipboard' –Append

PS> Get-Clipboard -Raw
Clipboard test

Clipboard test2
new clipboard

 

The clipboard can be cleared – more or less – with a blank string

Set-Clipboard -Value ' '

 

On Linux, Set-Clipboard requires the xclip utility to be in the path.

Clear-RecycleBin

The Clear-RecycleBin cmdlet returns in PowerShell v7 preview 6 on Windows

PS> Get-Command Clear-RecycleBin -Syntax
Clear-RecycleBin [[-DriveLetter] <string[]>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

 

To use

PS> Clear-RecycleBin

 

Will ask for confirmation you want to perform the action

PS> Clear-RecycleBin -Force

or

PS> Clear-RecycleBin -Confirm:$false

 

Will bypass the need for confirmation

Fibonacci series

Today’s date 23 November is 1123 id written as month then day. Those numbers are the start of the Fibonacci series. So as its Fibonacci day here’s a quick PowerShell function to create a Fibonacci series.

 

function new-fibonacci {
param (
[int]$numberofelements = 10
)

## first 2 elements

$ell1 = 1
$ell1

$ell2 = 1
$ell2

$i = 2
while ($i -lt $numberofelements){
$elnext = $ell1 + $ell2
$elnext

$i++
$ell2 = $ell1
$ell1 = $elnext

}

}

 

The function has 1 parameter which determines the number of elements of the series you want to produce.

 

The first two elements are hard coded as being equal to 1 and output from the function.

 

The bulk of the work is done in the while loop – deliberately chosen to test at the top of the loop.

 

The next element in the series is the sum of the current last and next to last elements. The counter is incremented, the current last element becomes the next to last and the newly calculated element becomes the current last element.

 

If you just want the numbers displayed

PS> new-fibonacci

 

will display the first ten elements

To get a specific number of elements

PS> new-fibonacci -numberofelements 17

 

If you want the numbers in an array for further processing

PS> $fib = new-fibonacci -numberofelements 17

 

Enjoy.

PowerShell v7 preview 6

PowerShell v7 preview 6 has arrived - https://github.com/PowerShell/PowerShell/releases

Its the last preview, as the Release candidate should appear next month, so this version is a good indication of what’ll be available in PowerShell v7.

 

Test-Connection finally works properly and is usable

 

Some graphical based cmdlets are now available

Out-GridView

Show-Command

Get-Help –ShowWindow

 

A number of other cmdlets return:

Get-Counter

Get- , Set-Clipboard  (now cross platform)

Out-Printer

Update-List

Clear-RecycleBin

 

Some of these are Windows only as the features don’t exist on other platforms

 

New experimental features include the null-conditional operators and using non-compatible Windows PowerShell modules in PowerShell core (no more WindowsCompatibility module)

 

I’ll look at some these in more depth in future posts.

I’ll try and put together a summary of all the major changes between PowerShell v6.2 and v7

Get-DnsServerResourceRecord

You saw how to discover the zones on a DNS server in the first post in this series. Now its time to discover the records in a zone. For that you need Get-DnsServerResourceRecord.

 

The syntax is:

Get-DnsServerResourceRecord [-ZoneName] <string> [[-Name] <string>] [-Type] <ushort> [-ComputerName <string>] [-Node] [-ZoneScope <string>] [-VirtualizationInstance <string>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [<CommonParameters>]

 

Get-DnsServerResourceRecord [-ZoneName] <string> [[-Name] <string>] [-ComputerName <string>] [-Node] [-ZoneScope <string>] [-VirtualizationInstance <string>] [-RRType <string>] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [<CommonParameters>]

 

Use as

PS> Get-DnsServerResourceRecord -ZoneName manticore.org -RRType A

 

or

PS> Get-DnsServerResourceRecord -ZoneName manticore.org -Type 1

 

The following record types can be used with RRType

HInfo
Afsdb
Atma
Isdn
Key
Mb
Md
Mf
Mg
MInfo
Mr
Mx
NsNxt
Rp
Rt
Wks
X25
A
AAAA
CName
Ptr
Srv
Txt
Wins
WinsR
Ns
Soa
NasP
NasPtr
DName
Gpos
Loc
DhcId
Naptr
RRSig
DnsKey
DS
NSec
NSec3
NSec3Param

 

I haven’t found any documentation that links the type number to the string version of the record type

Searching Active Directory by email address

I was asked recently about searching Active Directory by email address and returning the first and last names of the users.

First off I needed to populate the first and last name and email fields as they weren’t set in my test domain

Get-ADUser -Filter * -SearchBase 'OU=UserAccounts,DC=Manticore,DC=org' |
foreach {
$names = $_.Name -split ' '

Set-ADUser -Identity $_.DistinguishedName -EmailAddress $_.UserPrincipalName -GivenName $names[1].Trim() -Surname $names[0].Trim()

}

 

Get the users in the UserAccounts domain. Split the name and use that information to set the first (GivenName) and last (Surname) properties. Use the UserprincipalName for the email address.

 

Now create a CSV file with the email addresses

PS>  Get-ADUser -SearchBase 'OU=UserAccounts,DC=Manticore,DC=org' -Filter * -Properties EmailAddress | select -Property  EmailAddress | Export-Csv -Path C:\Test\addresses.csv

 

Test the file

PS>  Import-Csv -Path C:\Test\addresses.csv

 

You can’t use the –Identity property when you’re searching by email address. You have to use a filter:

PS>  Get-ADUser -SearchBase 'OU=UserAccounts,DC=Manticore,DC=org' -Filter {EmailAddress -eq }

 

Or LDAP filter

PS>  Get-ADUser -SearchBase 'OU=UserAccounts,DC=Manticore,DC=org' -LDAPFilter "(objectclass=user)(mail=DorothyJones@manticore.org)"

 

The difference is that the filter uses the AD cmdlet name for the property but the LDAP filter uses the LDAP name of the proeprty

 

To read the email addresses and find the corresponding names using a filter

Import-Csv -Path C:\Test\addresses.csv |
ForEach-Object {
$address = $_.EmailAddress
$user = Get-ADUser -SearchBase 'OU=UserAccounts,DC=Manticore,DC=org' -Filter {EmailAddress -eq $address}

$props = [ordered]@{
FirstName = $user.GivenName
Lastname = $user.Surname
Email = $_.EmailAddress
}
New-Object -TypeName PSobject -Property $props
}

 

Import the addresses. I’ve found it less error prone to create a variable to handle the value I’m searching for rather than trying to substitute into the filter. Once you have the account create the output object.

 

Using an LDAP filter is similar

Import-Csv -Path C:\Test\addresses.csv |
ForEach-Object {
$address = $_.EmailAddress
$user = Get-ADUser -SearchBase 'OU=UserAccounts,DC=Manticore,DC=org' -LDAPFilter "(objectclass=user)(mail=$address)"

$props = [ordered]@{
FirstName = $user.GivenName
Lastname = $user.Surname
Email = $_.EmailAddress
}
New-Object -TypeName PSobject -Property $props
}

 

The choice between a filter and an LDAP filter is up to you. Both have quirks and are probably equally awkward to use.

DNS zones

I’m working through the commonly used DNS server cmdlets to determine if everything works in PowerShell core. First up is the cmdlet to get the DNS zones on your DNS server.

 

PS> Get-DnsServerZone | Format-List -Property ZoneName, ZoneType, IsAutoCreated, IsDsIntegrated, IsReverseLookupZone, IsSigned

ZoneName : _msdcs.Manticore.org
ZoneType : Primary
IsAutoCreated : False
IsDsIntegrated : True
IsReverseLookupZone : False
IsSigned : False

 

ZoneName : 0.in-addr.arpa
ZoneType : Primary
IsAutoCreated : True
IsDsIntegrated : False
IsReverseLookupZone : True
IsSigned : False

 

ZoneName : 127.in-addr.arpa
ZoneType : Primary
IsAutoCreated : True
IsDsIntegrated : False
IsReverseLookupZone : True
IsSigned : False

 

ZoneName : 255.in-addr.arpa
ZoneType : Primary
IsAutoCreated : True
IsDsIntegrated : False
IsReverseLookupZone : True
IsSigned : False

 

ZoneName : Manticore.org
ZoneType : Primary
IsAutoCreated : False
IsDsIntegrated : True
IsReverseLookupZone : False
IsSigned : False

 

ZoneName : TrustAnchors
ZoneType : Primary
IsAutoCreated : False
IsDsIntegrated : True
IsReverseLookupZone : False
IsSigned : False

 

The DNS server cmdlets are created using CDXML so I’d expect them to work but it never hurts to test.

Get-Hotfix

Get-Hotfix returns to PowerShell in PowerShell v7 preview 5 – at least on Windows.

 

PS> Get-Command Get-HotFix -Syntax

Get-HotFix [[-Id] <string[]>] [-ComputerName <string[]>] [-Credential <pscredential>] [<CommonParameters>]

Get-HotFix [-Description <string[]>] [-ComputerName <string[]>] [-Credential <pscredential>] [<CommonParameters>]

 

It still uses the Win32_QuickFixEngineering CIM class under the covers which is why its Windows only.

 

My preferred way of using Get-Hotfix is

PS> Get-HotFix | Sort-Object -Property InstalledOn –Descending

 

That way I see the most recent fixes at the top of the list and its easier to determine if the system is up to date.

Resolve-Path

Resolve-Path is a cmdlet I haven’t used much – if at all – so I thought I should have a look at it.

At an arbitrary point in the file system:

PS> Get-Location

Path
----
C:\Scripts\Modules\Coordinates

 

.. Indicates the next level up

PS> Get-ChildItem -Path ..

Directory: C:\Scripts\Modules

etc

 

..\.. indicates two levels up

PS> Get-ChildItem -Path ..\..

Directory: C:\Scripts

etc

 

Resolve-Path will resolve any use of wildcards and other characters with meaning into the full path

PS> Resolve-Path -Path ..

Path
----
C:\Scripts\Modules

 

PS> Resolve-Path -Path ..\..

Path
----
C:\Scripts

 

Note that

PS> Resolve-Path -Path .

Path
----
C:\Scripts\Modules\Coordinates

 

is effectively the same as Get-Location