PowerShell for Windows Admins


October 2, 2015  8:24 AM

Quick update check

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell, WSUS

Want to check on any updates that haven’t been fully applied in your environment. Run this on your WSUS server (2012 R2)

£> Get-WsusUpdate -Classification All -Status Any -Approval AnyExceptDeclined |
where InstalledOrNotApplicablePercentage -ne 100

You can modify the filters:

Classification = one of All, Critical, Security, WSUS

Status = one of NoStatus, InstalledOrNotApplicable, InstalledOrNotApplicableOrNoStatus, Failed, Needed, FailedOrNeeded, Any

Approval = one of Approved, Unapproved, AnyExceptDeclined, Declined

October 1, 2015  2:47 AM

Strings and collections

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A question on the forum brought up a point that often confuses PowerShell novices.

QUESTION: What’s the difference between

$computers = ‘W12R2SUS, W12R2DSC’

and

$computers = ‘W12R2SUS’, ‘W12R2DSC’

ANSWER:

The first is a string

PS> $computers.GetType() | ft -a

IsPublic IsSerial Name   BaseType
——– ——– —-   ——–
True     True     String System.Object

 

The second is an array – a collection of strings

PS> $computers.GetType() | ft -a

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

The difference is important when you try to use $computers as the value for a parameter where you want to supply a list of computer names:

if you use the string – you’ll get an error

PS> $computers = ‘W12R2SUS, W12R2DSC’
PS> $ps = New-PSSession -ComputerName $computers
New-PSSession : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri
parameter, or pass URI objects instead of strings.
At line:1 char:7
+ $ps = New-PSSession -ComputerName $computers
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (System.String[]:String[]) [New-PSSession], ArgumentException
+ FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.NewPSSessionCommand

 

You have to use an array

PS> $computers = ‘W12R2SUS’, ‘W12R2DSC’
PS> $ps = New-PSSession -ComputerName $computers
PS> $ps

Id Name            ComputerName    State         ConfigurationName     Availability
— —-            ————    —–         —————–     ————
2 Session2        W12R2DSC        Opened        Microsoft.PowerShell     Available
1 Session1        W12R2SUS        Opened        Microsoft.PowerShell     Available

A trap that everyone falls into at some time or other but one that can be avoided


September 30, 2015  1:16 PM

PowerShell DSC for Linux 1.1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Linux, Powershell

PowerShell DSC for Linux 1.1 has been released.

http://blogs.msdn.com/b/powershell/archive/2015/09/30/powershell-dsc-for-linux-version-1-1-is-now-available-and-new-linux-resources.aspx

With bug fixes, support for the separation of configuration and node ids  and new resources this release makes it easier than ever to manage your Linux boxes through DSC


September 30, 2015  12:24 PM

Controlling copies

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I was recently asked about ‘forcing’ a copy so that only the files you want are copied

Consider a folder with lots of files. I want to copy those files that start with the letter t and have a txt extension

PS> Get-ChildItem -Path C:\test2\t*.txt
  Directory: C:\test2
Mode                LastWriteTime         Length Name
—-                ————-         —— —-
-a—-       08/08/2015     14:14              0 test.txt
-a—-       08/08/2015     14:16              0 test1.txt
-a—-       08/08/2015     14:17              0 test2.txt
-a—-       08/08/2015     14:17              0 test3.txt
-a—-       08/08/2015     14:17              0 test4.txt

I want to copy to c:\test1 which is empty

PS> Get-ChildItem -Path C:\test1\
PS>

Defining the copy is simply a matter of setting the path to files you want

PS> Copy-Item -Path C:\Test2\t*.txt -Destination C:\test1\ -Verbose
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test.txt Destination: C:\test1\test.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test1.txt Destination: C:\test1\test1.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test2.txt Destination: C:\test1\test2.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test3.txt Destination: C:\test1\test3.txt”.
VERBOSE: Performing the operation “Copy File” on target “Item: C:\Test2\test4.txt Destination: C:\test1\test4.txt”.
PS> Get-ChildItem -Path C:\test1\
    Directory: C:\test1
Mode                LastWriteTime         Length Name
—-                ————-         —— —-
-a—-       08/08/2015     14:14              0 test.txt
-a—-       08/08/2015     14:16              0 test1.txt
-a—-       08/08/2015     14:17              0 test2.txt
-a—-       08/08/2015     14:17              0 test3.txt
-a—-       08/08/2015     14:17              0 test4.txt


September 30, 2015  8:23 AM

WMF 5.0 production preview–installed

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

I’ve installed WMF 5.0 PP on a number of Windows 2012 R2 (full GUI and server core). Installation was over the top of a mixture of PowerShell 4.0 and various versions of PowerShell 5.0 previews.

Installation worked in all cases.

I’ve not noticed any problems with using powershell 5.0 in this environment


September 29, 2015  1:42 PM

Win32_ReliabilityRecords Class

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

The Win32_ReliabilityRecords class was introduced with Windows 7. It contains information from the Windows Event Log related to system reliability.

The most interesting properties are the Message and the TimeGenerated

£> Get-WmiObject -class win32_reliabilityRecords | select -First 1 | fl Message, TimeGenerated
Message       : Installation Successful: Windows successfully installed the following update: Definition Update for  Windows Defender – KB2267602 (Definition 1.207.1367.0)
TimeGenerated : 20150929170914.620000-000

Notice the usual delightful date format on TimeGenerated

Easiest way to resolve this and get sensibly formatted date is to use the CIM cmdlets

£> Get-CimInstance -ClassName win32_reliabilityRecords | select -First 1 | fl Message, TimeGenerated
Message       : Installation Successful: Windows successfully installed the following update: Definition Update for  Windows Defender – KB2267602 (Definition 1.207.1367.0)
TimeGenerated : 29/09/2015 18:09:14

Now you have a date you can read easily.


September 29, 2015  8:47 AM

Everything is recycled

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DevOps

I was just thinking this morning about the DevOps phenomenon. When I started in IT it wasn’t unusual for people to do development work and operations – especially in smaller organisations.

Over the years this changed and specialisation became the name of the game – you were a C# developer or a java developer or a Windows admin or a network guy…

|Now with DevOps being the flavour of the month the admin guy, or developer, who can at least have an intelligent conversation about the other guys issue’s is in big demand again.

The cynical side wonders how long before someone has the bright idea that separating developers to concentrate on development and ops staff to concentrate on admin stuff is more efficient  🙂


September 29, 2015  5:32 AM

Error handling for DNS lookups

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DNS, Powershell

Interesting question on the forum regarding the Resolve-DNSname cmdlet. This is part of the DNSclient module introduced with Windows 8.

If the DNS record is found everything is good

£> Resolve-DnsName W12R2DSC -Server server02 | ft -a

Name                   Type TTL  Section IPAddress
—-                   —- —  ——- ———
W12R2DSC.Manticore.org A    1200 Answer  10.10.54.204

If the record isn’t found you get an error

£> Resolve-DnsName W12R2DSC3 -Server server02
Resolve-DnsName : W12R2DSC3 : DNS name does not exist
At line:1 char:1
+ Resolve-DnsName W12R2DSC3 -Server server02
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (W12R2DSC3:String) [Resolve-DnsName], Win32Exception
+ FullyQualifiedErrorId : DNS_ERROR_RCODE_NAME_ERROR,Microsoft.DnsClient.Commands.ResolveDnsName

If you want to gracefully handle that error you use try-catch

$computer = ‘W12R2DSC3’
try {
Resolve-DnsName $computer -Server server02 -ErrorAction Stop
}
catch {
Write-Warning -Message “Record not found for $computer”
}


September 28, 2015  1:28 PM

PowerShell and DevOps Global Summit–call for topics extended

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
DevOps, Powershell

The call for topics for the PowerShell and DevOps Global Summit (renamed PowerShell Summit) has been extended.

If you’re thinking of submitting please do so.

If you want some ideas fro topics we’ll consider – see http://powershell.org/wp/2015/09/16/speaking-at-powershell-summit-2016-topic-ideas-for-aspiring-speakers/


September 28, 2015  12:28 PM

Updating help problem

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A question on the forum posed this question – why does update-help fail on Windows 10 in this case:

£> Update-Help -Module PSDesiredStateConfiguration -Force
Update-Help : Failed to update Help for the module(s) ‘PSDesiredStateConfiguration’ with UI culture(s) {en-GB} :
Unable to retrieve the HelpInfo XML file for UI culture en-GB. Make sure the HelpInfoUri property in the module
manifest is valid or check your network connection and then try the command again.
At line:1 char:1
+ Update-Help -Module PSDesiredStateConfiguration -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand

The usual reason is that Update-Help can’t find the help file. This is either because the URL in the module manifest is wrong or the help file isn’t available yet. In this case I suspect the help file isn’t available yet as DSC may still change as Server 2016 hasn’t shipped yet.


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: