首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将wpf文本值传递给powershell脚本并运行powershell脚本

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

Stack Overflow用户
提问于 2020-09-01 17:11:43
回答 2查看 165关注 0票数 0

我已经创建了一个powershell脚本来安装adobe。现在我想创建一个GUI来使用它,并指定adobe安装文件的路径。应该将该安装文件位置作为脚本的输入,然后运行total脚本和iinstall adobe

WPF XAML

代码语言:javascript
复制
<TextBox x:Name="AdbPath" HorizontalAlignment="Left" Height="24" Margin="131,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>

C#代码

代码语言:javascript
复制
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(); // ... 
}
EN

回答 2

Stack Overflow用户

发布于 2020-09-01 17:49:07

正如科曼所说,powershell也支持GUI。你可以用powershell创建一个很好的图形用户界面,而不需要额外的.NET WPF应用。

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

代码语言:javascript
复制
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中读取结果或对其执行任何其他操作。

票数 0
EN

Stack Overflow用户

发布于 2020-09-01 22:51:12

以下是Powershell中的WPF GUI示例。不需要C#!但是,如果您等待安装完成,GUI将冻结。您可以使用另一个线程(Job或Runspace)来执行该操作,或者在GUI关闭时执行安装。

代码语言:javascript
复制
# 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"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63684688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档