I created a function with some inspiration from here.
It's working as expected so far but only when an Option Switch is provided when calling the function. What I want to do is just be able type "Get-ExternalIP" without providing a switch. The function should then automatically use the -All switch. Also I'm not sure why the -All switch even works...
I've tried to set the Parameter $All = $true it's not working and VSCode tells me that it's not recommended anyways.
Is there a way to automatically pass along the -All Option Switch when the function is called without any parameters? Would somebody be able to explain why the function returns all info if the -All switch is specified?
Thanks!
Here's my code:
function Get-ExternalIP {
[CmdletBinding()]
param (
[parameter(Mandatory = $false)][switch]$Ip,
[parameter(Mandatory = $false)][switch]$HostName,
[parameter(Mandatory = $false)][switch]$City,
[parameter(Mandatory = $false)][switch]$Region,
[parameter(Mandatory = $false)][switch]$Country,
[parameter(Mandatory = $false)][switch]$Location,
[parameter(Mandatory = $false)][switch]$Provider,
[parameter(Mandatory = $false)][switch]$PostalCode,
[parameter(Mandatory = $false)][switch]$TimeZone,
[parameter(Mandatory = $false)][switch]$All
)
$IpInfo = Invoke-RestMethod https://ipinfo.io/json
switch ($PSBoundParameters.Values) {
$Ip { $IpInfo.ip }
$HostName { $IpInfo.hostname }
$City { $IpInfo.city }
$Region { $IpInfo.region }
$Country { $IpInfo.country }
$Location { $IpInfo.loc }
$Provider { $IpInfo.org }
$PostalCode { $IpInfo.postal }
$TimeZone { $IpInfo.timezone }
Default { $IpInfo }
}
}
Get-ExternalIP