IF statement not functioning in Azure runbook

DW_FL 41 Reputation points
2024-07-09T14:06:43.48+00:00

I have a runbook with a PowerShell if statement to check if the input value of a received MAC address is in a valid format. If the format is not valid I am running a cmdlet which calls a function to update the MAC address and use the updated MAC.

Else if the MAC is in the correct format is uses it.

When I trigger the runbook from a Power Automate flow it fails and it seems like IF portion of the IF statement match is not working.

The ELSE portion is working since the runbook executes successfully if the MAC is in the correct format.

When I use the Test pane of the runbook it does complete successfully so I am not sure why I am getting different results based on how the runbook is triggered.

Param 
(
    [string] $Description,
    [string] $BSSID,
    [string] $LocationID
)

Import-Module MicrosoftTeams

Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")

#Set-CsOnlineLisWirelessAccessPoint -BSSID $BSSID -Description $Description -LocationId $LocationID -ErrorAction "Stop"

$BSSID -match '(\d{2}):(\d{2}):(\d{2}):(\d{2}):(\d{2}):(\d{2})'

$InvalidBSSID = $Matches.Item(0)

function Update-BSSID {
    $matches.item(0) -replace ':','-'
    }

$UpdatedBSSID = Update-BSSID
    

if ($BSSID -eq $InvalidBSSID ) {

    Set-CsOnlineLisWirelessAccessPoint -BSSID $UpdatedBSSID -LocationId $locationID -Description $Description -ErrorAction "Stop" -Verbose
    
     } 
     
     else {
             Set-CsOnlineLisWirelessAccessPoint -BSSID $BSSID -LocationId $locationID -Description $Description -ErrorAction "Stop" -verbose
             }

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,181 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,283 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,671 Reputation points
    2024-07-09T20:16:34.3866667+00:00

    This should handle both IPv4 and IPv6 MAC address formats (with either ":" or "-" separators). I removed all the unnecessary grouping punctuation from the regexes. I also used two regexes for the IPv6 check because I think it's clearer than one long one). You can probably shorten them (and the IPv4 one, too):

    Param(
        [string] $Description,
        [string] $BSSID,
        [string] $LocationID
    )
    
    # Uncomment these!!!
    #Import-Module MicrosoftTeams
    #Connect-MicrosoftTeams -AccessTokens @("$graphToken", "$teamsToken")
    
    $UpdatedBSSID = ""
    # Test for IPv4 MAC address
    if ($BSSID -match '([0-9A-F]{2})(:|-)([0-9A-F]{2})(:|-)([0-9A-F]{2})(:|-)([0-9A-F]{2})(:|-)([0-9A-F]{2})(:|-)([0-9A-F]{2})') {
        $UpdatedBSSID = $BSSID -replace ':', '-'
    }   # Test for IPv6 MAC address
    elseif ( $BSSID -match '[0-9A-F]{2}(:|-)[0-9A-F]{2}(:|-)[0-9A-F]{2}(:|-)[0-9A-F]{2}(:|-)[0-9A-F]{2}(:|-)[0-9A-F]{2}(:|-)[0-9A-F]{2}(:|-)[0-9A-F]{2}' -OR
        $BSSID -match '[0-9A-F]{4}(:|-)[0-9A-F]{4}(:|-)[0-9A-F]{4}(:|-)[0-9A-F]{4}' ) {
        $UpdatedBSSID = $BSSID -replace ':', '-'
    }
    else {
        # Not sure what you want to do if MAC isn't in expected format
        Throw "BSSIS is invalid : '$BSSID'"
    }
    try {
        # Uncomment this!!!
        # Set-CsOnlineLisWirelessAccessPoint -BSSID $UpdatedBSSID -LocationId $locationID -Description $Description -ErrorAction "Stop" -Verbose
    }
    catch {
        # your error handling for failed action gores here
    }
    

    I don't know what you want to do in any of your error handling, though!

    NOTE: I don't have any way to test with MS Teams so be sure to uncomment the cmdlets in the above code!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,671 Reputation points
    2024-07-09T15:49:24.6866667+00:00

    If the MAC address is in the variable $BSSID your regex is incorrect. A MAC address is composed of hexadecimal digits, not decimal digits.

    Also, an IPv6 MAC address is 64 bits long, not 48 bits long. You should be checking the length of $BSSID before you use it. And an IPv6 MAC can be presented in two different forms. Either eight groups of two hex digits, with each group separated by a colon, or four groups of four hex digits with each group separated by a colon.

    You should also be prepared to handle MAC addresses without separators. E.g., a string or either 12 hex digits, or 16 hex digits.

    I'm not at all familiar with Azure runbooks, but in "normal" PowerShell the $Matches object would be referenced as $matches[0]. And you would enclose the regex in an "if" statement to test if the -match operator was successful.