Friday, 24 June 2011

Online cmdlet help

Not long after PowerShell v2 was released I did a presentation to most of the tech staff in the department which I pretty much simply wanted to use as an opportunity to give them a heads-up that they had better start thinking about upping their knowledge of PS.  Apart from demo-ing some of the cool stuff you could do with PowerShell I wanted to make sure that the audience left remembering three simple things that would help them help them start using it:

  1. verb-noun
    Cmdlet format is simply ‘what do you want to do’ hyphen ‘what do you want to do it to?’.
  2. get-command
    Want a list of available cmdlets?  This’ll do it.
  3. get-help
    If you can remember 1 & 2 then this cmdlet is all you need to learn more.

The get-help cmdlet uses locally stored info so is current at the time of installation and in the majority of cases is sufficient.  Something I found out yesterday was that you can use the –online parameter with get-help and if you’ve got a connection to the outside world i will bring up the most up-to-date help on TechNet, e.g.

get-help –online get-acl

will load…

image

Saturday, 21 May 2011

Hex / Binary / Decimal / Octal number conversion in WinDbg

Normally if I need to do any conversions between different number formats I’ll just do it in my head (yeah, right!) or load calc.exe.   A while back one of my posts showed how easy it was to convert numbers in PowerShell e.g.

[Convert]::ToString(734, 2)

will convert 734 to binary, and

[Convert]::ToString(179, 16)

will convert 179 to hexadecimal.

It’s just as easy to do it in WinDbg if you need to do any conversions while debugging using .formats.  The benefit of .formats is that it will give you all the available conversions it can, so if you were to enter .formats 0y1100011 you would see:

image

To make sure that WinDbg is aware of the number format you are entering as a parameter you give it the relevant prefix: 0y (binary), 0x(hexadecimal), 0t (octal) & 0n (decimal).  You can also set the default number base using the n command which means all numbers entered will be interpreted as such. Just entering n will show the current number base e.g.

image

And to change it to octal…

image

Thursday, 28 April 2011

Disabling sleep & hibernation on Win 7 in lab environments

Developing and supporting a desktop infrastructure for a University brings with it challenges that you may not normally face in a more corporate environment, specifically about 2500 PCs in labs and open-access areas that can potentially be used by our thousands of student users and trying to a maintain the balance between usability and security/supportability.  One such challenge is making sure that once a student has finished using a PC they can’t (easily) leave it an unusable state for the next user by either locking the workstation or putting it into a sleep or hibernated state.

To prevent a user from locking their workstation is simply a case of using group policy and setting the following in User Configuration:

image 

There are already GPO settings to prevent users from initiating sleep states but they seem to be an all-or-nothing kind of solution:

image 

I think the above is probably designed for kiosk-type machines or maybe those running some kind of display that you would not want to shutdown, and is certainly not ideal for some of our labs that are configured to dual-boot (although it would do a good job of preventing our students from getting any where near the Linux build Winking smile).  In the end I came up with GPO-based solution combining an existing GPO setting as well a single registry tweak, first off disabling the use of sleep states S1-S3:

image

All our student PCs are desktops so when we start making notebooks available the setting for machines running on battery will also need to be configured.  The next task was to disable hibernation.  From an elevated command prompt you would simply run:

powercfg –h off

..but there isn’t an equivalent GPO setting,  To achieve the same result I just used a GPO Preference setting to make the necessary change in the registry:

image

Setting the value to 1 re-enables hibernation.

I may end up making the registry change during build time by adding it to a MDT task sequence just so that it’s already present and one less GPO setting for the client to process.

Tuesday, 19 April 2011

Doing a 'start /wait' in PowerShell

Countless times I’ve had to write a script that would start a process, normally some sort of installation routine, but have needed to ensure that the process had finished doing its thing before passing control back to the script.  Normally if you’re writing a batch/cmd file you’d do something like this..

start /wait notepad.exe  

..or in VBscript something like this..

Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.Run("notepad.exe"),1,True  

As part of our Windows 7 rollout here at the Uni I want to reduce the number of startup/shutdown & logon/logoff scripts and if we have to script something then it would be done in PowerShell.  As such I’ve needed to reproduce the ‘start /wait’ type functionality that I’ve used before in .cmd & .vbs scripts in a .ps1.  After a bit of research seems like the easiest way to do it is with something like this:

$process = [System.Diagnostics.Process]::Start("notepad.exe")
$process.WaitForExit()  

And passing parameters is easy as well, you just need something like:

$process = [System.Diagnostics.Process]::Start("notepad.exe","c:\temp\sometextfile.txt")
$process.WaitForExit()  

Photosynth on the iPhone

Discovered today that Microsoft have made an iOS version of Photosynth available, and best of all it’s free.  Have already tried it out and must admit that it works pretty well.  So okay, the 360ยบ panoramas of my office aren’t that exciting but it’s still a very cool app.  App details can be found here.

Friday, 15 April 2011

Another dose of reality…

Once again Dilbert seems a little too close to reality for comfort… Dilbert.com

Thursday, 14 April 2011

Less is not always more

In the past I’ve made various posts giving examples of how PowerShell can be perfect for the the lazy sysadmin through not having to type as much.  This can be done through using aliases instead of the full cmdlet and truncating parameters (only available with PS v2 remember, you old-school v1 users will have to upgrade).  For example, why type all of…

Get-ChildItem u:\scripts -Include *.ps1 –Recurse

…when you could simply type…

 gci U:\Scripts -i *.ps1 –r

For simply running stuff from within PS the shorter version is ideal.  Problem arises when you start to copy-and-paste your one-liners into a script, that then gets put into production, that then might have to be revised / debugged / fixed by someone else in the future?  Am guilty as charged as have done this myself on numerous occasions, in an attempt to affirm my alpha-geek status.  Trouble is it all falls apart when one of your team asks you what one of your scripts is doing and you struggle to come up with a reasonable explanation.

So what lessons am I going to try and learn from this?

  • If I’m going to insist on trying to make my typing as little as possible I’ll back up my code with enough comments (but then kind of defeats the object of reduced typing!)
  • Use full cmdlet and parameter names in scripts (and comments!) as if I don’t its only going to come back and bite me anyway.

Monday, 11 April 2011

Assigning multiple values in PowerShell

Some time ago I posted a PS one-liner to display the number in the Fibonacci sequence up to 10000000.  I learnt something new about PowerShell today regarding assigning values to variable so thought I’d use the same one-liner to demonstrate.  Originally I assigned a starting value to two variables separately, like this…

$1 = 0; $2 = 1;

The neat trick I found today was that you can do the same with…

$1,$2=0,1;

…which now makes the one-liner now look like…

$1,$2=0,1;$3=$1+$2;do{$3;$1=$2;$2=$3;$3=$1+$2} while ($3 -lt 10000000)

Makes for slightly less typing (well one less “=” anyway, but they all add up for the lazy sysadmin), but its just continues to prove that PowerShell is so flexible in how it lets you use it.