Category Archives: Scripting

Scripting Games 2012 comments: #20 multiple ifs

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 … Continue reading

Posted in Powershell, Scripting | Leave a comment

Scripting Games 2012 comments: #19 default parameters

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, defines a parameter. The first thing the function … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #18 computer names

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 … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #17 opening a csv file

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 … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #16 reading environmental variables

Windows maintains a set of environmental variables. Some, but not all, can be seen via the env: PowerShell drive Get-ChildItem -Path env: You can also use WMI to see some of the variables Get-WmiObject -Class Win32_Environment | ft Name, VariableValue … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #15 pipelines

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 … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #14 local computer name

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 … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #13 Default Printer

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 … Continue reading

Posted in PowerShell and WMI, Scripting | Leave a comment

Scripting Games 2012 comments: #12 date conversions

  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* … Continue reading

Posted in PowerShell and WMI, Scripting | Leave a comment

Scripting Games 2012 comments: #11–its true

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 … Continue reading

Posted in Powershell, Scripting | Leave a comment