Script works in powershell but not C #

This script works when running in PowerShell ISE (it sets the given user to Remote Desktop Service Settings in Active Directory):

Get-ADUser FirstName.LastName | ForEach-Object {
    $User = [ADSI]"LDAP://$($_.DistinguishedName)"
    $User.psbase.invokeset("TerminalServicesProfilePath","\\Server\Share\HomeDir\Profile")
    $User.psbase.invokeset("TerminalServicesHomeDrive","H:")
    $User.psbase.invokeset("TerminalServicesHomeDirectory","\\Server\Share\HomeDir") 
    $User.setinfo()
}

      

But when I try to run it from a C # application, I get an error for each invokeset

one I call:

Throwing an "InvokeSet" exception with arguments "2":

"Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"

Here is the code inside my class PowerShell

:

public static List<PSObject> Execute(string args)
{
    var returnList = new List<PSObject>();

    using (var powerShellInstance = PowerShell.Create())
    {
        powerShellInstance.AddScript(args);
        var psOutput = powerShellInstance.Invoke();


        if (powerShellInstance.Streams.Error.Count > 0)
        {
            foreach (var error in powerShellInstance.Streams.Error)
            {
                Console.WriteLine(error);
            }
        }

        foreach (var outputItem in psOutput)
        {
            if (outputItem != null)
            {
                returnList.Add(outputItem);
            }
        }
    }

    return returnList;
}

      

And I call it like this:

var script = $@"
                Get-ADUser {newStarter.DotName} | ForEach-Object {{
                    $User = [ADSI]""LDAP://$($_.DistinguishedName)""
                    $User.psbase.invokeset(""TerminalServicesProfilePath"",""\\file\tsprofiles$\{newStarter.DotName}"")
                    $User.psbase.invokeset(""TerminalServicesHomeDrive"",""H:"")
                    $User.psbase.invokeset(""TerminalServicesHomeDirectory"",""\\file\home$\{newStarter.DotName}"") 
                    $User.setinfo()
                }}";

PowerShell.Execute(script);

      

Where newStarter.DotName

contains the (pre-existing) AD user account name.


I tried including Import-Module ActveDirectory

in the beginning of the C#

script, but with no effect. I also called $PSVersionTable.PSVersion

both in the script running fine and in the C#

script and both returned this version 3.


After updating the property names in

msTSProfilePath
msTSHomeDrive
msTSHomeDirectory
msTSAllowLogon

      

I am getting this error in C #:

Instance calling "setinfo" with arguments "0": "The attribute syntax specified for the directory service is not valid.

And requesting these properties in PowerShell is nothing (no errors, but also no output)


Does anyone know what might be causing this?

Many thanks

+1


source to share


1 answer


Updated answer: It seems that these attributes don't exist in 2008. Try this instead:

  • msTSAllowLogon
  • msTSHomeDirectory
  • msTSHomeDrive
  • msTSProfilePath

For a full explanation see the answer in this thread .

Original answer:

Comment from Abhijith pk is probably the answer. You need to run Import-Module ActiveDirectory

as you need to do in the PowerShell command line.



If you've ever run Import-Module ActiveDirectory

PowerShell from the command line, you know it takes a while to load. It will be the same when run in C #. So if you will be running multiple AD commands in your application, you are better off leaving the Runspace object alive as a static object and reusing it, which means you only load the ActiveDirectory module once.

Here's how to do it in C # in detail: https://blogs.msdn.microsoft.com/syamp/2011/02/24/how-to-run-an-active-directory-ad-cmdlet-from-net -c /

Specifically, this is the code:

InitialSessionState iss = InitialSessionState.CreateDefault(); 
iss.ImportPSModule(new string[] { "activedirectory" }); 
Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss); 
myRunSpace.Open();

      

+1


source







All Articles