【问题标题】:Powershell-Passing in Parameters into a Function chainPowershell-将参数传入函数链
【发布时间】:2012-07-16 16:08:18
【问题描述】:

您好,我是 Powershell 的新手,正在尝试构建一系列由顶层父函数管理的函数。 这是我正在创建的函数:

Function TraceIntegrityCheck{
    param($MasterTracefile, $FolderWhereATFare)

$MasterTraceFile$FolderWhereATFare 将是文件路径。我将传递这些。下面是我的所有内部函数的脚本,这些函数将使用$MasterTraceFile$FolderWhereATFare 路径。

#-------------------------------MasterTraceConfiguration--------------------------
#*********************************************************************************
Function TraceIntegrityCheck{
param($MasterTracefile, $FolderWhereATFare)
[string](cat $MasterTracefile) | Foreach-Object {$_ -replace "\(in\)", ""} | Set-      Content 'C:\tempFile.txt'
$src = [IO.File]::ReadAllText('C:\tempFile.txt')
$pattern ='(?<Key>(APDU\s*:\s*80\s*E[68]))\s*(?<Num>(\b[0-9A-F]{2}\s+)+)'
[Regex]::Matches($src, $pattern) | % { [Regex]::Replace($_.Groups     ['Key'].Value, 'APDU\s:\s*', '') + ' ' + [Regex]::Replace($_.Groups ['Num'].Value, '\s+', ' ') }|set-content 'C:\tempFile.txt' 
$file1 = 'C:\tempFile.txt'
$file2 = 'C:\tempFileMod.txt'
$startValue = '^80 E6'
$innerValue = '^80 E8'
$regex4 = "(?m)\A(?=$startValue)|\r\n(?=$startValue)"
$regex5 = "(?m)$innerValue"
$content = [IO.File]::ReadAllText($file1).TrimEnd()
$content -split $regex4 | Select-Object -Skip 1 |
  ForEach-Object -Begin {$i = 1} -Process {
    if ($_ -match $regex5) {
      $replacement = '{0:00}' -f $i++
      $file2 = $file2 -replace '\b(?=\.\w+$)', $replacement
      Set-Content -Path $file2 -Value $_ #-Verbose
    }
  }

#-------------------------------ATF Configuration---------------------------------
#*********************************************************************************

Function allOnOneLine{  
(([regex]::replace((get-content $FolderWhereATFare),"\s+"," "))).replace    ("9000","`r`n9000`r`n") > $FolderWhereATFare
}
#Call the Function
allOnOneLine

Function StripATF{
$original_file = '$FolderWhereATFare'
$destination_file =  'C:\modatfLinedup.txt'
(Get-Content $original_file) | Foreach-Object {
    $_ -replace '9000', '' 

    } | Set-Content $destination_file
}
#Call the StripATF
StripATF
#And the white space stripper
Function Wspace{
$file1 = 'C:\modatfLinedup.txt'
(gc $file1) | ? {$_.trim() -ne "" } | set-content $file1
}
#Call the Wspace
Wspace
Function spaceStrip{
$r=[regex]' ';
$original_file2 = 'C:\modatfLinedup.txt'
$destination_file2 ='C:\modatfLinedup.txt'
(Get-Content $original_file2) | Foreach-Object {
    $_ -replace '^ ', ''

     }| Set-Content $destination_file2
}
 #Call the Function
spaceStrip
Function folderDistribute{
  $file3 = 'C:\modatfLinedup.txt'
$file4 = 'C:\modATFLinedup.txt'
$startValue = '^80 E6'
$innerValue = '^80 E8'
$regex4 = "(?m)\A(?=$startValue)|\r\n(?=$startValue)"
$regex5 = "(?m)$innerValue"
$content = [IO.File]::ReadAllText($file3).TrimEnd()
$content -split $regex4 | Select-Object -Skip 1 |
  ForEach-Object -Begin {$i = 1} -Process {
    if ($_ -match $regex5) {
      $replacement = '{0:00}' -f $i++
  $file4 = $file4 -replace '\b(?=\.\w+$)', $replacement
  Set-Content -Path $file4 -Value $_ #-Verbose
    }
   }
#Call the Function
folderDistribute
}
}
C:\Users\nicalder\Desktop\Project1\atf\atf\modatfs\modatf.txt
TraceIntegrityCheck -file1 'C:\Users\user\Desktop\Project1\Master_Trace.txt' -    file2 'C:\Users\user\Desktop\Project1\modatf.txt'

我的问题在于尝试将原始 $MasterTraceFile 和 $FolderWhereATFare 文件串起来,以便我可以将它们传递给函数并让子函数都使用它们。

所以我真正想做的只是能够传入这两个参数文件路径,以便子函数可以使用它们。我想本质上是创建一个允许我使用传入参数的构造函数。

我知道这可能看起来令人困惑,所以请让我知道您的想法。我只是想通过子函数连接文件。

【问题讨论】:

    标签: powershell powershell-2.0 powershell-1.0 powershell-3.0


    【解决方案1】:

    您似乎想要执行以下操作:

    function outer($a) {
        function inner($b){
            write-host -fore red $a
            write-host -fore red $b
        }
    
        inner 2
    
    }
    
    outer 1
    

    上面确实打印了:

    1
    2
    

    outer函数中的$ainner函数中可用

    【讨论】:

    • 是的,谢谢 manojlds!这就是我想要做的,我发现当我传入一个参数时,我必须用双引号传入文件路径而不是像这样的单引号: TraceIntegrityCheck "C:\Users\nicalder\Desktop\ Project1\MasterCard_Trace\MasterCard_Trace.txt" "C:\Users\nicalder\Desktop\Project1\atf\atf*.txt" 其中:traceIntegrityCheck -file1 'C:\Users\nicalder\Desktop\Project1\MasterCard_Trace\MasterCard_Trace。 txt' -file2 'C:\Users\nicalder\Desktop\Project1\atf\atf*.txt' 非常感谢您对此的帮助!你摇滚!
    猜你喜欢
    • 2023-03-26
    • 2022-10-07
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多