2013-12-20 102 views
0

我需要在Windows Server 2012計算機上安裝AD。有沒有.NET類允許做,而不使用Powershell?在C#中安裝Active Directory域服務

如果我使用C#的PowerShell:

command = "powershell.exe -Command \"& {Install-windowsfeature -name AD-Domain-Services –IncludeManagementTools}\"" 
procStartInfo = new ProcessStartInfo("cmd", "/c " + command); 
procStartInfo.RedirectStandardOutput = true; 
procStartInfo.UseShellExecute = false; 
procStartInfo.CreateNoWindow = true; 
System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo = procStartInfo; 
proc.Start(); 
result = proc.StandardOutput.ReadToEnd(); 

我有錯誤:

Install-windowsfeature : The term 'Install-windowsfeature' is not recognized \nas the name of a cmdlet, function, script file, or operable program.

這是非常奇怪,因爲安裝-windowsfeature是內部PowerShell的認可。

感謝您的任何答案。

+0

你是如何啓動的?我剛剛在2012服務器上進行了測試,並且該cmdlet可在64位PS會話中識別,但在x86會話中未識別。 – mjolinor

+0

我剛剛添加了代碼,謝謝。 – Sean

回答

0

看來Install-Windowsfeature cmdlet僅在64位ps會話中可用。 如果你從32位應用程序或命令窗口,啓動PowerShell中使用的完整路徑開展:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe 

這應返回8,如果你在64位會話是:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -command {[intptr]::size} 
+0

我從Visual Studio 2013啓動,無論如何,我嘗試使用Powershell的完整路徑,就像你說的,但問題是一樣的。 – Sean

+0

你運行了[intptr] :: size測試嗎? – mjolinor

+0

是的,我從DOS命令提示符下運行:C:\ Windows \ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe -command {[intptr] :: size}但他回答:[intptr] :: size。如果我在目標機器的Powershell中運行[intptr] :: size,我得到8個。 – Sean