powershell 経由でいくつかの機能をインストールしたいと思います。機能をインストールしていなくてもPSObject
戻ります。Installed
環境: Windows Server 2019 x64、アプリ: .NET Framework + PowerShellStandard.Library 5.1
ServerManager
アプリはモジュールを実行する x64 用にビルドされています。
管理コンソールなどの機能をインストールするコード
InstallWindowsFeature(powerShell, "Web-Mgmt-Console");
インストール機能
void InstallWindowsFeature(PowerShell powerShell, string featureName)
{
if(IsWindowsFeatureInstalled(powerShell, featureName)) // Get-WindowsFeature call..
{
Console.WriteLine($"{featureName} is already installed. Skipping install...");
return;
}
powerShell.AddCommand("Install-WindowsFeature");
powerShell.AddParameter("Name", featureName);
Collection<PSObject> psObjects = powerShell.Invoke();
foreach (dynamic psObject in psObjects.Where(x => x != null))
Console.WriteLine($"Result: Success - {psObject.Success}, Restart needed: {psObject.RestartNeeded}, Exit code: {psObject.ExitCode}");
}
PowerShell 初期化
InitialSessionState initialSessionState = InitialSessionState.CreateDefault();
initialSessionState.ImportPSModule(new string[] { "ServerManager" });
Runspace powerShellRunscape = RunspaceFactory.CreateRunspace(initialSessionState);
powerShellRunscape.Open();
PowerShell powerShell = PowerShell.Create();
powerShell.Runspace = powerShellRunscape;
// install call
powerShell.Dispose();
powerShellRunscape.Close();
Install-WindowsFeature を正しく実行するには?