Powershell-GUI in WPF...

This topic contains 4 replies, has 3 voices, and was last updated by  Renaud 3 weeks, 1 day ago.

  • Author
    Posts
  • #103166

    Renaud
    Participant
      Hi everybody,

      I'm trying to do a GUI for a Powershell Sript.
      I'have done the GUI with Visual Studio with a WPF Template.
      The PowerShell script:

      ############# Post-Configuration Nano Server ########################################
      ############# Declaration des variables Network ########################################
      $NomNS = ""
      $IPDns = ""
      $Mask = ""
      $GW = ""
      $IPType = ""

      Add-Type -AssemblyName PresentationFrameWork, PresentationCore
      $ConfNS = @{ }
      $inputXML = Get-Content -Path "C:\Users\kris8\source\repos\ConfigurationNanoServer\ConfigurationNanoServer\MainWindow.xaml"
      $inputXMLClean = $inputXML -replace 'mc:Ignorable="d"'," -replace "x:N",'N' -replace 'x:Class=".*?"'," -replace 'd:DesignHeight="\d*?"'," -replace 'd:DesignWidth="\d*?"',"
      [xml]$xaml = $inputXMLClean
      $reader = New-Object System.Xml.XmlNodeReader $xaml
      $tempform = [Windows.Markup.XamlReader]::Load($reader)
      $NameNodes = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")
      $NameNodes = $NameNodes | ForEach-Object {$ConfNS.Add($_.Name, $tempform.FindName($_.Name))}

      $ConfNS
      #Module a Importer ou Installer
      #

      ############Code Executer lors du click sur le boutton "Execute"#######################################
      ############Declaration des variables pour la configuration Network et Firewall du Nano Server#########

      $ConfNS.ConfNanoServerButton1.add_Click({

      #Declaration Nom du Nano Server
      $NomNS = $ConfNS.ConfNanoServerTextBox1.Text
      #Declaration @IP DNS
      $IPDns = $ConfNS.ConfNanoServerTextBox2.Text
      #Declaration Mask
      $Mask = $ConfNS.ConfNanoServerTextBox3.Text
      #Declaration Gateway
      $GW = $ConfNS.ConfNanoServerTextBox4.Text
      #Declaration @IP DNS
      $IPType = $ConfNS.ConfNanoServerTextBox5.Text

      })

      $ConfNS.ConfNanoServerWindows.ShowDialog() | Out-Null

      When i execute the script the Gui appears but when i click on the button normally the variable are define.
      But if i just look the content of $NomNS, there nothing.

      The goal of this script is to create another script with the good value :

      How can i keep the value i have define in the PowerShell-GUI ?
      Thx for replay.

  • #103174

    Don Jones
    Keymaster

    We appreciate your patience; some posts often get held for moderation. In the future, please don't repeatedly re-post as that locks your account out.

    We generally don't moderate on weekends (US time).

  • #103189

    Renaud
    Participant

    Oh i don't know , i don't understand what it done on the web site !
    I really doesn't understand.
    OK...An a time the post was ok after the post gone.
    I'm sorry so.

    I just want to give other information in the first post.

    Thx for all.

  • #103190

    postanote
    Participant

    Well, firstly, this line is not correct.

    $inputXMLClean = $inputXML -replace ...
    

    Because your quoting is not correct. If you open this code in the PowerShell_ISE of VSCode, you will see lots of error indicators, staring at the 2nd replace statement and forward.

    So, my suggestion would be to backup, step through your code one segment at a time to make sure you are getting what you'd expect.

  • #103220

    Renaud
    Participant

    Thx for you're replay ^^
    I'have take this code to a tutoriel at this URL :
    on this video
    I don't understand sometimes he used the " and other times the '.
    He said on the video this line is usefull because VS don't like something with PowerShell, so he did this code.

    I segment and execute my code in PowerShell_ISE, and i'havent some errors :

    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> Add-Type -AssemblyName PresentationFrameWork, PresentationCore
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $ConfNS = @{ }
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $inputXML = Get-Content -Path "C:\Users\kris8\source\repos\ConfigurationNanoServer\ConfigurationNanoServer\MainWindow.xaml"
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $inputXMLClean = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace 'x:Class=".*?"','' -replace 'd:DesignHeight="\d*?"','' -replace 'd:DesignWidth="\d*?"',''
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> [xml]$xaml = $inputXMLClean
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $reader = New-Object System.Xml.XmlNodeReader $xaml
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $tempform = [Windows.Markup.XamlReader]::Load($reader)
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $NameNodes = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $NameNodes = $NameNodes | ForEach-Object {$ConfNS.Add($_.Name, $tempform.FindName($_.Name))}
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> $ConfNS.ConfNanoServerButton1.add_Click({
      
      #Declaration Nom du Nano Server
      $NomNS = $ConfNS.ConfNanoServerTextBox1.Text
    
      #Declaration @IP DNS
      $IPDns = $ConfNS.ConfNanoServerTextBox2.Text
    
      #Declaration Mask
      $Mask = $ConfNS.ConfNanoServerTextBox3.Text
    
      #Declaration Gateway
      $GW = $ConfNS.ConfNanoServerTextBox4.Text
    
      #Declaration @IP DNS
      $IPType = $ConfNS.ConfNanoServerTextBox5.Text
    
      })
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0>   $ConfNS.ConfNanoServerWindows.ShowDialog() | Out-Null
    
    PS C:\WINDOWS\SysWOW64\WindowsPowerShell\v1.0> 

    The GUI open open witout error, but when i fill the different field and click on the button nothing passed.
    and the variable are not set.

You must be logged in to reply to this topic.