header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
WPF Runspace Error
Last Post 31 Mar 2009 11:15 AM by Marco Shaw (MVP). 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
PowerShell JediUser is Offline
Basic Member
Basic Member
Posts:410
Avatar

--
29 Mar 2009 09:28 PM
    I was looking at a WPF/Powershell article MS Powershell blog WPF & PowerShell – Part 1 ( Hello World & Welcome to the Week of WPF )

    When ever I try the 3rd Example

    # Create a runspace to run Hello World


    $rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()


    $rs.ApartmentState, $rs.ThreadOptions = “STA”, “ReuseThread”


    $rs.Open()


    # Reference the WPF assemblies


    $psCmd = {Add-Type}.GetPowerShell()


    $psCmd.SetRunspace($rs)


    $psCmd.AddParameter("AssemblyName", "PresentationCore").Invoke()


    $psCmd.Command.Clear()


    $psCmd = $psCmd.AddCommand("Add-Type")


    $psCmd.AddParameter("AssemblyName", "PresentationFramework").Invoke()


    $psCmd.Command.Clear()


    $psCmd = $psCmd.AddCommand("Add-Type")


    $psCmd.AddParameter("AssemblyName", "WindowsBase").Invoke()


    $sb = $executionContext.InvokeCommand.NewScriptBlock(


    (Join-Path $pwd "HelloWorld.ps1")


    )


    $psCmd = $sb.GetPowerShell()


    $psCmd.SetRunspace($rs)


    $null = $psCmd.BeginInvoke()


    I get an error...


    Method invocation failed because [System.Management.Automation.PowerShell] doesn't contain a method named 'SetRunspace'



    I ran a $psCmd | gm



    and there isn't a method called 'SetRunspace'.



    What gives???



    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1647
    Avatar

    --
    30 Mar 2009 02:57 AM
    APIs change, even when something is in CTP.

    Before: $psCmd.Command.Clear()
    Now: $psCmd.Commands.Clear()

    Before: $psCmd.SetRunspace($rs)
    Now: $psCmd.Set_Runspace($rs)

    Try the above changes.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    30 Mar 2009 05:53 AM
    I understand that things change but...

    And that leads in to my next question. How would I have "Discovered" this change?

    $psCmd |gm

    doesn't expose that method.

    I mean shouldn't past blog posts be annotated. Otherwise you have a lot of searchable mis-information.
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1647
    Avatar

    --
    31 Mar 2009 06:19 AM
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    EBGreenUser is Offline
    Advanced Member
    Advanced Member
    Posts:859
    Avatar

    --
    31 Mar 2009 10:08 AM
    Get-Member does expose the method. You just have to know to ask for it:

    12:1:3 |(132)|P:\ PS>$psCMD | gm


    TypeName: System.Management.Automation.PowerShell

    Name MemberType
    ---- ----------
    InvocationStateChanged Event
    AddArgument Method
    AddCommand Method
    AddParameter Method
    AddParameters Method
    AddScript Method
    BeginInvoke Method
    BeginStop Method
    CreateNestedPowerShell Method
    Dispose Method
    EndStop Method
    Equals Method
    GetHashCode Method
    GetType Method
    Invoke Method
    Stop Method
    ToString Method
    Commands Property
    InstanceId Property
    InvocationStateInfo Property
    IsNested Property
    Runspace Property
    RunspacePool Property
    Streams Property

    Notice the Commands property. Remember that everything in PS is an object. So the property of an object is itself an object:

    12:2:17 |(133)|P:\ PS>$psCMD.Commands | gm


    TypeName: System.Management.Automation.PSCommand

    Name MemberType
    ---- ----------
    AddArgument Method
    AddCommand Method
    AddParameter Method
    AddScript Method
    Clear Method
    Clone Method
    Equals Method
    GetHashCode Method
    GetType Method
    ToString Method
    Commands Property

    So there is the Clear method.
    "Look Ma...no strings!"
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1647
    Avatar

    --
    31 Mar 2009 11:15 AM
    @EBGreen,

    Yes, the Clear() method is still there, but it would have been a bit more difficult to find the changes to the Get/Set Runspace members.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer