Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.


Go Back   Vista Forums > Misc Newsgroups > PowerShell

RB

Vista - How do you use .net enumeration values in powershell?

Reply
 
09-20-2007   #1 (permalink)
Bob Landau


 

How do you use .net enumeration values in powershell?

I am trying to read the file attributes given a file or folder. What I
expected to be simple is unfortunately not giving me the results I expected.


## This returns the type FileAttributes
(Get-ChildItem | % {$_.get_attributes()})[0].gettype()


## This does not produce the results that I get by executing cmd /c dir /aa;
nothing is printed on the screen
Get-ChildItem | % {if ($_.get_attributes() -band
[System.IO.FileAttributes].Archive) {echo 'yes'} }


## This results in a nul expression
$a = [System.IO.FileAttributes].Archive

$a -eq $null

So my problem appears to be because I don't understand how to access .NET
enumeration types. I would appreciate someone explaining to me how I can do
this.

thx
bob

My System SpecsSystem Spec
09-20-2007   #2 (permalink)
Jeff


 

Re: How do you use .net enumeration values in powershell?

On Sep 20, 9:06 pm, Bob Landau <BobLan...@xxxxxx>
wrote:
Quote:

> I am trying to read the file attributes given a file or folder. What I
> expected to be simple is unfortunately not giving me the results I expected.
>
> ## This returns the type FileAttributes
> (Get-ChildItem | % {$_.get_attributes()})[0].gettype()
>
> ## This does not produce the results that I get by executing cmd /c dir /aa;
> nothing is printed on the screen
> Get-ChildItem | % {if ($_.get_attributes() -band
> [System.IO.FileAttributes].Archive) {echo 'yes'} }
>
> ## This results in a nul expression
> $a = [System.IO.FileAttributes].Archive
>
> $a -eq $null
>
> So my problem appears to be because I don't understand how to access .NET
> enumeration types. I would appreciate someone explaining to me how I can do
> this.
>
> thx
> bob
Try this:

$a = [System.IO.FileAttributes]::Archive
or
$a = [System.IO.FileAttributes] "Archive"

Good luck.

Jeff

My System SpecsSystem Spec
09-20-2007   #3 (permalink)
Bob Landau


 

Re: How do you use .net enumeration values in powershell?

Bit by "static" again.

I was familar with static method/properties. In hindsight it seems obvious
that static should apply to enum types.

Thank you

bob


"Jeff" wrote:
Quote:

> On Sep 20, 9:06 pm, Bob Landau <BobLan...@xxxxxx>
> wrote:
Quote:

> > I am trying to read the file attributes given a file or folder. What I
> > expected to be simple is unfortunately not giving me the results I expected.
> >
> > ## This returns the type FileAttributes
> > (Get-ChildItem | % {$_.get_attributes()})[0].gettype()
> >
> > ## This does not produce the results that I get by executing cmd /c dir /aa;
> > nothing is printed on the screen
> > Get-ChildItem | % {if ($_.get_attributes() -band
> > [System.IO.FileAttributes].Archive) {echo 'yes'} }
> >
> > ## This results in a nul expression
> > $a = [System.IO.FileAttributes].Archive
> >
> > $a -eq $null
> >
> > So my problem appears to be because I don't understand how to access .NET
> > enumeration types. I would appreciate someone explaining to me how I can do
> > this.
> >
> > thx
> > bob
>
> Try this:
>
> $a = [System.IO.FileAttributes]::Archive
> or
> $a = [System.IO.FileAttributes] "Archive"
>
> Good luck.
>
> Jeff
>
>
My System SpecsSystem Spec
09-20-2007   #4 (permalink)
Shay Levi


 

Re: How do you use .net enumeration values in powershell?

BTW, Installing MoW's PowerTab can save you a lot of "if and how", It did
for me

Shay
http://scriptolog.blogspot.com


Quote:

> Bit by "static" again.
>
> I was familar with static method/properties. In hindsight it seems
> obvious that static should apply to enum types.
>
> Thank you
>
> bob
>
> "Jeff" wrote:
>
Quote:

>> On Sep 20, 9:06 pm, Bob Landau <BobLan...@xxxxxx>
>> wrote:
>>
Quote:

>>> I am trying to read the file attributes given a file or folder. What
>>> I expected to be simple is unfortunately not giving me the results I
>>> expected.
>>>
>>> ## This returns the type FileAttributes
>>> (Get-ChildItem | % {$_.get_attributes()})[0].gettype()
>>> ## This does not produce the results that I get by executing cmd /c
>>> dir /aa;
>>> nothing is printed on the screen
>>> Get-ChildItem | % {if ($_.get_attributes() -band
>>> [System.IO.FileAttributes].Archive) {echo 'yes'} }
>>> ## This results in a nul expression
>>> $a = [System.IO.FileAttributes].Archive
>>> $a -eq $null
>>>
>>> So my problem appears to be because I don't understand how to access
>>> .NET enumeration types. I would appreciate someone explaining to me
>>> how I can do this.
>>>
>>> thx
>>> bob
>> Try this:
>>
>> $a = [System.IO.FileAttributes]::Archive
>> or
>> $a = [System.IO.FileAttributes] "Archive"
>> Good luck.
>>
>> Jeff
>>

My System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: How do you use .net enumeration values in powershell?
Thread Forum
Using Powershell to search the registry for all Keys, Values, and PowerShell
Read values returned from a powershell command in VB.Net PowerShell
mirroringrole enumeration - script PowerShell
Combining enumeration values PowerShell
Networok Enumeration Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd