![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |