2016-11-11 103 views
1

這個腳本作品(它設置給用戶的遠程桌面服務簡介在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 ActveDirectoryC#腳本的頂部,但沒有效果。我也在正常運行的腳本和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#中後


有沒有人碰巧知道可能會導致這種情況?

非常感謝

+1

嘗試添加導入模塊用於廣告的腳本,你在C#中運行。 –

+0

@Abhijithpk剛剛嘗試在腳本中添加'Import-Module ActiveDirectory',但仍然看到相同的錯誤 – Bassie

回答

1

更新答案:看來,這些屬性不存在2008+。請嘗試以下的人,而不是:

  • msTSAllowLogon
  • msTSHomeDirectory
  • msTSHomeDrive
  • msTSProfilePath

一看便知在this thread爲全面解釋。

原來的答案:

阿濟斯pk的的評論可能是答案。您需要運行Import-Module ActiveDirectory,就像您需要在命令行PowerShell中執行的操作一樣。

如果您曾在PowerShell命令行中運行過Import-Module ActiveDirectory,那麼您將知道需要一段時間才能加載。在C#中運行時會一樣。因此,如果您將在應用程序中運行多個AD命令,最好將Runspace對象保持爲靜態對象並重用它,這意味着您只需加載一次ActiveDirectory模塊。

這裏有關於如何做,在C#中的細節: https://blogs.msdn.microsoft.com/syamp/2011/02/24/how-to-run-an-active-directory-ad-cmdlet-from-net-c/

特別,這是代碼:

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

嗨加百利感謝您的回答。我嘗試在腳本的頂部包含'Import-Module ActiveDirectory',但我仍然看到同樣的錯誤。你知道還有什麼可能導致這種情況嗎? – Bassie

+0

我已經更新了我的答案。如果您的AD在Windows Server 2008+上運行,看起來您可能需要查看不同的屬性。 –

+0

謝謝,我會試一試。是否有可能成爲PowerShell中同樣的命令工作的問題? (仍然聯繫同一臺服務器) – Bassie

相關問題