Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

creating a PSScriptProperty

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 09-11-2006   #1 (permalink)
=?Utf-8?B?c2VjcmV0R2Vlaw==?=
Guest


 

creating a PSScriptProperty

Hello assorted powershell legends!

I've been tinkering with powershell on my own and never though to post here,
until Thomas Lee suggested it.
Current problem: i can't find any way to create a ScriptBlock object from a
C# app.
I can create a command that is a script... but can't get an actual
"ScriptBlock" object.

code example below:

System.Management.Automation.ScriptBlock sc;

//help! do some magic here to instantiate my scriptblock...
//e.g.
//sc = new ScriptBlock(@"{""Hello""}"); //this doesn't work.
//perhaps i need to use:
//System.Management.Automation.CommandInvocationIntrinsics.NewScriptBlock
//but haven't worked out how that works yet...

PSScriptProperty psc = new PSScriptProperty("NewScriptBlock",sc);

//now i can add the script property to a PSObject's properties collection
//and invoke it...

ps1.Properties.Add(psc);

MessageBox.Show(ps1.Properties["NewScriptBlock"].Value);

....cheers
this is with RC1 by the way
lb

...:: secretGeek.net :: TimeSnapper.com ::..

My System SpecsSystem Spec
Old 09-12-2006   #2 (permalink)
=?Utf-8?B?ZHJlZXNjaGtpbmQ=?=
Guest


 

RE: creating a PSScriptProperty

I think there are better ways, but one method might be to use
invoke-expression.
It will return a scriptblock if you provide a string. Should be possible to
transform this code it into C#.

PS> $scriptblock = invoke-expression "{write-output 'hello, world!'}"
PS> $scriptblock | gm
TypeName: System.Management.Automation.ScriptBlock
PS> . $scriptblock
hello, world!

--
greetings
dreeschkind

"secretGeek" wrote:

> Hello assorted powershell legends!
>
> I've been tinkering with powershell on my own and never though to post here,
> until Thomas Lee suggested it.
> Current problem: i can't find any way to create a ScriptBlock object from a
> C# app.
> I can create a command that is a script... but can't get an actual
> "ScriptBlock" object.
>
> code example below:
>
> System.Management.Automation.ScriptBlock sc;
>
> //help! do some magic here to instantiate my scriptblock...
> //e.g.
> //sc = new ScriptBlock(@"{""Hello""}"); //this doesn't work.
> //perhaps i need to use:
> //System.Management.Automation.CommandInvocationIntrinsics.NewScriptBlock
> //but haven't worked out how that works yet...
>
> PSScriptProperty psc = new PSScriptProperty("NewScriptBlock",sc);
>
> //now i can add the script property to a PSObject's properties collection
> //and invoke it...
>
> ps1.Properties.Add(psc);
>
> MessageBox.Show(ps1.Properties["NewScriptBlock"].Value);
>
> ...cheers
> this is with RC1 by the way
> lb
>
> ..:: secretGeek.net :: TimeSnapper.com ::..

My System SpecsSystem Spec
Old 09-12-2006   #3 (permalink)
Narayanan Lakshmanan [MSFT]
Guest


 

Re: creating a PSScriptProperty

ScriptBlock does not have a public constructor available. If you are
developing a cmdlet then you can accept this as a parameter.

--
Narayanan Lakshmanan [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
news:F594C86B-0AB4-4287-AB8E-A0F61EF6B4A8@microsoft.com...
>I think there are better ways, but one method might be to use
> invoke-expression.
> It will return a scriptblock if you provide a string. Should be possible
> to
> transform this code it into C#.
>
> PS> $scriptblock = invoke-expression "{write-output 'hello, world!'}"
> PS> $scriptblock | gm
> TypeName: System.Management.Automation.ScriptBlock
> PS> . $scriptblock
> hello, world!
>
> --
> greetings
> dreeschkind
>
> "secretGeek" wrote:
>
>> Hello assorted powershell legends!
>>
>> I've been tinkering with powershell on my own and never though to post
>> here,
>> until Thomas Lee suggested it.
>> Current problem: i can't find any way to create a ScriptBlock object from
>> a
>> C# app.
>> I can create a command that is a script... but can't get an actual
>> "ScriptBlock" object.
>>
>> code example below:
>>
>> System.Management.Automation.ScriptBlock sc;
>>
>> //help! do some magic here to instantiate my scriptblock...
>> //e.g.
>> //sc = new ScriptBlock(@"{""Hello""}"); //this doesn't work.
>> //perhaps i need to use:
>> //System.Management.Automation.CommandInvocationIntrinsics.NewScriptBlock
>> //but haven't worked out how that works yet...
>>
>> PSScriptProperty psc = new PSScriptProperty("NewScriptBlock",sc);
>>
>> //now i can add the script property to a PSObject's properties collection
>> //and invoke it...
>>
>> ps1.Properties.Add(psc);
>>
>> MessageBox.Show(ps1.Properties["NewScriptBlock"].Value);
>>
>> ...cheers
>> this is with RC1 by the way
>> lb
>>
>> ..:: secretGeek.net :: TimeSnapper.com ::..


My System SpecsSystem Spec
Old 09-13-2006   #4 (permalink)
=?Utf-8?B?c2VjcmV0R2Vlaw==?=
Guest


 

Re: creating a PSScriptProperty

Thanks Narayanan.
Yes, I see that ScriptBlock doesn't have a public constructor.
I'm not developing a cmdLet in this case -- i'm trying to write C# code that
will add a Property to a PSObject, and the type of Property I want to add is
a ScriptProperty.
ScriptProperties look very cool, as they giv you a real flexibility there.
So i just want to add a simple script property.
If I grab a ScriptBlock (object) from an existing ScriptProperty, then I can
use it for my new ScriptProperty (with whatever name I want) but I haven';t
worked out how to set my own script inside the script block.
I figure there must be some object that "emits" ScriptBlocks -- ie, acts as
a ScriptBlock factory -- and somewhere I'd have a chance to specify the body
of the script itself... but I can't find any such class, yet.


"Narayanan Lakshmanan [MSFT]" wrote:

> ScriptBlock does not have a public constructor available. If you are
> developing a cmdlet then you can accept this as a parameter.
>
> --
> Narayanan Lakshmanan [MSFT]
> Windows PowerShell Development
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
> news:F594C86B-0AB4-4287-AB8E-A0F61EF6B4A8@microsoft.com...
> >I think there are better ways, but one method might be to use
> > invoke-expression.
> > It will return a scriptblock if you provide a string. Should be possible
> > to
> > transform this code it into C#.
> >
> > PS> $scriptblock = invoke-expression "{write-output 'hello, world!'}"
> > PS> $scriptblock | gm
> > TypeName: System.Management.Automation.ScriptBlock
> > PS> . $scriptblock
> > hello, world!
> >
> > --
> > greetings
> > dreeschkind
> >
> > "secretGeek" wrote:
> >
> >> Hello assorted powershell legends!
> >>
> >> I've been tinkering with powershell on my own and never though to post
> >> here,
> >> until Thomas Lee suggested it.
> >> Current problem: i can't find any way to create a ScriptBlock object from
> >> a
> >> C# app.
> >> I can create a command that is a script... but can't get an actual
> >> "ScriptBlock" object.
> >>
> >> code example below:
> >>
> >> System.Management.Automation.ScriptBlock sc;
> >>
> >> //help! do some magic here to instantiate my scriptblock...
> >> //e.g.
> >> //sc = new ScriptBlock(@"{""Hello""}"); //this doesn't work.
> >> //perhaps i need to use:
> >> //System.Management.Automation.CommandInvocationIntrinsics.NewScriptBlock
> >> //but haven't worked out how that works yet...
> >>
> >> PSScriptProperty psc = new PSScriptProperty("NewScriptBlock",sc);
> >>
> >> //now i can add the script property to a PSObject's properties collection
> >> //and invoke it...
> >>
> >> ps1.Properties.Add(psc);
> >>
> >> MessageBox.Show(ps1.Properties["NewScriptBlock"].Value);
> >>
> >> ...cheers
> >> this is with RC1 by the way
> >> lb
> >>
> >> ..:: secretGeek.net :: TimeSnapper.com ::..

>
>

My System SpecsSystem Spec
Old 09-14-2006   #5 (permalink)
Narayanan Lakshmanan [MSFT]
Guest


 

Re: creating a PSScriptProperty

Well the factory you expect is also not available. PSCmdlet has a member
"InvokeCommand" which returns an object of type CommandInvocationIntrinsics.
This has a method NewScriptBlock which compiles a string into a ScriptBlock
object. But neither PSCmdlet or CommandInvocationIntrinsics have any public
constructor available and NewScriptBlock isn't a static function either. But
in case you want to achieve the same from a cmdlet, then its doable.

--
Narayanan Lakshmanan [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"secretGeek" <secretGeek@discussions.microsoft.com> wrote in message
news:E07DF5A5-0127-4EE1-B6DA-F427CBDCC576@microsoft.com...
> Thanks Narayanan.
> Yes, I see that ScriptBlock doesn't have a public constructor.
> I'm not developing a cmdLet in this case -- i'm trying to write C# code
> that
> will add a Property to a PSObject, and the type of Property I want to add
> is
> a ScriptProperty.
> ScriptProperties look very cool, as they giv you a real flexibility there.
> So i just want to add a simple script property.
> If I grab a ScriptBlock (object) from an existing ScriptProperty, then I
> can
> use it for my new ScriptProperty (with whatever name I want) but I
> haven';t
> worked out how to set my own script inside the script block.
> I figure there must be some object that "emits" ScriptBlocks -- ie, acts
> as
> a ScriptBlock factory -- and somewhere I'd have a chance to specify the
> body
> of the script itself... but I can't find any such class, yet.
>
>
> "Narayanan Lakshmanan [MSFT]" wrote:
>
>> ScriptBlock does not have a public constructor available. If you are
>> developing a cmdlet then you can accept this as a parameter.
>>
>> --
>> Narayanan Lakshmanan [MSFT]
>> Windows PowerShell Development
>> Microsoft Corporation
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>> "dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
>> news:F594C86B-0AB4-4287-AB8E-A0F61EF6B4A8@microsoft.com...
>> >I think there are better ways, but one method might be to use
>> > invoke-expression.
>> > It will return a scriptblock if you provide a string. Should be
>> > possible
>> > to
>> > transform this code it into C#.
>> >
>> > PS> $scriptblock = invoke-expression "{write-output 'hello, world!'}"
>> > PS> $scriptblock | gm
>> > TypeName: System.Management.Automation.ScriptBlock
>> > PS> . $scriptblock
>> > hello, world!
>> >
>> > --
>> > greetings
>> > dreeschkind
>> >
>> > "secretGeek" wrote:
>> >
>> >> Hello assorted powershell legends!
>> >>
>> >> I've been tinkering with powershell on my own and never though to post
>> >> here,
>> >> until Thomas Lee suggested it.
>> >> Current problem: i can't find any way to create a ScriptBlock object
>> >> from
>> >> a
>> >> C# app.
>> >> I can create a command that is a script... but can't get an actual
>> >> "ScriptBlock" object.
>> >>
>> >> code example below:
>> >>
>> >> System.Management.Automation.ScriptBlock sc;
>> >>
>> >> //help! do some magic here to instantiate my scriptblock...
>> >> //e.g.
>> >> //sc = new ScriptBlock(@"{""Hello""}"); //this doesn't work.
>> >> //perhaps i need to use:
>> >> //System.Management.Automation.CommandInvocationIntrinsics.NewScriptBlock
>> >> //but haven't worked out how that works yet...
>> >>
>> >> PSScriptProperty psc = new PSScriptProperty("NewScriptBlock",sc);
>> >>
>> >> //now i can add the script property to a PSObject's properties
>> >> collection
>> >> //and invoke it...
>> >>
>> >> ps1.Properties.Add(psc);
>> >>
>> >> MessageBox.Show(ps1.Properties["NewScriptBlock"].Value);
>> >>
>> >> ...cheers
>> >> this is with RC1 by the way
>> >> lb
>> >>
>> >> ..:: secretGeek.net :: TimeSnapper.com ::..

>>
>>


My System SpecsSystem Spec
Old 09-14-2006   #6 (permalink)
klumsy@xtra.co.nz
Guest


 

Re: creating a PSScriptProperty

whats your context?
though a factory might not be avalible, there is nothing stopping you
in C# from creating your own pipeline and running a string of
powershell text that does the same thing. I do that all the time for
various things in powershell analyzer.

Karl
for everything i often have to make a choice.. can i, or should i try
to do this with the native powershell dotnet objects, or should i just
pass in a string to a pipeline object and get powershell to do it for
me. Some times its a hard call to make.

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Accounts Benny Vista account administration 0 11-29-2007 12:49 PM
Creating a New Identity Shrishail Chougala Vista mail 2 09-20-2007 01:12 AM
creating new mail carol Vista mail 6 08-19-2007 01:24 AM
Creating a menu Marco Shaw PowerShell 2 12-18-2006 01:02 PM
Creating DVD =?Utf-8?B?QW5kcmVhcw==?= Vista installation & setup 1 09-12-2006 03:44 AM


Vistax64.com 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 2005-2008

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 47 48 49 50 51