This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
Inspired by this Nice piece of Code :
Classic WTF - What is Truth?I came up with this TypeExtension for PowerShell :
BoolFileMode.ps1xml
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>System.Boolean</Name>
<Members>
<ScriptMethod>
<Name>File</Name>
<Script>
if ($this -eq $false){"File Not found"} Else {$True}
</Script>
</ScriptMethod>
</Members>
</Type>
</Types>
You can load this with this command :
# Execute in same Directory as BoolFileMode.ps1xml
Update-TypeData BoolFileMode.ps1xml
So now you can do things like this :
PoSH>ls foo*
Directory: Microsoft.PowerShell.Core\FileSystem::E:\Documents and Settings\Mow
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 5/31/2006 8:36 PM foo
-a--- 7/6/2006 10:50 PM 24 foo.txt
# Normal Boolean
PoSH>$file = ([bool](gc foo.txt))
PoSH>$file
True
PoSH>$file = ([bool](gc foobar.txt))
Get-Content : Cannot find path 'E:\Documents and Settings\Mow\foobar.txt' because it does not exist.
At line:1 char:19
+ $file = ([bool](gc <<<< foobar.txt))
PoSH>$file
False
# Update the Bool Type to add a FileMode
PoSH>Update-TypeData G:\powershell\BoolFileMode.ps1xml
PoSH>$file = ([bool](gc foo.txt)).file()
PoSH>$file
True
PoSH>$file = ([bool](gc fooBar.txt)).file()
Get-Content : Cannot find path 'E:\Documents and Settings\Mow\fooBar.txt' because it does not exist.
At line:1 char:19
+ $file = ([bool](gc <<<< fooBar.txt)).file()
PoSH>$file
File Not found
very handy ;-)
# If a bool is a file its not found
PoSH>$true.file()
True
PoSH>$False.file()
File Not found
But on Second tought I did not add this to my profile, as I would Advice you to do (and in your next session this WTF ScriptMethod goes away ;-)
TypeExtensions are Powerfull, but use them with care !Greetings /\/\o\/\/
Tags : Monad msh PowerShell