【发布时间】:2017-03-25 23:29:45
【问题描述】:
此脚本在 PowerShell ISE 中运行时有效(它在 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()
}
但是当我尝试从 C# 应用程序运行它时,我调用的每个 invokeset 都会出现错误:
使用“2”个参数调用“InvokeSet”的异常:
“未知名称。(来自 HRESULT 的异常:0x80020006 (DISP_E_UNKNOWNNAME))”
这是代码,在我的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;
}
我这样称呼它:
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);
其中newStarter.DotName 包含(已经存在的)AD 用户的帐户名。
我尝试在C# 脚本的顶部包含Import-Module ActveDirectory,但没有任何效果。我还在正常运行的脚本和C# 脚本中调用了$PSVersionTable.PSVersion,并且都返回正在使用的版本3。
将属性名称更新为
msTSProfilePath
msTSHomeDrive
msTSHomeDirectory
msTSAllowLogon
我在 C# 中遇到此错误:
使用“0”参数调用“setinfo”的异常:“指定给目录服务的属性语法无效。
并且在 PowerShell 中什么也不查询这些属性(没有错误但也没有输出)
有谁知道这是什么原因造成的?
非常感谢
【问题讨论】:
-
尝试在您在 c# 中运行的脚本中添加 import-module for ad。
-
@Abhijithpk 刚刚尝试将
Import-Module ActiveDirectory添加到脚本中,但仍然看到相同的错误
标签: c# powershell active-directory terminal-services