简体   繁体   中英

How to pass wpf text value to powershell script and run that powershell script

I have created a powershell script to install adobe. Now i want to create a GUI to use that and specify the path for adobe instllatation files. That installation files location should be taken as input to script andlater run the total script and iinstall adobe

WPF XAML

<TextBox x:Name="AdbPath" HorizontalAlignment="Left" Height="24" Margin="131,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="116"/>

C# code

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(); // ... 
}

As summmen says, powershell also supports GUI. You can create a nice GUI with powershell without extra .NET WPF app.

If you want to do it though here how i would do it (! Not tested !):

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! 
}

With this method, you have to parse the path to the PSScript and the params you like to parse to the PSScript. After the script runs, you can read the result out of psObject or do anything else with it.

Here is an example of WPF GUI in Powershell. No need for C#! However the GUI will freeze if you wait for the Installation to finish. You could use another thread (Job or Runspace) for that action or just execute the Installation when the GUI is closed.

# 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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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