5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コマンドラインから自分のグローバルIPアドレスを取得する for Windows PowerShell

Last updated at Posted at 2017-01-29

コマンドラインから自分のグローバルIPアドレスを取得する

のWindows PowerShell版を作ってみた。

IP Address Details - ipinfo.io というサイトほんと便利ですね。

(そして頭かくして尻隠さずなところはお約束)

PS C:\tmp> #オブジェクトを取得
PS C:\tmp> $infos = Invoke-RestMethod -Uri "ipinfo.io"
PS C:\tmp> #オブジェクトをそのまま表示
PS C:\tmp> $infos


ip       : **.***.***.**
hostname : hogebank************.bbtec.net
city     : HogeCity
region   : HogeCity
country  : JP
loc      : **.****,***.****
org      : AS***** Hogebank BB Corp.
postal   : ***-****



PS C:\tmp> #オブジェクトのメンバー表示。StringじゃないところがPowerShellっぽい
PS C:\tmp> $infos | gm


   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
city        NoteProperty string city=HogeCity
country     NoteProperty string country=JP
hostname    NoteProperty string hostname=hogebank************.bbtec.net
ip          NoteProperty string ip=**.***.***.**
loc         NoteProperty string loc=**.****,***.****
org         NoteProperty string org=AS***** Hogebank BB Corp.
postal      NoteProperty string postal=***-****
region      NoteProperty string region=HogeCity


PS C:\tmp> # 結局はこうすればグローバルIPが表示されます
PS C:\tmp> $infos.ip
**.***.***.**
PS C:\tmp> # さらに簡略系
PS C:\tmp> (Invoke-RestMethod -Uri "ipinfo.io").ip
**.***.***.**
PS C:\tmp>
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?