-
An if statement is used to test a condition and if is true do one thing and do another if it is false. It can be written generically as if (<condition>){ do stuff} else {do other stuff} Sometimes we need to test numerous alternatives. We could use...
-
I often saw scripts that did something like this function test1 { param ( [string] $computername ) if ( ! $computername ) { $computername = $env:COMPUTERNAME } Get-WmiObject -Class Win32_OperatingSystem -ComputerName $computername } A function, or script...
-
I have mentioned computer names a few times. One oddity is accessing the local machine. There are three options dot . localhost $env:COMPUTERNAME There are a number of occasions when . and localhost fail. For example PS> Get-EventLog -List -ComputerName...
-
Some of the events involved creating a CSV file. While it wasn’t explicitly asked that you opened the file many entrants chose to add that code to their scripts. There were a number of options presented – most involving opening Excel and importing the...
-
There are a few comments to make about using the pipeline but one of the obvious issues I saw from the games was this type of approach $p = Get-Process $p | where {$_.Name -like "powershell*"} The only time this is valid is if...
-
There are a number of ways to pass the names of the local machine into a script or function: use the actual name use the IP address (if the processing in the script can work with IP addresses) use 127.0.0.1 – the loop back address (if the processing...
-
In one of the events you had to find the default printer. This can be done using WMI. The full list of printers can seen using: Get-WmiObject -Class Win32_Printer If you want to examine the printer objecy – to determine what information is available...
-
A few times in the games you were asked for date time based information. Now WMI has a lot of classes that return WMI information. It is in an awkward format though PS> Get-WmiObject -Class Win32_OperatingSystem | fl *time* CurrentTimeZone : 60 LastBootUpTime ...
-
The games are over for another year. The number of entries was huge – 150% increase over last year. Congratulations to the winners and to everyone who took part. One thing I noticed was the number of scripts that made testing booleans harder than it needed...
-
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/06/2012-scripting-games-advanced-event-5-list-errors.aspx This is the one I was asked to supply a commentary for http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/20/expert-commentary...
-
Beginners event 5 http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/06/2012-scripting-games-beginner-event-5-provide-a-source-and-errors.aspx Looking for problem applications. Need report from each server form application log listing source and...
-
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/05/2012-scripting-games-beginner-event-4-compare-two-folders.aspx Makes sure you run the script provided with the event to get the right folders. The script randomly removes 1 file from each folder...
-
It was bugging me that I couldn’t get try-catch work on the folder where I didn’t have permissions. Finally thought of the reason – set the ErrorAction to Ignore. Heres the revised script [ CmdletBinding ( ) ] param ( [string] $path = "C:\2012sg...
-
Onto event 3 http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/04/2012-scripting-games-beginner-event-3-create-a-file-in-a-folder.aspx where we have to create a file in a folder. Simple – well yes but not with the twists the Scripting Guy throws...
-
I have seen a few interesting variations on using where-object during the games. This is normally aliased to where. You can use ? if you are fanatical about aliases but to me your PowerShell starts to become unreadable if its heavily aliased. Consider...