Put your PC into Standby or Hibernate with Powershell

by dstock 4. May 2009 16:00

When ever I leave my desk, I make sure that I lock my workstation for security reasons.  That's fine for security but it doesn't do much for saving power as my computer just sits there idle.   I wrote a quick PowerShell function that puts the computer into Standby.  When I come back it only takes a couple seconds to start back up and I am saving some power.  I added the line below to my $profile

 

$ret = ni -p function: -value {[Reflection.Assembly]::LoadwithPartialname("System.Windows.Forms")>$null;$ret=[System.Windows.Forms.Application]::SetSuspendState(0,0,0);} -name gsb

 

 (gsb is short for Go To Standby)

 Now when I leave my desk for a few minutes I just type in gsb in Powershell and voila, power saving. Remember that this puts all your processes on hold and your computer will be off the network.

For this script to work, your pc must support ACPI enabled.  If you want to put the computer into hibernate instead of standby, change the first 0 to a 1  aka SetSupendState(1,0,0)

In the future I hope to publish a PowerShell Cmdlet  Set-PowerState.  This will allow you via script to set your PC into either Hibernate or Standby (as above).  In addition on supported systems, it will cause them to wake up at a set time.   I have another project I need to finish first but hopefully I'll have a first version out soon.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PowerShell

Convert a SecureString to a String (Plain Text) in PowerShell

by dstock 29. March 2009 18:30

Repost from my old Blog:

In PowerShell I was looking for a way to let a user enter a password while hiding the characters they typed.  My reason for this is that I wanted to pass it as command line parameter to an application.  I found that the built in Read-Host -assecurestring would cover the users typing with astricks but the object returned was a SecureString.

The securestring object does not have a property or method to allow you to retrieve the unencrypted string.  The reason for this is for security.  You need to explicitly write something if you need to get the string.  In many cases you will be able to provide the SecureString to other objects that will use it for such things as credentials

To get the convert a securestring to a string value you need to first use the Marshal Object to convert the SecureString to a intPtr (pointer).  Then use the Marshal's method of PtrToStringAuto to convert the intPtr into a string. 

The example code is:
$secret=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR((read-host -assecurestring)))

If you found this useful, please click one of the Google links.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PowerShell

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

About the blog

Focusing on PeopleSoft, SQL, PowerShell, C# and anything else that tweaks my interest