Powershell extract TCP port from log file to update Firewall rule

0

Every time our FlexLm server reboots, the licensing services reset their input TCP port. I want to extract the new TCP port from the log file to update the firewall rule. I can extract the port number, but the script does not like the single quote delimiter.

$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)

The firewall rule updates if I use the $tempPort variable, but returns the error when I use the $IdeatePortNo variable.

set-netfirewallrule : The port is invalid. When Protocol is TCP or UDP, individual ports or ranges are allowed. Also, the following port keywords are allowed on Firewall Rules: RPC, RPCEPMap, Teredo, IPHTTPSIn, IPHTTPSOut, PlayToDiscovery. At U:\Scripts\UpdateIdeateFirewallRule.ps1:15 char:94

  • ... Inbound"} | set-netfirewallrule -Action Allow -Protocol TCP -LocalPor ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (MSFT_NetFirewal...ystemName = ""):root/standardcimv2/MSFT_NetFirewallRule) [Set-NetFirewallRule], CimException
    • FullyQualifiedErrorId : HRESULT 0x80070057,Set-NetFirewallRule

excerpt of log file

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

Here is my error when I try "$($matches.1)" enter image description here

powershell
firewall
asked on Stack Overflow Nov 18, 2020 by markus1998 • edited Nov 19, 2020 by markus1998

1 Answer

2

Since the only value you're after is a port number, the code can be simplified to

$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)"
}

The pattern will only match the line with ideate using TCP-port and the pattern \d{1,5} will match and number that is 1 to 5 digits long.

Edit

Looking at the help for Set-NetFirewallrule it shows local port to be a string array. That's likely why the number by itself isn't working where your literal string does. I've edited the answer to force the number to be a string.

answered on Stack Overflow Nov 18, 2020 by Doug Maurer • edited Nov 18, 2020 by Doug Maurer

User contributions licensed under CC BY-SA 3.0