/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" Working with Fixed Length delimited Text files in ... "," PowerShell and Active Directory Part 3 (UserProper... "," PowerShell and MOM 2005 "," PowerShell and Active Directory Part 2 "," PowerShel and Active Directory Part 1 "," TechEd RoundUp "," A big hurray for the Scripting Guy ! "," Teched "," PowerShell Tab Completion Part 4 "," The DFO Show - Introducing Windows PowerShell "," ")

Thursday, July 06, 2006

 


PowerShell Boolean FileMode



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.0encoding="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 :


Comments:
Anonymous Jeffrey Snover
PSMDTAG:TYPEEXTENSION: File on BOOL
PSMDTAG:CMDLET: Update-TypeData

Jeffrey Snover [MSFT]
Windows PowerShell Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
 
Post a Comment

Links to this post:

Create a Link



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?