gpt4 book ai didi

c# - 与C#代码一起使用时,如何在Powershell中添加程序集引用?

转载 作者:行者123 更新时间:2023-12-02 23:32:59 26 4
gpt4 key购买 nike

添加时...

using System.DirectoryServices.AccountManagement;
我收到一个错误:

The type name or namespace "DirectoryServices" is not in the namespace "System" (missing the assembly link?) Add-Type: Failed to add type. Compilation errors detected.


整体代码:
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)

最佳答案

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)

关于c# - 与C#代码一起使用时,如何在Powershell中添加程序集引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62879351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com