Powershell Script auto enter -Port number

  • 8 months ago
Comment

ChatGPT
1 / 1

If you need to automatically enter a port number in a PowerShell script, you can use the Read-Host cmdlet to prompt the user for input. Here's an example of how you can do this:

powershell
$port = Read-Host "Enter a port number"

# Validate if the input is a valid port number (optional)
if ($port -match '^\d+$' -and [int]$port -ge 1 -and [int]$port -le 65535) {
Write-Host "You entered port $port"
# Your further processing with the port number goes here
} else {
Write-Host "Invalid port number. Please enter a valid port number."
}

In this example, the user will be prompted to enter a port number, and the script will validate if the input is a valid port number within the valid range (1-65535).

Please note that if you want to automate this process without user input, you would need to provide a specific port number directly in the script, instead of using Read-Host.