Powershellコマンドレットを使用してC#でAzure VMを管理するための.Netフォームアプリケーションを開発しようとしています。私はAzureモジュールを使用してこれを動作させる必要があります。C#を使用してPowershellコマンド(azureVM)を実行
は、コマンドレットの一つアドインAzureAccountされます
を私の質問は、私はC#プロジェクトでは、このモジュール(アズール)を含めることができる方法ですか?
Powershellコマンドレットを使用してC#でAzure VMを管理するための.Netフォームアプリケーションを開発しようとしています。私はAzureモジュールを使用してこれを動作させる必要があります。C#を使用してPowershellコマンド(azureVM)を実行
は、コマンドレットの一つアドインAzureAccountされます
を私の質問は、私はC#プロジェクトでは、このモジュール(アズール)を含めることができる方法ですか?
@Prageeth Saravananは、コメントセクションで、PowerShellをC#でどのように統合するかについての有用なリンクを提供しました。
https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/
クイック例:
System.Management.Automation
System.Collections.ObjectModel
注:
まず私は、これらのレフリーを含める必要がありましたあなたが "Management.Automation" のNuGetパッケージを追加する必要があります。 「System.Management.Automation」と入力するだけで検索できます。
C#コード:
//The first step is to create a new instance of the PowerShell class
using (PowerShell powerInstance = PowerShell.Create()) //PowerShell.Create() creates an empty PowerShell pipeline for us to use for execution.
{
// use "AddScript" to add the contents of a script file to the end of the execution pipeline.
// use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
PowerShellInstance.AddScript("param($param1) $d = get-date; $s = 'test string value'; $d; $s; $param1; get-service");
// use "AddParameter" to add a single parameter to the last command/script on the pipeline.
PowerShellInstance.AddParameter("param1", "parameter 1 value!");
//Result of the script with Invoke()
Collection<PSObject> result = powerInstance.Invoke();
//output example : @{yourProperty=value; yourProperty1=value1; yourProperty2=StoppedDeallocated; PowerState=Stopped; OperationStatus=OK}}
foreach (PSObject r in result)
{
//access to values
string r1 = r.Properties["yourProperty"].Value.ToString();
}
}
・ホープ、このことができます!
PowerShellコマンドレットImport-moduleを使用して、対応するモジュールを現在のセッションに追加することができます。 forceパラメータを使用して、モジュールを同じセッションに再インポートすることができます。
Import-module -name azure -force
インポートされるモジュールは、ローカルコンピュータまたはリモートコンピュータにインストールする必要があります。 Azure PowerShellコマンドレットをC#プロジェクトから実行してAzure PowerShellがインストールされていることを確認する必要がある場合は、インストールモジュールAzureRMまたはAzureを使用することができます。詳細はGet Started Azure PowerShell cmdletsを参照してください。 Azure VMでは、デフォルトでAzure PowerShellがインストールされます。 C#を使用してPowerShellコマンドまたはPS1ファイルを呼び出す方法については、linkまたは別のSO Threadと記載されているPrageeth Saravananを参照してください。
PSスクリプトを作成してC#から呼び出す必要があります。 https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/ –
このスレッドに関する最新情報はありますか? –
私はちょうどanswerert @ TomSun-MSFTを投稿しました –