Functions/GenXdev.Webbrowser/Get-ChromeRemoteDebuggingPort.ps1
<##############################################################################
Part of PowerShell module : GenXdev.Webbrowser Original cmdlet filename : Get-ChromeRemoteDebuggingPort.ps1 Original author : René Vaessen / GenXdev Version : 1.264.2025 ################################################################################ MIT License Copyright 2021-2025 GenXdev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ################################################################################> ############################################################################### <# .SYNOPSIS Returns the configured remote debugging port for Google Chrome. .DESCRIPTION Retrieves and manages the remote debugging port configuration for Google Chrome. The function first checks for a custom port number stored in $Global:ChromeDebugPort. If not found or invalid, it defaults to port 9222. The port number is then stored globally for use by other Chrome automation functions. .OUTPUTS System.Int32 Returns the configured Chrome debugging port number. .EXAMPLE $port = Get-ChromeRemoteDebuggingPort Write-Host "Chrome debug port: $port" .EXAMPLE $port = Get-ChromePort Write-Host "Chrome debug port: $port" #> function Get-ChromeRemoteDebuggingPort { [CmdletBinding()] [OutputType([System.Int32])] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')] param() begin { # initialize the default chrome debugging port [int] $port = 9222 } process { # check if a custom port is configured in the global scope if ($Global:ChromeDebugPort) { # attempt to parse the global port value if ([int]::TryParse($Global:ChromeDebugPort, [ref] $port)) { Microsoft.PowerShell.Utility\Write-Verbose ` -Message "Using configured Chrome debug port: $port" } else { Microsoft.PowerShell.Utility\Write-Verbose ` -Message "Invalid port config, using default port: $port" } } else { Microsoft.PowerShell.Utility\Write-Verbose ` -Message "No custom port configured, using default port: $port" } # ensure the port is available in global scope $null = Microsoft.PowerShell.Utility\Set-Variable ` -Name ChromeDebugPort ` -Value $port ` -Scope Global } end { return $port } } |