Powershell从日志文件提取TCP端口以更新防火墙规则

Powershell从日志文件提取TCP端口以更新防火墙规则,powershell,firewall,Powershell,Firewall,每次FlexLm服务器重新启动时,授权服务都会重置其输入TCP端口。我想从日志文件中提取新的TCP端口以更新防火墙规则。我可以提取端口号,但脚本不喜欢单引号分隔符 $IdeateLog = "C:\Autodesk\Network License Manager\ideate.log" $LogTxt= "*(lmgrd) ideate using TCP-port*" $content = Get-Content $IdeateLog $content

每次FlexLm服务器重新启动时,授权服务都会重置其输入TCP端口。我想从日志文件中提取新的TCP端口以更新防火墙规则。我可以提取端口号,但脚本不喜欢单引号分隔符

$IdeateLog = "C:\Autodesk\Network License Manager\ideate.log"
$LogTxt= "*(lmgrd) ideate using TCP-port*"

$content = Get-Content $IdeateLog
$content | foreach {
        if ($_ -ilike $LogTxt){
        $IdeatePortTxt = $_ -replace "ideate using TCP-port ",""
        $IdeatePortTxtLen = $IdeatePortTxt.Length - 17
        $IdeatePortNo = "'"+ $IdeatePortTxt.Substring(17, $IdeatePortTxtLen) + "'"
        }
      }
$tempPort = '65443'

get-netfirewallrule -DisplayName "*LMtools - Ideate*" | where {$_.Direction -eq "Inbound"} | set-netfirewallrule -Action Allow -Protocol TCP -LocalPort @($IdeatePortNo)
如果我使用$tempPort变量,防火墙规则将更新,但在使用$IdeatePortNo变量时返回错误

设置netfirewallrule:端口无效。当协议为TCP或UDP时,允许使用单个端口或范围。此外,防火墙规则允许使用以下端口关键字:RPC, RPCEPMap、Teredo、IPHTTPSIn、IPHTTPSOut、PlayToDiscovery。 在U:\Scripts\UpdateIdeateFirewallRule.ps1:15 char:94

  • 。。。入站“}设置netfirewallrule-Action Allow-Protocol TCP-LocalPor
    • CategoryInfo:InvalidArgument:(MSFT_NetFirewall…ystemName=“”):root/standardcimv2/MSFT_NetFirewallRule)[设置NetFirewallRule],CimException
    • FullyQualifiedErrorId:HRESULT 0x80070057,设置NetFirewallRule
日志文件摘录

7:21:56 (lmgrd) -----------------------------------------------
 7:21:56 (lmgrd)   Please Note:
 7:21:56 (lmgrd) 
 7:21:56 (lmgrd)   This log is intended for debug purposes only.
 7:21:56 (lmgrd)   In order to capture accurate license
 7:21:56 (lmgrd)   usage data into an organized repository,
 7:21:56 (lmgrd)   please enable report logging. Use Flexera's
 7:21:56 (lmgrd)   software license administration  solution,
 7:21:56 (lmgrd)   FlexNet Manager, to  readily gain visibility
 7:21:56 (lmgrd)   into license usage data and to create
 7:21:56 (lmgrd)   insightful reports on critical information like
 7:21:56 (lmgrd)   license availability and usage. FlexNet Manager
 7:21:56 (lmgrd)   can be fully automated to run these reports on
 7:21:56 (lmgrd)   schedule and can be used to track license
 7:21:56 (lmgrd)   servers and usage across a heterogeneous
 7:21:56 (lmgrd)   network of servers including Windows NT, Linux
 7:21:56 (lmgrd)   and UNIX.
 7:21:56 (lmgrd) 
 7:21:56 (lmgrd) -----------------------------------------------
 7:21:56 (lmgrd) 
 7:21:56 (lmgrd) 
 7:21:56 (lmgrd) Server's System Date and Time: Mon Nov 16 2020 07:21:56 Pacific Standard Time
 7:21:56 (lmgrd) pid 6432
 7:21:56 (lmgrd) SLOG: Summary LOG statistics is enabled.
 7:21:56 (lmgrd) Detecting other license server manager (lmgrd) processes...
 7:21:59 (lmgrd) Done rereading
 7:21:59 (lmgrd) FlexNet Licensing (v11.16.2.0 build 242433 x64_n6) started on PANIC (IBM PC) (11/16/2020)
 7:21:59 (lmgrd) Copyright (c) 1988-2018 Flexera. All Rights Reserved.
 7:21:59 (lmgrd) World Wide Web:  http://www.flexerasoftware.com
 7:21:59 (lmgrd) License file(s): C:\Licenses\Ideate\Rushing_07-16_PANIC.lic
 7:21:59 (lmgrd) lmgrd tcp-port #####
 7:21:59 (lmgrd) (@lmgrd-SLOG@) ===============================================
 7:21:59 (lmgrd) (@lmgrd-SLOG@) === LMGRD ===
 7:21:59 (lmgrd) (@lmgrd-SLOG@) Start-Date: Mon Nov 16 2020 07:21:59 Pacific Standard Time
 7:21:59 (lmgrd) (@lmgrd-SLOG@) PID: 6432
 7:21:59 (lmgrd) (@lmgrd-SLOG@) LMGRD Version: v11.16.2.0 build 242433 x64_n6 ( build 242433 (ipv6))
 7:21:59 (lmgrd) (@lmgrd-SLOG@) 
 7:21:59 (lmgrd) (@lmgrd-SLOG@) === Network Info ===
 7:21:59 (lmgrd) (@lmgrd-SLOG@) Listening port: #####
 7:21:59 (lmgrd) (@lmgrd-SLOG@) 
 7:21:59 (lmgrd) (@lmgrd-SLOG@) === Startup Info ===
 7:21:59 (lmgrd) (@lmgrd-SLOG@) Is LS run as a service: Yes
 7:21:59 (lmgrd) (@lmgrd-SLOG@) Server Configuration: Single Server
 7:21:59 (lmgrd) (@lmgrd-SLOG@) Command-line options used at LS startup: -c C:\Licenses\Ideate\Rushing_07-16_#####.lic -l C:\Autodesk\Network License Manager\ideate.log -z -s 
 7:21:59 (lmgrd) (@lmgrd-SLOG@) License file(s) used:  C:\Licenses\Ideate\Rushing_07-16_#####.lic
 7:21:59 (lmgrd) (@lmgrd-SLOG@) ===============================================
 7:21:59 (lmgrd) SLOG: FNPLS-INTERNAL-VL1-4096
 7:21:59 (lmgrd) Starting vendor daemons ... 
 7:21:59 (lmgrd) Started ideate (pid 7552)
 7:21:59 (ideate) FLEXnet Licensing version v11.9.1.0 build 89952 x64_n6
 7:21:59 (ideate) lmgrd version 11.16, ideate version 11.9

 7:21:59 (ideate) Server started on ##### for:  
 7:21:59 (ideate) IBL (consisting of:       BIMLink_2012    
 7:21:59 (ideate) BIMLink_2013  BIMLink_2014    BIMLink_2015    
 7:21:59 (ideate) BIMLink_2016  BIMLink_2017    BIMLink_2018    
 7:21:59 (ideate) BIMLink_2019  BIMLink_2020    BIMLink_2021    
 7:21:59 (ideate) BIMLink_2022) 
 7:21:59 (ideate) IEX (consisting of:       Explorer_2012   
 7:21:59 (ideate) Explorer_2013 Explorer_2014   Explorer_2015   
 7:21:59 (ideate) Explorer_2016 Explorer_2017   Explorer_2018   
 7:21:59 (ideate) Explorer_2019 Explorer_2020   Explorer_2021   
 7:21:59 (ideate) Explorer_2022)    
 7:21:59 (ideate) ISB (consisting of:       Sticky_2014 
 7:21:59 (ideate) Sticky_2015   Sticky_2016 Sticky_2017 
 7:21:59 (ideate) Sticky_2018   Sticky_2019 Sticky_2020 
 7:21:59 (ideate) Sticky_2021   Sticky_2022)    
 7:21:59 (ideate) Apps (consisting of:      IdeateApps_2015 
 7:21:59 (ideate) IdeateApps_2016 IdeateApps_2017 IdeateApps_2018 
 7:21:59 (ideate) IdeateApps_2019 IdeateApps_2020 IdeateApps_2021 
 7:21:59 (ideate) IdeateApps_2022) 
 7:21:59 (ideate) STL (consisting of:       StyleManager_2016 
 7:21:59 (ideate) StyleManager_2017 StyleManager_2018 StyleManager_2019 
 7:21:59 (ideate) StyleManager_2020 StyleManager_2021 StyleManager_2022) 
 7:21:59 (ideate) EXTERNAL FILTERS are OFF
 7:21:59 (lmgrd) ideate using TCP-port 65443
 8:01:40 (ideate) TCP_NODELAY NOT enabled

这是我在尝试“$($matches.1)”时出现的错误。

由于您所追求的唯一值是端口号,因此代码可以简化为

$IdeateLog = "C:\Autodesk\Network License Manager\ideate.log"

$content = Get-Content $IdeateLog

if($content -match 'ideate using TCP-port (\d{1,5})')
{
    get-netfirewallrule -DisplayName "*LMtools - Ideate*" | where {$_.Direction -eq "Inbound"} |
        set-netfirewallrule -Action Allow -Protocol TCP -LocalPort "$($matches.1)"
}
模式将仅与使用TCP端口的
ideate匹配,模式
\d{1,5}
将匹配长度为1到5位的数字

编辑


查看
设置NetFirewallrule
的帮助,它将本地端口显示为字符串数组。这可能就是为什么数字本身不能像文本字符串那样工作的原因。我编辑了答案,以强制数字为字符串。

由于您追求的唯一值是端口号,因此代码可以简化为

$IdeateLog = "C:\Autodesk\Network License Manager\ideate.log"

$content = Get-Content $IdeateLog

if($content -match 'ideate using TCP-port (\d{1,5})')
{
    get-netfirewallrule -DisplayName "*LMtools - Ideate*" | where {$_.Direction -eq "Inbound"} |
        set-netfirewallrule -Action Allow -Protocol TCP -LocalPort "$($matches.1)"
}
模式将仅与使用TCP端口的
ideate匹配,模式
\d{1,5}
将匹配长度为1到5位的数字

编辑


查看有关
设置NetFirewallrule
的帮助,它显示本地端口为字符串数组。这可能是数字本身无法与文字字符串一起工作的原因。我已编辑了答案,以强制数字为字符串。

您应该显示日志文件的示例,您肯定会得到更好的建议关于解决您的问题。几乎可以肯定有一种比替换+子字符串+串联更简单的方法。感谢您提供的提示。这里是日志文件的摘录:您应该展示日志文件的外观示例,并且您肯定会得到关于解决您的问题的更好建议。几乎可以肯定有一种比替换+子字符串+串联更简单的方法加上子串加上连接。谢谢你的提示。这里是一个日志文件的摘录:谢谢你,道格!不客气!如果你发现答案很有用,请考虑一下它的投票。如果它解决了你的问题,考虑把它标记为接受的答案。如果你能提供更好的答案,你可以在以后改变它。嗨,道格,1美元。未在防火墙规则中更新变量。如果我将其替换为数字9999或静态变量$tempPort=8888,两者都将更新。如果将其更改为“$($MATCHS.1)”,它仍然不能与$($MATCHS.1)一起使用,该怎么办谢谢你的建议。谢谢你,道格!不客气!如果你发现答案是有用的,请考虑它的投票。如果它解决了你的问题,考虑将它标记为可接受的答案。如果你能提供更好的答案,你可以随时改变它。嗨,道格,$匹配。1变量没有被更新在防火墙规则中。如果我替换它WI。数字9999或静态变量$tempPort=8888都将更新。如果将其更改为“$($matches.1)”,它仍然不能与$($matches.1)一起使用。感谢您的建议。