Wednesday, 18 July 2012

LttP: Powershell Games 2012 - Beginner Event 5

Beginner Event 5 was a nice challenge as I've not yet had the need/desire to use Powershell to query the Event logs.

Anyway, my "before I read what the expert says" entry is as follows:

Get-EventLog "Application" -ComputerName localhost -EntryType error | Group-Object -Property "Source" | Sort-Object -Property "Count" -Descending | Format-Table -Property "Count","Name" -AutoSize

Then I could quite simply just add the following to the end to write the output to a file:
 | Out-File  

LttP: Powershell Games 2012 - Begineer Event 4

This one was quite interesting, and after reading the Scripting Wife's comments I was determined that I too would  create a one-liner.

Beginner Event 4:

 ForEach($item in (Compare-Object (Get-Childitem "C:\1") (Get-Childitem "C:\2"))){$item.InputObject}

Although normally I would prefer to break it out a bit more to something like this (although it does exactly the same thing):

$Differences=(Compare-Object (Get-Childitem "C:\1") (Get-Childitem "C:\2"))

ForEach($item in $Differences)

{

 $item.InputObject

}

UPDATE:

I probably would've lost a point or two, as there is an even easier way to do that using the "-PassThru" option...

Compare-Object (Get-Childitem "C:\1") (Get-Childitem "C:\2") -Property Name -PassThru

LttP: Powershell Games 2012 - Beginner Event 3

Again, here's my entry for the Beginner Event 3 before I read the expert commentary.

<#
 .Synopsis
 2012SG_BE3 is the entry for the 2012 Powershell Scripting Games Beginner Event 3
 
 When run it will create a text file listing the running Process Name and ID.
 
 .Description
 2012SG_BE3 will create a text file listing the name and ID of all the running Process on the specified computer.
 
 If run multiple times results will be appended to the end of the file.
 
 .Example 2012SG_BE3.ps1 -ResultsRoot "D:\ScriptingGames" -ComputerName "HomeLaptop"
 
 Will store the name and ID's of the running processes on the computer named HomeLaptop in "D:\ScriptingGames\2012SG\event3\process3.txt"
 
 .Example 2012SG_BE3.ps1
 
 Will store the name and ID's of the running processes on the localhost in "C:\2012SG\event3\process3.txt"
 
 .Parameter ResultsRoot
 The -ResultsRoot parameter is used to change the Root folder of the Results.  
 If left blank the System Drive (as defined in the Environment Variables) will be used.
 
 If the trailing "\" is missing it will be added by default.
 
 .Parameter ComputerName
 The -ComputerName parameter is used to define the computer to collect the running Processes from.  
 If left blank "localhost" will be used.
    
 .Notes
    NAME: 2012SG_BE3
 AUTHOR: Owen Ballentine
 LASTEDIT: 04/27/2011 12:11:58
 
 .Link 
 http://sid351.blogspot.co.uk/
#>
Param([string]$ResultsRoot,[string]$ComputerName)
$FileLocation=""
If($ResultsRoot)
{
 $FileLocation=$ResultsRoot
 If($ResultsRoot.Substring($ResultsRoot.Length-1) -ne "\")
 {
  $FileLocation+="\"
 }
}
else
{
 $FileLocation=$env:SystemDrive
 $FileLocation+="\"
}
If(!$ComputerName)
{
 $ComputerName="localhost"
}
$FileLocation+="\2012SG\event3\"
Function Check-Path
{
param($Location, $TypeOfPath)
 If(!(Test-Path $Location))
 {
  New-Item -Path $Location -ItemType $TypeOfPath
 }
}
Check-Path $FileLocation "directory"
$FileLocation += "process3.txt"
Add-Content $FileLocation (Get-Process -ComputerName $ComputerName | Format-Table -Property "ProcessName","ID" -AutoSize | Out-String) 
I've made a mental note to look for a way to make PS scripts look nicer in the blog. Done...a bit. :)

UPDATE:

Looking at the expert commentary, I would've lost a point or two for not putting any error handling in the Check-Path function.  The expert setup error handling and then went one step further to default to the current users "My Documents".  Also, I needlessly added the "ComputerName" element.

At least I didn't fall for the hardcoded "C:\" trap that was set :)

LttP: Powershell Games 2012 - Beginner Event 2

Before I go reading the expert comentary I'll post my "entry"; then update if I'm wrong.

Beginner Event 2:

Get-Service -ComputerName localhost | Where-Object {$_.Status -eq "Running"}
UPDATE:

It looks like I fell into a well placed trap, that I'm sure others did during the games as well.

After reading the expert commentary I've learned my lesson and will be using Get-Member as well as Get-Help in future!

My reviewed entry would be:
Get-Service -ComputerName localhost | Where-Object {$_.CanStop}
The output looks similar, but there is a big difference between something that is Running, and something that "can stop" as the requirements state.

Oh well, I got this one wrong but at least I learned somethings!

Late to the Party - Powershell Games 2012 - Beginner Event 1

Well,  I had some downtime at work today which led me to spend some time brushing up on my scripting skills.

On my adventures across the web trying to find some "Powershell Challenges" I found the "Powershell Games 2012".  Like the title says, I'm late to this party; they kicked off and finshed back in April. 

Still, late is better than never, so I'm going to tackle the challenges and document my progress here :)

Beginner Event 1:

Here's my "entry":

Get-Process | Sort-Object -Property "WS" -Descending | select -First 10
The output looks like this:

Monday, 28 February 2011

Beeping VM's!

This is just a simple post so I remember how to stop VM's from beeping all the time!

Windows guest machines (the beep will comeback on a reboot):
net stop beep

Add the following to disable the beep service entirely:
sc config beep start= disabled


VMWare Player has a setting to quash all beeps from it's VM's:
Add mks.noBeep = "TRUE" to
%SystemDrive%\Users\%username%\AppData\Roaming\VMware\preferences.ini

Friday, 25 February 2011

My (early) experiences with SEO

I've been doing a lot of SEO work over the past week or two.

My "tl;dr point" will be this: SEO needs to be baked into the website as early as possible, preferably at the design phase.

Now for the "tl" part :)

The two key things, that I've found, with SEO are:

Organisation
Having all images under one folder, and all css files under another folder definitely helps when it comes to tracking down problems and setting up robots.txt files.

Standards
Both traditional web standards, such as using H1's and making sure titles, descriptions and alt tags are set, and "personal" standards, such as only using lowercase links.

When it comes to alt tags there is some arguments about the impact it has on SEO, but I think it's a small thing to get into the habit of doing, and if it helps, great. Anyway, the real reason alt tags are around is to help people who can't see all that well, so I think even for that reason it's a good idea (for non-layout related images.)

The self-discipline with naming formats will be of a massive help to prevent you from creating duplicate (or more) points of access to content. In a crawlers eyes "example.com/Page.aspx" is different to "example.com/page.aspx", and as such can lead to problems with sites getting multiple entries in indexes, where they end up competing with them self.

Also a tip, at the moment (Feb 2011) hyphens are the symbol to use to replace spaces in page names (i.e. www.example.com/this-is-a-page.aspx, instead of www.example.com/thisisapage.aspx).

If you're "attacking" an old site that needs bending with the SEO bar, then the best thing to do is setup 301 redirects from the old links to the new ones. Now this doesn't mean that every example HAS to be taken care of; only the old links that are being replaced should need 301'ing. Afterall, people normally copy and paste links, so if you've not been using "ThIs-Is-A-pAgE.aspx" then there shouldn't be a reason to 301 it. The exception to that is when you KNOW someone has a link to one of your pages and it's outside your self-disciplined regime. You know this because you've setup Google's wonderful webmaster tools, of course!

The other angle of attack on SEO is probably more a marketing / sales one, and that's around getting a community of cross-linkers, and generating fresh, important, content on a regular basis. From a technical point of view, there really is no reason why a website shouldn't be optimised for the search crawlers.

The only pointer I can give on that last bit is to become an "authority" on something that you know a lot about, and are willing to publicly share. Such as providing a pretty web app for people to use to generate international dialing phone numbers (for calling or texting), or a big bunch of recipes that you swear by. (Obviously it helps if this is somewhat related to what you're trying to sell!)

Once you've got that covered you can do the networking and try to get people to cross-link to you as a reference. That should increase your Page Rank, which will help your SEO efforts.