Skip to content

Instantly share code, notes, and snippets.

@AndrewPla
Last active January 12, 2020 10:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AndrewPla/3bf4cb8b20eeeac19f3d4febdee0a380 to your computer and use it in GitHub Desktop.
Save AndrewPla/3bf4cb8b20eeeac19f3d4febdee0a380 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