Skip to content

Instantly share code, notes, and snippets.

@anton-dudarev
Forked from AndrewPla/Get-PublicIPInfo.ps1
Created December 29, 2019 12:48
Show Gist options
  • Save anton-dudarev/e21ddf5869b83157ee27c68b2f8e77d1 to your computer and use it in GitHub Desktop.
Save anton-dudarev/e21ddf5869b83157ee27c68b2f8e77d1 to your computer and use it in GitHub Desktop.
Returns Public IP info from ip.nf Can be ran on remote computers
function Get-PublicIPInfo {
<#
.Description
Returns Public IP info from ip.nf
.Parameter IPAddress
Supply an IP address that you would like to lookup.
.Parameter ComputerName
Names of computers to run this command on.
.Parameter Credential
Credential used by Invoke-Command when performing the lookup on a remote machine.
.Example
PS> Get-PublicIPInfo
Returns your public IP info
.Example
PS> $computers = Import-Csv c:\computers.csv
PS> $computers | Get-PublicIpInfo -Credential $Cred
Grabs a list of computernames from csv.
Sends these computernames and invokes the lookup using the provided credential.
#>
[cmdletbinding(DefaultParameterSetName = '__AllParameterSets')]
param(
[Parameter(ParameterSetName = 'IP', Mandatory)]
$IPAddress,
[Parameter(ParameterSetName = 'Invoke', ValueFromPipeline, Mandatory)]
[string[]]
$ComputerName,
[Parameter(ParameterSetName = 'Invoke')]
[pscredential]
$Credential
)
process {
Switch ($PSCmdlet.ParameterSetName) {
'__AllParameterSets' {
(Invoke-RestMethod 'https://ip.nf/me.json').ip
}
'Invoke' {
Write-Verbose "Processing $($computername.count) computers" -verbose
foreach ($computer in $ComputerName) {
Write-Verbose "querying $computer" -verbose
$icmParams = @{
Computername = $computer
ScriptBlock = {
(Invoke-RestMethod 'https://ip.nf/me.json').ip }
}
if ($Credential) { $icmParams['Credential'] = $Credential }
Invoke-Command @icmParams
}
}
'IP' {
foreach ($Ip in $IPAddress) {
(Invoke-RestMethod "https://ip.nf/$IP.json").ip
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment