Как в powershell добавить ссылку на сборку при использовании совместно с кодом c#?

При добавлении

using System.DirectoryServices.AccountManagement;

Получаю ошибку:

Имя типа или пространства имен "DirectoryServices" отсутствует в пространстве имен "System" (пропущена ссылка на сборку?) Add-Type : Не удалось добавить тип. Обнаружены ошибки компиляции.

Код целиком:

Add-Type -Language CSharp @"

using Microsoft.Win32;
using System.DirectoryServices.AccountManagement;

namespace MyApp
{
    public static class Program 
    {
        public static string GetCurrentUser()
        {
            _ = new PrincipalContext(ContextType.Domain);
            UserPrincipal user = UserPrincipal.Current;
            return user.ToString();
        }
        public static void SetRegKey(string userName)
        {
            string regString = userName;
            string subKey = @"SYSTEM\ControlSet001\Services\LanmanServer\Parameters";
            RegistryKey key = Registry.LocalMachine.CreateSubKey(subKey);
            key.SetValue("srvcomment", regString);
            key.Close();
        }
    }
}

"@;

$userName = [MyApp.Program]::GetCurrentUser()
[MyApp.Program]::SetRegKey($userName)

Ответы (1 шт):

Автор решения: Malezha
Add-Type -ReferencedAssemblies "System.DirectoryServices.AccountManagement" -Language CSharp @"

using Microsoft.Win32;
using System.DirectoryServices.AccountManagement;

namespace MyApp
{
    public static class Program 
    {
        public static string GetCurrentUser()
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal user = UserPrincipal.Current;
            return user.ToString();
        }
        public static void SetRegKey(string userName)
        {
            string regString = userName;
            string subKey = @"SYSTEM\ControlSet001\Services\LanmanServer\Parameters";
            RegistryKey key = Registry.LocalMachine.CreateSubKey(subKey);
            key.SetValue("srvcomment", regString);
            key.Close();
        }
    }
}

"@;

$userName = [MyApp.Program]::GetCurrentUser()
[MyApp.Program]::SetRegKey($userName)
→ Ссылка