You may have seen this at the top of scripts or modules
#Requires –Version 3
This will stop the script running if the PowerShell version is 2.0 or below.
Other options are available
#Requires –PSSnapin can be used to force the loading of a snapin
#Requires –Modules can be used to force importing of modules
#Requires –Shellid controls which shells the script can run in. Default PowerShell installs have the ISE and console
#Requires –RunAsAdminstrator – script must be running in instance of PowerShell with elevated privileges
These provide a simple and easy way to control execution without lots of code. I particularly like the last one.
Some times you need to reboot a remote machine as part of your process. PowerShell provides the Restart-Computer cmdlet to perform that task:
Restart-Computer -ComputerName server03
If you want you process to pause until the reboot has finished then you add the –Wait parameter:
Restart-Computer -ComputerName server03
You final options when pausing are to wait until a specific service – PowerShell, WinRM or WMI is avialable:
Restart-Computer -ComputerName server03 -Wait -For PowerShell
Restart-Computer -ComputerName server03 -Wait -For WinRM
Restart-Computer -ComputerName server03 -Wait -For Wmi
These options should cover most situations as these services are among the last to become available when a machine starts.
My Learn AD management in a Month of Lunches has gone to the printers – expect it soon
Like all well behaved technology Desired State Configuration (DSC) requires access to WMI. Specifically it needs root/Microsoft/Windows/DesiredStateConfiguration
This is available on the Windows 2012 R2 DSC server you configure but isn’t available on older versions of Windows.
You must install Windows Management Framework 4.0 (PowerShell 4.0) on any machine that you are going to configure through DSC. Windows Server 2012 already has it installed but for Windows 2008 R2 & Windows 2012 you’ll need to download the appropriate version of WMF 4.0 and install it.
With the PowerShell 65 days away there is still plenty of time to get registered. This is the only event this year that will feature such a line up of PowerShell experts – including the PowerShell team themselves.
The attendees at last year’s Summit decided it was an excellent experience – this years will be just as good.
Registration details from http://powershell.org/wp/community-events/summit/
I’ve never really understood the logic behind the Wait-Job cmdlet. The idea of PowerShell Jobs is to kick of tasks that grind away in the background and you get the prompt back so you can keep on working. Wait-Job stops the prompt being returned until one or more jobs have finished.
It clicked this morning – I need to run some tasks in parallel and decided that a number of jobs was the best way (I looked at workflow but there are some complications such that I didn’t pursue that route). The only complication with using Jobs is that you don’t know when they finish (unless you check with Get-Job) and I needed a number of Jobs to finish before starting the last job in the series – this is a data dependency issue that I can’t avoid.
Wait-Job is the answer. Start my first set of jobs. Use:
Get-Jobs | Wait-Job
which means nothing else is done until all my jobs finish and then start the last job.
As I’m going to be running this through a scheduled task I don’t care about the prompt being frozen.
Nice to finally find a use for a cmdlet I’ve ignored since it appeared.
A free ebook on managing various aspects of your networking is now available from http://powershell.org/wp/2014/02/19/free-ebook-from-microsofts-scripting-guy-windows-powershell-networking-guide/
The book is written by Ed Wilson – The Scripting Guy – and is a must read for any PowerShell user.
No – we’re not announcing the start of the next games just yet.
There is however a chance for you to shape the next games – head over to http://powershell.org/wp/2014/02/17/what-should-the-scripting-games-look-like-next-time/ and tell us what you would like to see in the next games
The Winter 2014 Scripting Games are officially closed
And the winners declared.
A big thank you to all who took part as competitors, coaches or judges.
We will be back with a Summer games later in the year – stay tuned for further details
I had a comment left on one of my posts recently that mentioned a “Hello World” script. The concept of a Hello World program/script is that it is a simple introduction to using a new language that gives a known result.
For a brand new user to PowerShell is is how you create a Hello World script:
open PowerShell with elevated privileges (Run as Administrator)
type Set-ExecutionPolicy RemoteSigned –Force and press enter
This enables scripts to run.
Run these two lines by typing them individually and pressing enter
New-Item -Path c:\ -Name TestScripts -ItemType Directory cd C:\TestScripts
They create a test folder and move your location to that folder
“‘Hello World'” > script1.ps1
and press enter. This creates your script. The single quotes nested in the double quotes are so that the text in the script will be surrounded by quotes – its a string value.
Run the script by typing
and pressing enter. You will see the results:
The .\ is required as PowerShell won’t run commands in the current folder by default. You have to explicitly give the local path.
You’ve now written your first PowerShell script and discovered most of the “gotchas” that cause people problems when they are first learning PowerShell