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.