這個腳本作品(它設置給用戶的遠程桌面服務簡介在Active Directory設置):腳本可以在PowerShell中,但在PowerShell ISE中運行時,不C#
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
一個錯誤,我稱之爲:
Exception calling "InvokeSet" with "2" argument(s):
"Unknown name. (Exception from 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 = [email protected]"
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用戶的帳戶名。
我想包括Import-Module ActveDirectory
在C#
腳本的頂部,但沒有效果。我也在正常運行的腳本和C#
腳本中都調用了$PSVersionTable.PSVersion
,並且都返回了正在使用版本3的情況。
Exception calling "setinfo" with "0" argument(s): "The attribute syntax specified to the directory service is invalid.
而且在PowerShell中沒有查詢這些屬性(沒有錯誤,也沒有輸出):
更新屬性名稱
msTSProfilePath
msTSHomeDrive
msTSHomeDirectory
msTSAllowLogon
我得到這個錯誤在C#中後
有沒有人碰巧知道可能會導致這種情況?
非常感謝
嘗試添加導入模塊用於廣告的腳本,你在C#中運行。 –
@Abhijithpk剛剛嘗試在腳本中添加'Import-Module ActiveDirectory',但仍然看到相同的錯誤 – Bassie