繁体   English   中英

如何将 wpf 文本值传递给 powershell 脚本并运行该 powershell 脚本

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM