how to force DSC script resource to run under STA apartment

Welcome Forums DSC (Desired State Configuration) how to force DSC script resource to run under STA apartment

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #58327

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 1,524
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

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

    }
    }

The topic ‘how to force DSC script resource to run under STA apartment’ is closed to new replies.