May 30, 2009

Powershell Scheduled Task or Windows Service

I have many powershell scripts that do reporting, alerting, or even gathering of statistical data. I needed a way to have those scripts running either all the time or at intervals. Here is a simple way to do this in powershell:

Scheduled Task:
  • Create the Task and the Time
  • Run as Administrator
  • powershell.exe -nologo -command "&{c:\MyTask.ps1}"
One note is that powershell.exe must be in your environment variables. If not, then just put the full path in to powershell.exe (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)

Run as a Windows Service
  • Download RunAsService
  • Run through the basic installation and set up the user to run the service (or local if that is all that is needed)
  • In the .config used by RunAsService
<name>POSh Service</name>
<parameters>-file "C:\MyPOShServices\runmon.ps1"</parameters>
The only way I know how to do this currently is with some open source. Please let me know if there is another way.

Restart/Shutdown using powershell

Actually, this is very simple to do in powershell with some built in cmdlets (note this is v2 CTP3 only):
Restart-Computer -Computername (Get-Content c:\list.txt) -throttlelimit 5

Shutdown-Computer –Computername (Get-Content c:\list.txt) –force –throttlelimit 5
Just be careful that you don't take your network down.

May 28, 2009

Visual Studio 2010 and .NET 4.0

Well, the beta is here for us to download. I guess it is about that time to upgrade and start to look at this. F# seems to be the new language that I saw in Microsoft research last year and is now going to be part of the 2010 distro.

Here is the link for the download: .NET 4.0

Can't execute powershell scripts

So you downloaded powershell and you also downloaded a script from the internet (maybe even from my blog). You try to run it, but you get some security error. By default POSh 'disables' scripts not created locally. You can go to the powershell prompt and see what your policy is by running:
You can set it to a level that works for you (see help). I set mine to Unrestricted, but be careful with that setting. You may want to it set it to RemoteSigned instead.
Set-ExecutionPolicy Unrestricted
On a side note, you can also change this manually in the registry.

Replace macro fields with powershell

Have you ever needed to update config files when moving from dev to qa and then finally to prod? One simple way of doing this is with powershell and this can than be called out from a nant script if you like. You will need to template your config files (or .xml files, or any file with any extension) and then have a corresponding value to update it with.

Here is a little snippet, update to suit your needs:


<add key="MyKey" value="$foo" />

$foo = 'MyValue'

$currentDir = (Get-Location -PSProvider FileSystem).ProviderPath

# Load the variables and values to our current scope
. ($currentDir + "/ConfigValues.ps1")

# Process all files in templates folder
foreach ($file in (dir "templates/*.*"))
$text = [IO.File]::ReadAllText($file)
$ExecutionContext.InvokeCommand.ExpandString($text) | out-file (Join-Path $pwd $
Write-Host "Done."

Things in powershell can be written very quickly and this is a great example.


One of my favorite should take a look!


May 27, 2009

Powershell Merge/Compare

How cool is powershell? Well, I needed to do a compare for some .config files in an automated process. I could use C#, but why not look towards a few lines and see what powershell can do. Sure enough, there is Compare-Object. This did not work as I expected and I instead did a quick google search and saw someone else who did this same thing: Powershell Merge

All I had to do was:
$a = Get-Content cachingDev.config
$b = Get-Content cachingProd.config
Compare-Object $a $b -SyncWindow 50
Yup, powershell is really that awesome.

AzMan Tutorial Posts

AzMan has been part of my toolbelt for close to five years now. It all started back when I read a small article about it and saw it in an early version of some open-source code from Microsoft (pre EntLib days). I knew that this was a simple RBAC idea that would go far in the world of .NET. I jumped on it and have rolled it into many applications. I'll post some tutorials over the next few days on how to set it up and some code samples on how to access it as well.

One of the things that really bothered me about AzMan was the store location (lack of sql specifically). My conversation with Dave at Microsoft (he was the product manager back then) concluded with "...wait till Longhorn". Turns out, he was right as Windows 7 has the SQL approach. I'll update some code to show how to access it via SQL and update my original import/export script (which currently can be found here: AzMan Bulk Import/Export) to include it as well.

Where is my Quick Launch bar for windows 7!?

Add Quick Launch To Windows 7

Testing Windows 7 Beta

So I wanted to test Windows 7 after hearing some really great things about it. My problem is that I can't just walk away from Windows XP. Alas, we all eventually have to is the way you can do it easily and push it out on a VM:
  1. Download windows 7: Windows 7 Beta
  2. Download VMWare Free player: VMWare Player
  3. Use EasyVMX to create your files: EasyVMX
  4. Open up the vmdk with your free player
  5. Run through the install (don't do an upgrade)
  1. Super-Simple for a quick way...or choose another option if you want sme finer details
  2. Choose windows Vista as the operating system...64 or 32 bit depending on what your host OS is
  3. Set memory...more you can spare the better
  4. Tick the Live ISO CD checkbox at the bottom
  5. Create and download
One last note is that you can always increase memory or change options in the My_Virtual_Machine_.vmx (or your name) file. Just open it up in notepad and change things like numvcpus or memsize.

Enjoy Windows 7!

My first post

Well I finally decided to do it...create a blog. I guess my blog will be more like a twitter blog as I will post on this all day. Hey, I have been meaning to do this for years and I figure it is about time that I get in and start to do this. Look for some more posts later today!