【问题标题】:How to pass wpf text value to powershell script and run that powershell script如何将 wpf 文本值传递给 powershell 脚本并运行该 powershell 脚本
【发布时间】:2020-12-20 09:25:17
【问题描述】:

我创建了一个用于安装 adobe 的 powershell 脚本。现在我想创建一个 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(); // ... 
}

【问题讨论】:

  • powershell 也支持 GUI - 所以你不需要额外的 .NET WPF 应用程序来实现这个 btw
  • 您可以以接受路径作为参数的方式创建 powershell 脚本。通过将路径作为 powershell 脚本参数传递来从 wpf 应用程序调用 powershell 脚本
  • WPF
  • private void BtnInstall_Click(object sender, RoutedEventArgs e) { var process = new Process(); process.StartInfo.FileName = "powershell.exe"; process.StartInfo.Arguments = @"C:\Temp\adobe2Rev1.ps1";进程.开始();进程.WaitForExit(); // ... }
  • 这是我调用脚本的 wpf 和 C# 代码

标签: c# wpf powershell


【解决方案1】:

正如 summmen 所说,powershell 也支持 GUI。您无需额外的 .NET WPF 应用程序即可使用 powershell 创建一个漂亮的 GUI。

如果你想在这里做,我会怎么做(!未测试!):

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 中读取结果或用它做任何其他事情。

【讨论】:

    【解决方案2】:

    这是 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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      相关资源
      最近更新 更多