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