1

您好,我對Powershell來說是相當新的,並且正在嘗試構建一系列由頂級父級函數管理的函數。 這是我創造的功能: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文件,以便我可以將它們傳遞到功能,並有孩子的功能使用它們。

所以我真正想要做的只是能夠傳遞這兩個參數文件路徑,以便子功能可以使用它們。我想本質上是創建一個構造函數,允許我使用傳入參數。

我知道這可能會讓人感到困惑,所以請讓我知道您的想法。我只是試圖通過兒童功能連接文件。

回答

3

好像你想要做的事,如:

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

    inner 2 

} 

outer 1 

上面並打印:

1 
2 

也就是說,$aouter功能可在inner功能

+0

是謝謝你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'非常感謝您的幫助!你搖滾! – 2012-07-18 14:12:36