powershell - Powershell 从日志文件中提取 TCP 端口以更新防火墙规则
问题描述
每次我们的 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 变量时会返回错误。
set-netfirewallrule : 端口无效。当协议为 TCP 或 UDP 时,允许单独的端口或范围。此外,防火墙规则中允许使用以下端口关键字:RPC、RPCEPMap、Teredo、IPHTTPSIn、IPHTTPSOut、PlayToDiscovery。在 U:\Scripts\UpdateIdeateFirewallRule.ps1:15 char:94
- ... 入站"} | set-netfirewallrule -Action Allow -Protocol TCP -LocalPor ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidArgument: (MSFT_NetFirewal...ystemName = ""):root/standardcimv2/MSFT_NetFirewallRule) [Set-NetFirewallRule], CimException
- fullyQualifiedErrorId : HRESULT 0x80070057,Set-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
解决方案
由于您追求的唯一值是端口号,因此代码可以简化为
$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)"
}
该模式将仅匹配行,ideate using TCP-port
并且该模式\d{1,5}
将匹配 1 到 5 位长的数字。
编辑
查看Set-NetFirewallrule
它的帮助显示本地端口是一个字符串数组。这可能就是为什么数字本身在您的文字字符串中不起作用的原因。我已经编辑了答案以强制数字为字符串。
推荐阅读
- c# - 为什么执行 exe 的 Control M OS Job 会在执行过程中卡住?
- python-3.x - Jupyter 与 Python 版本混合使用。内核使用 python 3.x 而不是 2.x
- apache-kafka - GenericJMXConfMBean:没有 MBean 匹配 ObjectName kafka.network:type=RequestMetrics,name=RequestsPerSec,request=Produce
- reactjs - 将文本字段 MUI 的占位符居中
- java - 无法让我的复制构造函数在我的简单代码中工作(我是 java 新手)
- r - 有没有办法使用 R 中的一个应用函数来优化这个循环?
- c - 字符串未显示在 C 的控制台中
- python - 简单的 Python 多线程网络服务器,带有 Asyncio 和在主函数中调用的事件
- java - 如何将最后两位小数打印成整数
- c# - Entity Framework Core 使用 CurrentValues.SetValues 从其他实体更新实体属性,由于实体上的主键而失败