0

The program does not work. It started but with errors and did not read two xml file?

C: \program> powershell.exe -noexit .\1.ps1 chpercent -Path1 c:\program\MIK_Autokontinent.xml -Path2 c:\program\MIK_AutoPremium.xml Get-Content: Cannot bind the argument to the "Path" parameter because it is an empty string. C:\program\1.ps1: 20 character: 42+ $inputpecentw1.Text = [xml] (Get-Content $Path1) | ForEach-Object {$ ...+ ~~~~~~+ CategoryInfo: InvalidData: (:) [Get-Content], ParameterBindingValidationException+FullyQualifiedErrorId: ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content: Cannot bind the argument to the "Path" parameter because it is an empty string. C:\program\1.ps1: 21 character: 49+ $inputpecentw2.Text = [xml] (Get-Content $ Path2) | ForEach-Obj ...+ ~~~~~~+ CategoryInfo: InvalidData: (:) [Get-Content], ParameterBindingValidationException+FullyQualifiedErrorId: ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.GetContentComman

Add-Type -AssemblyName System.Windows.Forms
    function chpecent {
    param(
      [string]$Path1,
      [string]$Path2
    )
    $form1 = New-Object 'System.Windows.Forms.Form'
    $textnamef1 = New-Object 'System.Windows.Forms.Label'
    $textnamef2 = New-Object 'System.Windows.Forms.Label'
    $textPercent = New-Object 'System.Windows.Forms.Label'
        $inputpecentw1 = New-Object 'System.Windows.Forms.TextBox'
        $inputpecentw2 = New-Object 'System.Windows.Forms.TextBox'
        $Read = New-Object 'System.Windows.Forms.Button'
        $Save = New-Object 'System.Windows.Forms.Button'
        $form1_Load = {
     $inputpecentw1.Text = [xml](Get-Content $Path1) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
            $inputpecentw2.Text = [xml](Get-Content $Path2) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
        }    
        $Read_Click = {
            #press read button
          $inputpecentw1.Text = [xml](Get-Content $Path1) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
            $inputpecentw2.Text = [xml](Get-Content $Path2) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
             }
        $Save_Click = {
            #press save button
             $new_value1 = $inputpecentw1.Text
             $new_value2 = $inputpecentw2.Text
            [xml](Get-Content $Path1)  | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.SetAttribute("Percent", $new_value1) }; $_.Save($Path1) }
            [xml](Get-Content $Path2)  | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.SetAttribute("Percent", $new_value2) }; $_.Save($Path2) }
          } 
         $form1.Controls.Add($textnamef1)
         $form1.Controls.Add($textnamef2)
        $form1.Controls.Add($inputpecentw1)
        $form1.Controls.Add($inputpecentw2)
            $form1.Controls.Add($Read)
        $form1.Controls.Add($Save)
        $form1.ClientSize = '800, 800'
        $form1.Text = 'проценты'
        $form1.add_Load($form1_Load)
        $textnamef1.AutoSize = $True
        $textnamef1.Location = '80, 40'
        $textnamef1.Text = 'MIK_Autokontinent.xml'    
        $textnamef2.AutoSize = $True
        $textnamef2.Location = '80, 80'
        $textnamef2.Text = 'MIK_AutoPremium.xml'    
        $textPercent.AutoSize = $True
        $textPercent.Location = '20, 10'
        $textPercent.Text = 'Percent'
        $inputpecentw1.Location = '20, 40'
        $inputpecentw1.Size = '20, 20'

        $inputpecentw2.Location = '20, 80'
        $inputpecentw2.Size = '20, 20'
        $Read.Location = '20,740'
        $Read.Size = '100, 40'
        $Read.Text = 'Прочитать'
        $Read.add_Click($Read_Click)
        $Save.Location = '680,740'
        $Save.Size = '100, 40'
        $Save.Text = 'Сохранить'
        $Save.add_Click($Save_Click)
      return $form1.ShowDialog()
    } #End Function
    chpecent | Out-Null 

Example of file MIK_Autokontinent.xml

<?xml version="1.0" encoding="utf-8"?><FieldConditions Enable="0" Version="1"/><FieldCostOptions Enabled="1" Version="1"><IncreaseCost Enabled="1" Percent="-5"/></FieldCostOptions>

1 Answer 1

0

Add a param block to the top of your function, declaring the two paths:

function chpecent {
param(
  [string]$Path1,
  [string]$Path2
)
...
}

and then replace all hardcoded paths with those:

    $form1_Load = {
        $inputpecentw1.Text = [xml](Get-Content $Path1) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
        $inputpecentw2.Text = [xml](Get-Content $Path2) | ForEach-Object { $_.SelectNodes(' //FieldCostOptions/IncreaseCost') | ForEach-Object { $_.GetAttribute("Percent") } } | Out-String
    } 

... and so on, and then finally call the function like:

PS C:\> chpercent -Path1 c:\program\MIK_Autokontinent.xml -Path2 c:\program\MIK_AutoPremium.xml
Sign up to request clarification or add additional context in comments.

5 Comments

@danrather you need to dot-source (or "import") the function first. . .\path\to\script.ps1; chpercent -Path1 c:\program\MIK_Autokontinent.xml -Path2 c:\program\MIK_AutoPremium.xml
Please read the actual error messages. Add-Type -AssemblyName System.Windows.Forms
@danrather no, at the very top of your script
@danrather read your error messages :) Try this
how do i run this program?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.