PowerShell for Windows Admins


October 24, 2015  7:48 AM

Testing connectivity before Invoke-Command

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A question on the forum asked about testing if a remote machine could be reached before using Invoke-Command against it.

The usual way to test if you can reach a remote machine is to ping it

PS> Test-Connection -ComputerName $env:COMPUTERNAME -Quiet
True

That shows you can reach the machine but it doesn’t mean that you can use Invoke-Command to send a request.

I think a better test is to use Test-WSMan

It will test if the WinRm service is running (won’t test if remoting is enabled)

$computers = “$env:COMPUTERNAME”, ‘NotFound’

foreach ($computer in $computers){
$target = $computer
if (Test-WSMan -ComputerName $computer -ErrorAction Ignore) {
Invoke-Command -ComputerName $computer -ScriptBlock {Get-Service}
}
else {
Write-Warning -Message “Couldn’t connect to $computer”
}

}

You can push the output to file or put the unreachable machine names into a file if you need to record them.

 

October 23, 2015  10:08 AM

UK PowerShell User Group

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Two meetings of the UK PowerShell group have been announced

Tuesday 24 November in Manchester

Thursday 26 November at Rackspace, in Hayes Middlesex

Details from  www.get-psuguk.eventbrite.co.uk

There is a rumour that I might be speaking at the 26 November event


October 20, 2015  1:49 PM

PowerShell and SSH

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell, SSH

The PowerShell team have announced the availability of OpenSSH 7.1 for Windows – http://blogs.msdn.com/b/powershell/archive/2015/10/19/openssh-for-windows-update.aspx

This is their first step in bringing SSH support into PowerShell.

The roadmap for next steps is available on their blog at the link above


October 19, 2015  5:00 AM

PowerShell resources–you tube

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

One place you might not think of looking for PowerShell resources is you tube.  PowerShell.org has its own channel on you tube – https://www.youtube.com/user/powershellorg – where we’ve posted the session recordings from:

PowerShell Summit Europe 2015

PowerShell summit Europe 2014

PowerShell Summit NA 2015

selected sessions from

PowerShell Summit NA 2014 and

PowerShell Summit NA 2013

are also available.

In addition we have a number other recordings available.

Enjoy


October 17, 2015  4:00 AM

WMI linked classes

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

You will find that many WMI classes have links – some are associations built in to WMI (a subject for another time) while other classes can be linked based on property values. An example of the latter is the Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration classes. The DeviceId on Win32_NetworkAdapter  matches Index on Win32_NetworkAdapterConfiguration .

The follwoing function (adapted from a question on the forum) shows one way this link can be used

function Get-NetworkInfo{
[cmdletbinding()]
param( [string]$ComputerName )

$networkinfo = @()

$networks = Get-WmiObject Win32_Networkadapter -Filter ‘NetEnabled=True’ -ComputerName $ComputerName

$adapter = 0
foreach($network in $networks){

$id = $network.DeviceId

$IPinfo = Get-WmiObject win32_networkadapterconfiguration -Filter “Index = $id” -ComputerName $ComputerName
$winServers = 0
$winServers = ($IPinfo.WinsPrimaryServer -join ‘,’),($IPinfo.WinsSecondaryServer -join ‘,’)

$adapter += 1

$props = @{
‘Adapter’ = $adapter;
‘Manufacturer’ = $network.Manufacturer;
‘Description’ = $network.Description;
‘Connection’ = $network.NetConnectionID;
‘SpeedGB’ = [math]::Round($network.Speed / 1GB, 2)
‘IPAddress’ = $IPinfo.IPAddress -join ‘,’
‘Submask’ = $IPinfo.IPSubnet -join ‘,’
‘Gateway’ = $IPinfo.DefaultIPGateway -join ‘,’
‘DNSServers’ = $IPinfo.DnsServerSearchOrder-join ‘,’
‘WinServers’ = $winServers -join ‘,’
‘DomainSuffixes’ = $IPinfo.DNSDomainSuffixSearchOrder -join ‘,’
}

$networkinfo += New-Object -TypeName psobject -Property $props
}
$networkinfo
}
Get-NetworkInfo -ComputerName $env:COMPUTERNAME


October 16, 2015  5:40 AM

Manning Deal of The Day – 17 October 2015

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

A treat for PowerShell users looking for books. 50% off PowerShell in Depth, second edition and PowerShell in Action, third edition on 17 October 2015

Half off Windows PowerShell in Action, Third Edition. Use code dotd101715au at https://www.manning.com/books/windows-powershell-in-action-
third-edition

Half off Powershell in Depth, Second Edition. Use code dotd101715au at https://www.manning.com/books/powershell-in-depth-second-edition

I’m co-author of both books and while I may be a touch biased about it I think this is n excellent opportunity to get 2 great PowerShell books

 


October 14, 2015  5:07 AM

Modifying MAC address

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

 

Another question on the forum brought up an interesting point. User want adapter name and mac address

PS> Get-NetAdapter | select Name, MacAddress

Name                         MacAddress
—-                         ———-
WiFi                         28-18-78-D0-05-11
Bluetooth Network Connection 28-18-78-D0-05-12

but wanted to remove the hyphens in the mac address so tried

PS> Get-NetAdapter | select Name, MacAddress | foreach {$_.MacAddress -replace ‘-‘,”}
281878D00511
281878D00512

This doesn’t work because the foreach cmldet only knows to put put the new string when the hyphens have been replaced in the mac address. The name is effectively filtered out.

The answer is to use a calculated field in select-object like this

PS> Get-NetAdapter | select Name, @{N=’MacAddress’; E={$_.MacAddress -replace ‘-‘,”}}

Name                         MacAddress
—-                         ———-
WiFi                         281878D00511
Bluetooth Network Connection 281878D00512

N = Name and E= Expression though they are usually shorten for brevity as shown


October 14, 2015  4:59 AM

NICs with IP addresses

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

A question on the forum asked about discovering those network adapters that had IP addresses configured. The user had tried

PS> Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter “IPAddress IS NOT NULL”
Get-WmiObject : Invalid query “select * from Win32_NetworkAdapterConfiguration where IPAddress IS NOT NULL”
At line:1 char:1
+ Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter “IPAdd …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Actually they’d tried the full WQL query. I don’t do that because its more typing than using the class name and a filter.

The problem is that IPAddress is a string array and WQL won’t let you query against an array of properties.

You have 2 choices

Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where IPAddress

which is fine fro local machines but means you are filtering out results on the local machine when querying a remote machine – wasteful.

A better solution is to use the IPEnabled property. This is a boolean that is set to TRUE when an IP address (static or DHCP) has been set on a NIC. Your query then becomes

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter “IPEnabled = $true”

OR

Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -Filter “IPEnabled = $true”


October 13, 2015  10:45 AM

Microsoft Edge Browser

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Windows 10

For the most part I’ve been quite happy with the new Edge browser in Windows 10. One of the irritations was that I could only have one instance open at a time. This appears to have been resolved in the Windows Insider Preview build 10565 which allows multiple instances.

Nice


October 13, 2015  10:38 AM

NSlookup in PowerShell

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DNS, Powershell

nslookup.exe is a command line executable that is used to discover the IP address of a remote machine from its FQDN. The results look something like this:

PS> nslookup powershell.org
DNS request timed out.
timeout was 2 seconds.
Server:  UnKnown
Address:  192.168.0.1

Non-authoritative answer:
DNS request timed out.
timeout was 2 seconds.
Name:    powershell.org
Addresses:  104.28.15.25
104.28.14.25

The output is text – not the most useful of things to use.

One option if you want to perform lookups in PowerShell is to write your self a script

$ns = (nslookup.exe powershell.org )[-4..-3]
$lookup = [PSCustomObject]@{
Name = ($ns[0] -split ‘:’)[1].Trim()
Address = ($ns[1] -split ‘:’)[1].Trim()
}
$lookup

Run nslookup and take the 3rd and 4th lines from the end of the output. Then create an output object where the text in the array is split at the : the second element is used and trimmed of blank spaces.

If you like using .NET static methods you can do this:

PS> [System.Net.DNS]::GetHostEntry(‘powershell.org’)

HostName       Aliases AddressList
——–       ——- ———–
powershell.org {}      {104.28.15.25, 104.28.14.25}

Best of all is the use the Resolve-DnsName cmdlet from the DnsClient module that’s present in Windows 8 and later

PS> Resolve-DnsName -Name ‘powershell.org’ | ft -a

Name           Type TTL Section IPAddress
—-           —- — ——- ———
powershell.org A    300 Answer  104.28.15.25


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: