February 17, 2015

Invalid Command.... Meh!!!

We have all been there, we have all made little typo’s – But something that narks this Grumpy Admin off is PowerShell is very proud of it’s syntax and makes a large noise when you make a mistake! 

get-itemss : The term ‘get-itemss’ is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ get-itemss
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-itemss:String) [], CommandN
otFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

As you can see there is a lot of text about 10 lines to say in the most basic way – ha ha you idiot you made a mistake – Command Not Found.

As we know Powershell works quite differently to other command line interfaces from Microsoft. And one thing that makes me grumpy is when you enter commands in the wrong window or the wrong prompt. As no doubt like the Grumpy Admin you have a million windows open!

You would think the contrast between black and blue would be enough for the grumpy admin to get his syntax right all the time. Not withstanding the odd typo or mistake of not using

Get-Help

Lets take one command that I use without thinking the LS command.

Works in Powershell! Great
Works in Linux – Great
Works in DOS- not so great

I do now these days try to always do everything at a PS prompt rather than the DOS prompt. However, there are times I forget, and when I make these sort of typing invalid commands and I make mistakes it make me grumpy!

 

Lucky for this grumpy admin, the wonderful people at Microsoft also have/had this issue, that is why they implemented ALIASES from the get go. A full list of aliases can be displayed using

Get-Alias

These aliases are great, they not only map useful common commands from other environments like Linux to the PowerShell equivalent, they also make scripts smaller with their useful shorthand.

Some people actually think LS is an command – where in fact is is an alias for the Get-ChildItem PowerShell CmdLet.

As we know, the grumpy admin is a lazy admin!!! so typing less is always good! So when your are busy scripting you use these aliases to make your script smaller! You won’t regret it!

If you really want to get into it and understand how the whole mapping commands works you use the

| Export-CSV <filename/path>

You will see how the command actually works.  If you do this and open up the generated CSV File, you can see more information about the scope and visibility of the commands, their display names and all that neat stuff. But in all honestly the Get-Alias should be all you need. If you really adventurous you can also use

New-Alias

As with most things in Microsoft there are other ways of doing things, again this makes me grumpy! You could also use the the NAL alias for the new-alias command or you could use :-

Set-Alias

To create your own aliases, to speed things up.

Grumpy Admin gives, Grumpy Admin takes away!

Remove-Item alias:<alias name here>

What would happen if you upset Grumpy Admin and he put some not very helpful commands in your profile.ps1? –

Remove-Item alias:ps
NAL PS LS

This would leave a few people scratching their heads, and wondering what they are doing wrong.  But then if you are using Powershell to manage processes and using the PS alias command, I would hope you know about aliases so that you know it a practical joke, but knowing some technical staff out there they would rebuild the whole dam server over something like this – That would make me very very grumpy!

So here is a small sample of the aliases I tend to use quite a bit and find useful!

% -> ForEach-Object
cls -> Clear-Host
cd -> Set-Location
copy -> Copy-Item
del -> Remove-Item
dir -> Get-ChildItem
ft -> Format-Table
ipmo -> Import-Module
ls -> Get-ChildItem
rm -> Remove-Item
rmdir -> Remove-Item
write -> Write-Output

Also the Grumpy Admin’s top top tip, is create and alias for Notepad.exe like this :-

Set-Alias Edit Notepad.exe

Hazzy