该程序不起作用。它开始但有错误,没有读取两个xml文件?

时间:2019-12-02 13:32:57

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0

该程序不起作用。它开始但有错误,没有读取两个xml文件?

C:\ program> powershell.exe -noexit。\ 1.ps1 chpercent -Path1 c:\ program \ MIK_Autokontinent.xml -Path2 c:\ program \ MIK_AutoPremium.xml Get-Content:无法将参数绑定到“ Path”参数,因为它是一个空字符串。 C:\ program \ 1.ps1:20个字符:42+ $ inputpecentw1.Text = [xml](获取内容$ Path1)| ForEach-Object {$ ... + ~~~~~~ + CategoryInfo:InvalidData:(:) [Get-Content],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

获取内容:由于参数为空字符串,因此无法将其绑定到“ Path”参数。 C:\ program \ 1.ps1:21个字符:49+ $ inputpecentw2.Text = [xml](获取内容$ Path2)| ForEach-Obj ... + ~~~~~~ + CategoryInfo:InvalidData:(:) [获取内容],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 

文件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 个答案:

答案 0 :(得分:0)

在函数顶部添加一个param块,声明两条路径:

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

,然后将所有硬编码路径替换为那些

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

...等等,然后最终调用如下函数:

PS C:\> chpercent -Path1 c:\program\MIK_Autokontinent.xml -Path2 c:\program\MIK_AutoPremium.xml