PowerShell for Windows Admins


August 31, 2016  12:01 PM

Windows 10 updates

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Windows 10

With the anniversary update delivered the Windows 10 development carries on with new updates becoming available – now up to build 14915

Not much in the way of PowerShell goodies though  🙁

August 31, 2016  10:02 AM

Access functions

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I had a request for the Access functions I wrote about in this post

https://richardspowershellblog.wordpress.com/2009/12/18/access-functions/

They are now available from

https://onedrive.live.com/?id=43CFA46A74CF3E96%2179699&cid=43CFA46A74CF3E96


August 30, 2016  12:26 PM

Optimising WMI calls–part 3

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

The next change just uses 1 call to get the disk information instead of 2

Measure-Command -Expression {

$srvs = ‘W16TP5TGT01’, ‘W16TP5TGT02′

for ($i=1; $i -le 150; $i++){

foreach ($srv in $srvs) {
$cs = New-CimSession -ComputerName $srv
$bootupMemory = Get-CimInstance -Query “SELECT * FROM Win32_OperatingSystem” -CimSession $cs
$cpuLoad = Get-CimInstance -Query “SELECT * FROM Win32_Processor” -CimSession $cs

$tSessions = Get-CimInstance -Query “SELECT * FROM Win32_TerminalService” -CimSession $cs

$sfilt = “Name=’imaservice’ OR Name=’mfcom’ OR Name=’cpsvc’ OR Name=’msmq'”
$reqserv = Get-CimInstance -ClassName Win32_Service -Filter $sfilt -CimSession $cs

$ima = $reqserv | where Name -eq ‘imaservice’
$mfcom = $reqserv | where Name -eq ‘mfcom’
$ctxPrintMgr = $reqserv | where Name -eq ‘cpsvc’
$msmqstatus = $reqserv | where Name -eq ‘msmq’

$dfilt = “Deviceid=’c:’ OR Deviceid=’D:'”
$drives = Get-CimInstance -ClassName Win32_Logicaldisk -Filter $dfilt -CimSession $cs

$cDrive = $drives | where deviceid -eq ‘c:’
$dDrive = $drives | where deviceid -eq ‘d:’
Remove-CimSession -CimSession $cs
}
}
}

Time now becomes

Days              : 0
Hours             : 0
Minutes           : 6
Seconds           : 36
Milliseconds      : 923
Ticks             : 3969235528
TotalDays         : 0.00459402260185185
TotalHours        : 0.110256542444444
TotalMinutes      : 6.61539254666667
TotalSeconds      : 396.9235528
TotalMilliseconds : 396923.5528

Not such a dramatic change but overall we’re now taking 26.4% less time to run the code.


August 30, 2016  11:41 AM

Optimising WMI calls–part 2

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

Last time we looked at using CIM sessions to make a set of WMI calls run quicker. This time we’ll reduce the number of calls.  I’m deliberately just reducing the number of calls to the Win32_Service class.  We’ll look at the disks another time

Our code becomes

Measure-Command -Expression {

$srvs = ‘W16TP5TGT01’, ‘W16TP5TGT02′

for ($i=1; $i -le 150; $i++){

foreach ($srv in $srvs) {
$cs = New-CimSession -ComputerName $srv
$bootupMemory = Get-CimInstance -Query “SELECT * FROM Win32_OperatingSystem” -CimSession $cs
$cpuLoad = Get-CimInstance -Query “SELECT * FROM Win32_Processor” -CimSession $cs

$tSessions = Get-CimInstance -Query “SELECT * FROM Win32_TerminalService” -CimSession $cs

$sfilt = “Name=’imaservice’ OR Name=’mfcom’ OR Name=’cpsvc’ OR Name=’msmq'”
$reqserv = Get-CimInstance -ClassName Win32_Service -Filter $sfilt -CimSession $cs

$ima = $reqserv | where Name -eq ‘imaservice’
$mfcom = $reqserv | where Name -eq ‘mfcom’
$ctxPrintMgr = $reqserv | where Name -eq ‘cpsvc’
$msmqstatus = $reqserv | where Name -eq ‘msmq’

$cDrive = Get-CimInstance -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’c:'” -CimSession $cs
$dDrive = Get-CimInstance -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’d:'” -CimSession $cs
Remove-CimSession -CimSession $cs
}
}
}

The change is to create a filter that pulls back JUST the services we want. Use that to create a collection of Win32_Service objects and then populate the variables with the required service data

Time drops dramatically

Days              : 0
Hours             : 0
Minutes           : 6
Seconds           : 50
Milliseconds      : 133
Ticks             : 4101339515
TotalDays         : 0.0047469207349537
TotalHours        : 0.113926097638889
TotalMinutes      : 6.83556585833333
TotalSeconds      : 410.1339515
TotalMilliseconds : 410133.9515

Total time goes from 539.42 seconds to 410.13 seconds.  That’s reduced the time by 23.96%

These are just simple coding changes remember- we’re not performing any clever parallel processing here


August 30, 2016  11:12 AM

PowerShell speaker opportunities still open

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

You have just over a month to register your session proposals for the 2017 PowerShell Summit – https://powershell.org/2016/08/01/powershell-and-devops-global-summit-2017-call-for-topics/


August 29, 2016  1:37 PM

Optimising WMI calls–part 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Recently saw some code where user was running this

$bootupMemory = gwmi -Query “SELECT * FROM Win32_OperatingSystem” -ComputerName $srv
$cpuLoad = gwmi -Query “SELECT * FROM Win32_Processor” -ComputerName $srv

$tSessions = gwmi -Query “SELECT * FROM Win32_TerminalService” -ComputerName $srv

$ima = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’imaservice'” -ComputerName $srv
$mfcom = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’mfcom'” -ComputerName $srv
$ctxPrintMgr = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’cpsvc'” -ComputerName $srv
$msmqstatus = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’msmq'” -ComputerName $srv

$cDrive = gwmi -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’c:'” -ComputerName $srv
$dDrive = gwmi -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’d:'” -ComputerName $srv

against 300 machines.  There were some more calls they involved WMI classes installed by Citrix which I don’t use in my lab

Question was why was it running slow

Two thoughts initially are that repeated calls to Get-WmiObject involve creating, using and removing DCOM connections. This assumes that DCOM isn’t blocked by a firewall or the network. Using a single CIM session should speed up the process.

Secondly making multiple calls to the same class is inefficient.

In addition using the WQL query involves more typing which makes things more difficult to maintain.

Last point is that everyone knows how much I love aliases so you won’t be surprised if I point out that using them is BAD

I don’t have 300 servers in my lab – though using nano server VMs on a machine with 64GB ram you could do that – so I used some PowerShell looping to get round that

Measure-Command -Expression {

$srvs = ‘W16TP5TGT01’, ‘W16TP5TGT02’

for ($i=1; $i -le 150; $i++){

foreach ($srv in $srvs) {
$bootupMemory = gwmi -Query “SELECT * FROM Win32_OperatingSystem” -ComputerName $srv
$cpuLoad = gwmi -Query “SELECT * FROM Win32_Processor” -ComputerName $srv

$tSessions = gwmi -Query “SELECT * FROM Win32_TerminalService” -ComputerName $srv

$ima = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’imaservice'” -ComputerName $srv
$mfcom = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’mfcom'” -ComputerName $srv
$ctxPrintMgr = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’cpsvc'” -ComputerName $srv
$msmqstatus = gwmi -Query “SELECT * FROM Win32_Service WHERE name=’msmq'” -ComputerName $srv

$cDrive = gwmi -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’c:'” -ComputerName $srv
$dDrive = gwmi -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’d:'” -ComputerName $srv
}
}
}

Measure command will run the commands but it reports the time taken rather than the results

I have 2 servers and loop 150 times round connecting to them and getting the results

Some caching of connectivity information occurs but this is close enough

Days              : 0
Hours             : 0
Minutes           : 8
Seconds           : 59
Milliseconds      : 419
Ticks             : 5394194858
TotalDays         : 0.00624328108564815
TotalHours        : 0.149838746055556
TotalMinutes      : 8.99032476333333
TotalSeconds      : 539.4194858
TotalMilliseconds : 539419.4858

A time of just under 9 minutes isn’t bad. This is fast enough to run during the day and can definitely be run over night

But we should be able to do better than that

Lets use a CIM session instead of individual DCOM sessions

Measure-Command -Expression {

$srvs = ‘W16TP5TGT01’, ‘W16TP5TGT02’

for ($i=1; $i -le 150; $i++){

foreach ($srv in $srvs) {
$cs = New-CimSession -ComputerName $srv
$bootupMemory = Get-CimInstance -Query “SELECT * FROM Win32_OperatingSystem” -CimSession $cs
$cpuLoad = Get-CimInstance -Query “SELECT * FROM Win32_Processor” -CimSession $cs

$tSessions = Get-CimInstance -Query “SELECT * FROM Win32_TerminalService” -CimSession $cs

$ima = Get-CimInstance -Query “SELECT * FROM Win32_Service WHERE name=’imaservice'” -CimSession $cs
$mfcom = Get-CimInstance -Query “SELECT * FROM Win32_Service WHERE name=’mfcom'” -CimSession $cs
$ctxPrintMgr = Get-CimInstance -Query “SELECT * FROM Win32_Service WHERE name=’cpsvc'” -CimSession $cs
$msmqstatus = Get-CimInstance -Query “SELECT * FROM Win32_Service WHERE name=’msmq'” -CimSession $cs

$cDrive = Get-CimInstance -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’c:'” -CimSession $cs
$dDrive = Get-CimInstance -Query “SELECT * FROM Win32_Logicaldisk WHERE deviceid=’d:'” -CimSession $cs
Remove-CimSession -CimSession $cs
}
}
}

Time is now

Days              : 0
Hours             : 0
Minutes           : 8
Seconds           : 31
Milliseconds      : 839
Ticks             : 5118397269
TotalDays         : 0.00592407091319444
TotalHours        : 0.142177701916667
TotalMinutes      : 8.530662115
TotalSeconds      : 511.8397269
TotalMilliseconds : 511839.7269

That’s a 5% speed increase for minimal coding effort

Next step is to remove the redundant WMI calls which we’ll do in the next post


August 18, 2016  9:59 AM

PowerShell is Open Sourced

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

For those of you that have been at PowerShell events over the last few years you’ll have heard Jeffrey Snover state that he wanted to take PowerShell to other platforms.

Now its happened

Jeffrey has announced that an ALPHA release of PowerShell is now available for Linux and Mac.  Currently available for Ubuntu, Centos, Red Hat and Mac OS X with more to come

The announcement is at

https://azure.microsoft.com/en-us/blog/powershell-is-open-sourced-and-is-available-on-linux/

Also see PowerShell blog

https://blogs.msdn.microsoft.com/powershell/2016/08/18/powershell-on-linux-and-open-source-2/

Some  points to note:

ISE isn’t available as part of the alphas release but VSCode is available for Linux and Mac giving an consistent editor across the platforms

PowerShell remoting will be extended to use Open SSH as well as WSMAN

Planned enhancements include:

Additional Linux Distros covered – parity with .NET Core.

Writing Cmdlets in Python and other languages

PSRP over OpenSSH

WSMan based remoting to downlevel versions of Windows and WSMan based PSRP on Linux.

Editor Services and auto-generated GUI

Unix-style wildcard expansion

Increasing test code coverage for Windows and Linux editions

Continue increasing cmdlet coverage for Linux and Windows

REMEMBER this an ALPHA release – there’s still a lot to do and its a open source project so community effort is required

Enjoy


August 16, 2016  1:46 PM

Update-Help errors

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Updatable help brings the benefit of up to date help with typos fixed and new edge cases described. The down side is that it sometimes fails:

PS> Update-Help -Force
Update-Help : Failed to update Help for the module(s) ‘Microsoft.PowerShell.Operation.Validation’
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 -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCo
mmand

Update-Help : Failed to update Help for the module(s) ‘PSScriptAnalyzer’ with UI culture(s) {en-US} : The Help content at the specified location is not valid. Specify a location that contains valid Help Content.
At line:1 char:1
+ Update-Help -Force
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Update-Help], Exception
+ FullyQualifiedErrorId : HelpContentXmlValidationFailure,Microsoft.PowerShell.Commands.UpdateHe
lpCommand

The failure messages are relatively self-explanatory if you read the details. The problem usually boils down to the needed help files not being available at the URI provided in the module manifest. This often occurs with new modules – the creation of the help files lags well behind the creation of the module usually.

Unfortunately there isn’t much you can do apart from try to find examples of the modules use online


August 10, 2016  10:41 AM

What you don’t know

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
IT skills, knowledge

This article – https://powershell.org/2016/08/08/what-are-your-known-problems-solved-in-dsc/ – started me thinking about the times I’ve worked on big projects.

One of the things we’d do was discuss things that could become problems. Knowledge falls into three groups:

– things you know

– things you don’t know – you know that X is a thing but you don’t know its value

– things you don’t know you don’t know

The last is the one that hurts. Its the things that you don’t know you don’t know that cause the surprises – for example the sudden realisation at 2am that application A won’t install if application B is install. Its not documented and you didn’t know you didn’t know that fact. Now you have to rethink your whole approach.

How do we get round this?

One way is experience – you remember what’s caused problems in the past and you check for those things. Its often said that good judgement comes from experience which comes from bad judgement!

The other way is research. Too many times I’ve seen people assume that something will work because something similar worked in the past. Some bad examples are around treating a new version of Windows the same as the current version or even worse the version you started with 10 years ago.

IT is constantly evolving. One of the things with being a professional (IT pro) is keeping up with your subject. Have you? What don’t you know you don’t know?

Continuing education is going to be a must do activity for IT pros going forward with automation & devops related skills being top of the must learn pile.


August 6, 2016  9:23 AM

Kindle Fire extinction

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
General

Today my Kindle Fire tablet finally stopped working. Its been more than temperamental for the last 12 months – freezing at least once or twice a day, not responding to touch input, not downloading content but today it finally stopped.

Will I buy another one?

No. The whole raft of problems with it freezing seemed to start when Amazon started updating the OS. None of the updates have fixed the problem.

I’ll either revert to my old Kindle that just keeps on working or get something else.

Come on Amazon – you had a great product. It isn’t anymore. Solve the problems and you might get some custom


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: