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

Vista - how to get text string from description?

Reply
 
Old 05-28-2008   #1 (permalink)
DarrickIvy


 
 

how to get text string from description?

Just started to play with powershll and is using CTP2.

$a = get-help write-error
$b = $a.description

If I typed $b, I get
Quote:

> The Write-Error cmdlet writes an object to the error pipeline. You can use this cmdlet to generate error messages along with othe
r information such as an id, object data, and suggested actions.

Cool. I then try $a.name + $b, I get
Quote:

> Write-Error
Why? I am expecting this:
Quote:

> Write-Error The Write-Error cmdlet writes an object to the error pipeline. You can use this cmdlet to generate error messages along with othe
r information such as an id, object data, and suggested actions.

What am I missing. Thank you.

My System SpecsSystem Spec
Old 05-28-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: how to get text string from description?

Quote:

> Why? I am expecting this:
>
Quote:

>> Write-Error The Write-Error cmdlet writes an object to the error pipeline. You can use this cmdlet to generate error messages along with othe
> r information such as an id, object data, and suggested actions.
>
> What am I missing. Thank you.
You absolutely need that exact syntax?

How about:
$a|foreach-object{$_.name,$_.description}

The problem is that the addition operator can't be used for the
particular objects you're dealing with.

$a.gettype() doesn't return a string object.

I've not played with it enough, but there's a method to get a string out
of this, but even it doesn't work properly at first glance:

$a.name.tostring() + $b.tostring()

Maybe casting $a and $b to a string when creating them will help:

[string]$a=get-help write-error
etc.

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 05-28-2008   #3 (permalink)
DarrickIvy


 
 

Re: how to get text string from description?

Thanks for your reply Marco. You do give me more ideas to try out. I found
out that $a.description is of type PSObject[], not string. So, if I use
"$a.name + $a.description[0].text", it gives me my expected result.
Quote:

> You absolutely need that exact syntax?
No. I am simply playing around to get used to powershell. This is what I
don't understand. If I say "$a | get-member -name description", I get

description NoteProperty System.Management.Automation.PSObject[]
description=System.Management.Automation.PSObject[]

in one of the output lines. It says it is type PSObject[] (powershell
object array?).

Ok. Then I try "$a.description.GetType()", I get

True True PSObject[] System.Array

which seems to comfirm that the "description" property is PSObject[] type.

Then I say "$b = $a.description", then "$b.GetType()", it says it is String
type? Why? And at this point, $b is a empty string. Why? Why do I have to
say [PSObject[]]$b = $a.description to make $b of type PSObject[]?

Thank you.

My System SpecsSystem Spec
Old 05-28-2008   #4 (permalink)
Keith Hill [MVP]


 
 

Re: how to get text string from description?

"DarrickIvy" <DarrickIvy@xxxxxx> wrote in message
news:30B495F4-64EF-429D-9859-D382CF410615@xxxxxx
Quote:

> Thanks for your reply Marco. You do give me more ideas to try out. I
> found
> out that $a.description is of type PSObject[], not string. So, if I use
> "$a.name + $a.description[0].text", it gives me my expected result.
>
Quote:

>> You absolutely need that exact syntax?
>
> No. I am simply playing around to get used to powershell. This is what I
> don't understand. If I say "$a | get-member -name description", I get
>
> description NoteProperty System.Management.Automation.PSObject[]
> description=System.Management.Automation.PSObject[]
>
> in one of the output lines. It says it is type PSObject[] (powershell
> object array?).
>
> Ok. Then I try "$a.description.GetType()", I get
>
> True True PSObject[] System.Array
>
> which seems to comfirm that the "description" property is PSObject[] type.
>
> Then I say "$b = $a.description", then "$b.GetType()", it says it is
> String
> type? Why?
I don't see that on my system:

38> $b.GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True PSObject[] System.Array

Is it possible that you have turned $b into a variable of type string? For
instance, if you execute a statement like this:

[string]$b = ...

Then $b will only contain a string and PowerShell will try real hard to
convert everything to a string to fit into that variable. Try you
experiment with a different/new variable and see if you get the same
results:

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Concatenate text string and text in variable with no space between PowerShell
Concatenate text string and text in variable with no space between VB Script
Creating a Text string PowerShell
Re: Err.Description does not contain the full text of the error me VB Script
How to search and replace text in a string PowerShell


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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46