[英]How to pass a powershell script to Windows PowerShell Host in C#?
[英]How to pass wpf text value to powershell script and run that powershell script
我創建了一個 powershell 腳本來安裝 adobe。 現在我想創建一個 GUI 來使用它並指定 adobe 安裝文件的路徑。 該安裝文件位置應作為腳本的輸入,然后運行整個腳本和 iinstall adobe
WPF XAML
<TextBox x:Name="AdbPath" HorizontalAlignment="Left" Height="24" Margin="131,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>
C# 代碼
private void BtnInstall_Click(object sender, RoutedEventArgs e)
{
var process = new Process();
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = @"C:\Temp\adobe2Rev1.ps1";
process.Start();
process.WaitForExit(); // ...
}
正如 summmen 所說,powershell 也支持 GUI。 您可以使用 powershell 創建一個漂亮的 GUI,而無需額外的 .NET WPF 應用程序。
如果你想這樣做,我會怎么做(!未經測試!):
private static void RunPSScript(string path, string[] params)
{
Collection<PSObject> psObjects;
RunspaceConfiguration rsConf= RunspaceConfiguration.Create();
using(Runspace rs = RunspaceFactory.CreateRunspace(rsConf)){
rs.Open();
RunspaceInvoke rsInvoke= new RunspaceInvoke(rs);
using(Pipeline pipeline = runspace.CreatePipeline()){
Command scriptCmd = new Command(path);
Collection<CommandParameter> cmdParams= new Collection<CommandParameter>();
foreach (string scriptParam in params)
{
CommandParameter cmdParam = new CommandParameter(null, scriptParameter);
cmdParams.Add(commandParm);
scriptCmd.Parameters.Add(cmdParam);
}
pipeline.Commands.Add(scriptCmd);
psObjects = pipeline.Invoke();
}
}
//Do something with psObjects!
}
使用此方法,您必須解析 PSScript 的路徑以及您想解析為 PSScript 的參數。 腳本運行后,您可以從 psObject 中讀取結果或對其執行任何其他操作。
這是 Powershell 中的 WPF GUI 示例。 不需要 C#! 但是,如果您等待安裝完成,GUI 將凍結。 您可以為該操作使用另一個線程(作業或運行空間),或者僅在 GUI 關閉時執行安裝。
# Assamblies
try{
Add-Type -AssemblyName PresentationFramework
} catch { Throw "Failed to load assemblies."}
# Could aswell be an Get-Content of an XML-file
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Title" WindowStartupLocation = "CenterScreen"
SizeToContent = "WidthAndHeight" ShowInTaskbar = "True">
<StackPanel >
<TextBox x:Name="AdbPath" Text="C:\Temp" HorizontalAlignment="Left" Height="24" Margin="131,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>
<Button x:Name="button1" Content="Install" Height="24" Margin="131,52,0,0" Width="116" />
<Button x:Name="button2" Content="Close and Install" Height="24" Margin="131,52,0,0" Width="116" />
</StackPanel>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
# Create variables for named XML nodes
$Xaml.SelectNodes("//*[@*[local-name()='Name']]") | ForEach-Object {
New-Variable -Name $_.Name -Value $($Window.FindName($_.Name)) -Force
}
# Create a button Click event
$button1.add_Click({
$InstallPath = $AdbPath.Text
Write-Warning "Installing Adobe in $InstallPath.."
# Note that the GUI will freeze while being busy
sleep 5
Write-Warning "Installation Done"
})
$button2.add_Click({
$InstallPath = $AdbPath.Text
$Window.DialogResult = $true
})
[void]$Window.ShowDialog()
if ($Window.DialogResult -eq $true){
#Install after the window is closed
Write-Warning "Installing Adobe in $InstallPath.."
sleep 3
Write-Warning "Installation Done"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.