2

こんにちは、Power Shell に 21 行のコードを含むファイルがあります。このファイルを C# のボタンで実行する方法が必要です。Visual Studio 2010 で c# を使用しています。達成する方法があれば教えてください。

// Powershell
$Proc = Get-Process | Sort-Object CPU -Descending
$cores = Get-WmiObject Win32_processor
$memory = Get-WmiObject Win32_OperatingSystem
$Total = 10
$num = 1

Clear-Content c:\scripts\prueba.txt

foreach ($objItm in $Proc) {
If ($num -gt $Total) {
    break #break the loop
}

[string] $strID=[System.Convert]::ToString($objItm.ID)
[string] $strProcessName=[System.Convert]::ToString($objItm.ProcessName)
[string] $strCPU=[System.Convert]::ToString($objItm.CPU)
[string] $strNUM=[System.Convert]::ToString($num)

$info=$strNUM+"-"+$strID+"-"+$strProcessName+"-"+$strCPU

$num += 1

$info|Add-Content c://scripts/prueba.txt

}
//Code c#
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); 
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline(); 
pipeline.Commands.Add(scriptFile)
Execute PowerShell script results = pipeline.Invoke();

たとえば、ボタンを押してスクリプトを実行する必要があります

4

2 に答える 2

0

Ingo KarsteinによるPS2EXEを使用できます

これにより、独自の powershell ホストとスクリプトが埋め込まれた ac# 実行可能ファイルが作成されます。

PS2EXEでc#コードを取得し、暗号化されたpowershellスクリプトの内容を読み取り、復号化して実行する機能を追加するac#プロジェクトがあります。

于 2013-07-23T07:58:12.670 に答える