how to force DSC script resource to run under STA apartment

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of vamsee vamsee 1 week, 5 days ago.

  • Author
    Posts
  • #58327
    Profile photo of vamsee
    vamsee
    Participant

    I have a .NET DLL whose methods i want to call from DSC script resource. However the catch is that the .NET DLL expects STA threads only to call the methods. MTA threads will not work.
    When i called the .NET method from DSC script resource, I am seeing that the method is not running because it is expecting STA whereas the Script is running in MTA.
    Below is the bare bones Script resource
    Script change_Ddrivelabel{
    PSDscRunAsCredential=$mycred
    SetScript= {
    $keypath="Software\Policies\Microsoft\Internet Explorer\LinksBar"
    $valname="Enabled"
    [int] $data=0
    [Microsoft.Win32.RegistryValueKind] $regtype=[Microsoft.Win32.RegistryValueKind]::DWord
    [string] $lgpoerr = $null

    [Reflection.Assembly]::LoadFile("D:\vamc\test-lgpo\LocalPolicy.dll")
    $lgpo = new-object LocalPolicy.LGPO_Executor
    $success=$lgpo.ApplyLGPO($keypath,$valname,$data,$regtype,[ref][string]$lgpoerr) #this is the method call that expects to run in STA but instead is being run in MTA.
    if($success) {
    "Success" | Out-File -FilePath "D:\vamc\test-lgpo\out.txt"
    }
    else {
    $lgpoerr | Out-File -FilePath "D:\vamc\test-lgpo\out.txt"
    }

    }
    GetScript= {
    @{
    GetScript=$GetScript
    SetScript=$SetScript
    TestScript=$TestScript
    Result=""
    }
    }
    TestScript= {
    return $false
    }

    }
    Question:- How do i force DSC to run a certain script resource in STA apartment model instead of MTA apartment model?

  • #58337
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I'm fairly certain you can't. Not without rewriting the LCM. Aside from the general bad-idea-ness of Script resources in terms of maintainability and modularity, the closest workaround I could think of would be to launch PowerShell.exe, since there's a switch that let's you specify.

  • #58361
    Profile photo of vamsee
    vamsee
    Participant

    I found a simple way to solve this. See the below code
    Script somescriptResourceName{
    SetScript= {
    [System.Threading.Thread]::CurrentThread.ApartmentState=[System.Threading.ApartmentState]::STA

    }
    }

You must be logged in to reply to this topic.