PowerShell for Windows Admins


September 20, 2013  1:51 PM

ErrorAction and WarningAction

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

You’ve all probably done something like this:

£> Get-Process -Name xyz
Get-Process : Cannot find a process with the name “xyz”. Verify the process name and call the cmdlet again.
At line:1 char:1
+ Get-Process -Name xyz
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (xyz:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

First though of many people is to do this

Get-Process -Name xyz -ErrorAction silentlycontinue

Now, thats not good as you don’t know that an error has occurred. Its better to deal with it rather than suppress it so you wrap it in a try-catch:

£> try {
>> Get-Process -Name xyz -ErrorAction Stop
>> }
>> catch{
>> “oops”
>> }
>>
oops

In reality you would want to do more than say “oops”

There are a number of situations where cmdlets give you a warning rather than an error. if you want to suppress them use –WarningAction. It has the same options as –ErrorAction

SilentlyContinue. Suppresses the warning message and continues
executing the command.

Continue. Displays the warning message and continues executing
the command. “Continue” is the default value.

Inquire. Displays the warning message and prompts you for
confirmation before continuing execution. This value is rarely
used.

Stop. Displays the warning message and stops executing the
command.

More details in about_Common_Parameters

September 20, 2013  2:39 AM

Using WMI methods with the CIM cmdlets

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Using WMI class methods with the CIM cmdlets can cause a lot of confusion. This article, of mine, should clear up that confusion for you.

http://blogs.technet.com/b/heyscriptingguy/archive/2013/09/20/hey-dude-where-are-my-methods.aspx


September 20, 2013  1:41 AM

iOS7–1 giant step–backwards

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Just upgraded my ipad to iOS7 overnight. It looks awful. Like a child’s toy. Apple may have been a triumph of style over substance in the past but they’ve lost any claim to style with this one.


September 18, 2013  1:47 PM

Module versions in PowerShell 4.0

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Comparing a Windows 8.1 build with a Windows 8 machine I think these modules remain the same between Windows 8/PowerShell 3 and Windows 8.1/PowerShell 4

BitLocker
BitsTransfer
BranchCache
CimCmdlets
DirectAccessClientComponents
DnsClient
iSCSI
ISE
Kds
Microsoft.PowerShell.Diagnostics
Microsoft.PowerShell.Host
Microsoft.PowerShell.Security
Microsoft.WSMan.Management
MMAgent
MsDtc
NetConnection
NetSwitchTeam
NetTCPIP
NetworkConnectivityStatus
NetworkTransition
PKI
PSDiagnostics
PSWorkflowUtility
ScheduledTasks
TroubleshootingPack
Wdac
WindowsDeveloperLicense
WindowsErrorReporting

While these modules appear to have changed – at least with the version number

1.0.0.0 AppBackgroundTask
2.0.0.0 AppLocker
2.0.0.0 Appx
1.0.0.0 AssignedAccess
1.0 Defender
2.0 Dism
2.0.0.0 International
3.1.0.0 Microsoft.PowerShell.Management
3.1.0.0 Microsoft.PowerShell.Utility
2.0.0.0 NetAdapter
1.0.0.0 NetEventPacketCapture
2.0.0.0 NetLbfo
1.0.0.0 NetNat
2.0.0.0 NetQos
2.0.0.0 NetSecurity
1.0.0.0 PcsvDevice
1.1 PrintManagement
1.0 PSDesiredStateConfiguration
1.1.0.0 PSScheduledJob
2.0.0.0 PSWorkflow
2.0.0.0 SecureBoot
2.0.0.0 SmbShare
2.0.0.0 SmbWitness
1.0.0.0 StartScreen
2.0.0.0 Storage
2.0.0.0 TLS
2.0.0.0 TrustedPlatformModule
2.0.0.0 VpnClient
1.0.0.0 WindowsSearch

Some of these are new – look for the 1.0 version numbers. I’ll be working through the changes over the next few weeks. More to come


September 17, 2013  1:04 PM

Death of the command line?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I saw this today “the desktop is going to disappear, just like the command line before it” in http://windowsitpro.com/windows-8/windows-desktop-death

Has the guy never heard of PowerShell?


September 16, 2013  11:15 AM

PowerShell Summit NA 2014

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Public registration for next years PowerShell Summit is now open.

http://powershell.org/wp/2013/09/15/powershell-summit-n-a-2014-public-registration-now-open-with-discounts/

The 2013 summit was the PowerShell event of the year. Next years promises to be at least that good.


September 15, 2013  4:16 AM

PowerShell help

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

There’s an announcement from the team producing PowerShell help that most of the help for PowerShell 4.0/Windows 8.1/Windows 2012 R2 is complete.

You can see the latest versions of the help files at

http://technet.microsoft.com/en-us/library/dn249523.aspx

The version numbers may not be fully up to date but they will be corrected

The link is worth bookmarking for future use


September 15, 2013  3:44 AM

PowerShell whimsy with arrays

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I was thinking about the Christmas song – 12 days of Christmas and wondering just how many presents were delivered. Don’t ask why!

So I decided to work it out.

$days = 12
$presents = 1..$days

$total = 0
0..($days-1) | foreach {
$total += $presents[$_] * $presents[(-1 * $presents[$_] )]
}
$total

You start with 12 days & create an array containing the values 1 to 12

Set the total to 0 & loop through the days. Because array indices start at 0 you need the range 0-11 which you can calculate as 0..($days-1)

The total number of presents delivered of a type is the number of things 1 partridge, 2 turtle doves etc multiplied by the number of times its delivered 12, 11, 10 etc

The number of presents of a particular type is from the array $presents[$_]. The days delivered is found by getting an inverse relationship into the array. So present 1 delivered 12 times which is value of last element; present 2 delivered 11 times which is next to last element etc.

Now the point of the post is to show you that you can access the last element of an array using an index of –1; the next to last using –2 and so on.

As we want to calculate this we end up with taking the value of the array for that point and multiplying by –1 to get the value we need fo r the multiplication

$presents[(-1 * $presents[$_] )]

Add the calculated values together and you get the total number of presents delivered.

I’ve deliberately put more () into this than needed to make the logic easier to follow.


September 12, 2013  1:35 PM

Windows 2012 activation

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Following the nightmare of my last post – don’t what I was doing last night – it seems that Windows 2012 R2 will activate as soon as an internet connection is established.

If you attempt to manually activate (not noticing that its happened already) it will ask for a product. Still doesn’t appear to accept the key I’d already input during install which seems odd.

At least its working


September 11, 2013  2:43 PM

Windows 2012 R2–ouch

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The other day I downloaded Windows Server 2012 R2. Goodie I thought I’ll download that. Download worked. Installed 2012 R2 in to a VM and started to configure it tonight.

Went to activate Windows and it asked for a product key. No biggie as I grabbed that during the download.

Entered the key and it was rejected. Huh. Assumed a mistype and tried again. Still rejecting. Went back to MSDN and got a second key. Rejected again.

Next steps – trash the VM & re-download & rebuild.

I do love doing things a second time for no apparent reason.


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: