Category Archives: Powershell Basics

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: #9 Beginners event 5

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 number of errors. Requirements: should be capable of running against remote machine doesn’t need to run remotely for this scenario don’t … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #8 Beginners Event 4

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 – sneaky. After last nights head scratching this one is much simpler. It’s solvable with … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting Games 2012 comments: #7 update on beginners event 3

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\event3″, [string]$file … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment

Scripting games 2012 comments: #6 Beginners event 3

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 at you. The requirements: save file in c:\2012sg\event3 file contains process name and … Continue reading

Posted in Powershell Basics, Scripting | Leave a comment