簡體   English   中英

Powershell - 分隔輸出並在內部操作數據

[英]Powershell - Delimit output and manipulate data inside

我正在嘗試創建一個 Powershell 腳本,該腳本基於輸入的 TXT 或 CSV,將其拆分為由特定正則表達式分隔的部分,然后根據其他正則表達式操作其中的數據。

更具體地說,我試圖操作來自 NAT 規則庫的數據,其格式如下:

IP version            : IPv4
Index                 : 1
----------General----------
Original Source       : Any
Translated Source     : ORIGINAL
Original Destination  : 192.168.1.1
Translated Destination: ORIGINAL
Original Service      : IKE
Translated Service    : ORIGINAL
Inbound Interface     : Any
Outbound Interface    : Any
Comment               : IKE NAT
Enable NAT Policy     : True
System Policy         : True
----------Advanced----------
[...] (irrelevant data)

我想要實現的是獲取所有數據並對其進行操作以創建一個命令,該命令將通過另一個防火牆上的 API 為 TXT 文件中存在的所有 NAT 規則自動添加該規則,因此對於此示例:

add nat-rule position 1 enabled true original-source "Any" translated-source "Original" original-destination "192.168.1.1" translated-destination: "Original" original-service "IKE" translated-service: "Original" comment "IKE NAT"

此規則的位置應基於“索引”變量。

我基本上堅持說程序:“這些部分以'IP版本'開始並以'系統策略:......'結束

對於這些部分中的每一個,我相信我可以為變量分配一個正則表達式,如果匹配,它會將值分配給一個新變量。

如何通過使用 Powershell 實現這一目標?

到目前為止,我已經設法生成了以下腳本:

$filepath = 'C:\test.txt'
$getfile = Get-Content $filepath -Raw
$splitfile = $getfile - split 

foreach($object in $splitfile){
 Write-Host $object
}

提前致謝。

要解析這樣的文件,您確實需要進行一些拆分和替換。

完成后,我認為最簡單的方法是使用ConvertFrom-StringData cmdlet 獲取 Hashtable 中的所有值,並使用該哈希中的值填充模板字符串以構建命令字符串。

像這樣的東西:

$filepath = 'C:\test.txt'
$content  = Get-Content -Path $filepath -Raw
# build a template string for the NAT rules
$natRule = 'position {0} enabled {1} original-source "{2}" translated-source "{3}" ' +
           'original-destination "{4}" translated-destination: "{5}" '+
           'original-service "{6}" translated-service: "{7}" comment "{8}"'

# do some text splitting and replacing to create a Hashtable of values in each text block
$content -split 'IP version' | Where-Object { $_ -match '\S' } | ForEach-Object {
    # for each text block, take off the stuff starting at "----------Advanced----------",
    # remove the line "----------General----------",
    # replace the colons (:) with equal signs (=),
    # and convert this data into a Hashtable
    # the final replacement doubles any backslash because 
    # ConvertFrom-StringData regards them as regex escape characters
    $ht = 'IP version' + ($_ -split '\r?\n-+Advanced-+')[0] -replace 
          '\r?\n-+General-+' -replace ':', '=' -replace
          '\\', '\\' | ConvertFrom-StringData    

    # next build your nat rule command from the template using the values in the Hashtable
    $command = $natRule -f $ht.Index,
                           ($ht.'Enable NAT Policy').ToLower(),  # not sure if "True" needs to be lowercase here..
                           $ht.'Original Source', 
                           $ht.'Translated Source',
                           $ht.'Original Destination',
                           $ht.'Translated Destination',
                           $ht.'Original Service',
                           $ht.'Translated Service',
                           $ht.Comment

    # show the command we've built
    Write-Host "add nat-rule $command"

    # execute the command via API
    # Uncomment if you know what you're doing ;)
    # add nat-rule $command
}

屏幕上的結果命令如下所示:

add nat-rule position 1 enabled true original-source "Any" translated-source "ORIGINAL" original-destination "192.168.1.1" translated-destination: "ORIGINAL" original-service "IKE" translated-service: "ORIGINAL" comment "IKE NAT"

add nat-rule position 2 enabled true original-source "Source" translated-source "COPY" original-destination "192.168.1.2" translated-destination: "ORIGINAL" original-service "IKE&TINA" translated-service: "ORIGINAL" comment "IKE NOT"

PS 代碼-replace '\\\\', '\\\\'可能看起來很可笑,但由於-replace使用正則表達式,我們需要用另一個反斜杠來轉義第一部分中的反斜杠。 什么是將所有反斜杠加倍

我設法生成了以下腳本。 也許它有點不優雅或粗糙,但它可以完成工作。

感謝大家的幫助和提示。

$filepath = 'C:\test.txt'
$getfile = Get-Content $filepath -Raw
$splitfile = $getfile -split 'IP Version     : '

#Assignment of variables for regex part
$regex_index = 'Index     : '
$regex_original_source = 'Original Source     : '
$regex_translated_source = 'Translated Source     : '
$regex_original_destination = 'Original Destination     : '
$regex_translated_destination = 'Translated Destination : '
$regex_original_service = 'Original_Service     : '
$regex_translated_service = 'Translated_Service     : '
$regex_comment = 'Comment     : '

#Loop for each line of the text

foreach($object in $splitfile){
#Split the file for each line and analyze for each regex, if present, the output might be performed via CLI
$lines = $object.Split([Environment]::NewLine)

$data_index=@($lines) -match $regex_index
$value_index = $data_index -replace $regex_index, ''
#Write-Host $value_index

$data_original_source=@($lines) -match $regex_original_source
$value_original_source = $data_original_source -replace $regex_original_source, ''
#Write-Host $value_original_source

$data_translated_source=@($lines) -match $regex_translated_source
$value_translated_source = $data_translated_source -replace $regex_translated_source, ''
#Write-Host $value_translated_source

$data_original_destination=@($lines) -match $regex_original_destination
$value_original_destination = $data_original_destination -replace $regex_original_destination, ''
#Write-Host $value_original_destination

$data_translated_destination=@($lines) -match $regex_translated_destination
$value_translated_destination = $data_translated_destination -replace $regex_translated_destination, ''
#Write-Host $value_translated_destination

$data_original_service=@($lines) -match $regex_original_service
$value_original_service = $data_original_service -replace $regex_original_service, ''
#Write-Host $value_original_service

$data_translated_service=@($lines) -match $regex_translated_service
$value_translated_service = $data_translated_service -replace $regex_translated_service, ''
#Write-Host $value_translated_service

$data_comment=@($lines) -match $regex_comment
$value_comment = $data_comment -replace $regex_comment, ''
#Write-Host $value_comment

#Create string for variable assignment and print
$string = "add nat-rule position $value_index enabled true original-source `"$value_original_source`" translated-source `"$value_translated_source`" original-destination `"$value_original_destination`" translated-destination `"$value_translated_destination`" original-service `"$value_original_service`" translated-service `"$value_translated_service`" comments `"$value_comment`"

Write-Host $string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM