0
0

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 1 year has passed since last update.

PowerShell で Local IP を hosts に記載してみた

Last updated at Posted at 2023-09-20

前提

  • Default NIC の "Loopback Pseudo-Interface 1" は除外する。
  • PowerShell は管理者モードで起動していることとする。

1. バージョン情報

1.1. Windows OS Version

PS C:\Users\Administrator> (Get-CimInstance -ClassName Win32_OperatingSystem).Version
10.0.19045
PS C:\Users\Administrator>

1.2. PowerShell Version

PS C:\Users\Administrator> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.19041.3031
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.3031
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
PS C:\Users\Administrator>

2. 事前確認

2.1. ホスト名の確認

PS C:\Users\Administrator> hostname
XXXXXXXXXX
PS C:\Users\Administrator>

2.2. hosts の内容確認

PS C:\Users\Administrator> Get-Content "C:\Windows\System32\drivers\etc\hosts"
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost
PS C:\Users\Administrator>

2.3. NIC 名の確認

PS C:\Users\Administrator> Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -ne "Loopback Pseudo-Interface 1"} | Select-Object -ExpandProperty InterfaceAlias
AAAAAAAAAA
BBBBBBBBBB
PS C:\Users\Administrator>

2.4. Local IP の確認

PS C:\Users\Administrator> Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -ne "Loopback Pseudo-Interface 1"} | Select-Object -ExpandProperty IPAddress
XXX.XXX.X.XX
YYY.YYY.Y.YY
PS C:\Users\Administrator>

3. Local IP を hosts に記載する

# IPv4アドレスを持つネットワークインターフェースを取得し、ループバックインターフェースを除外する。
$nicIPs = Get-NetIPAddress -AddressFamily IPv4 | Where-Object {$_.InterfaceAlias -ne "Loopback Pseudo-Interface 1"} | Select-Object -ExpandProperty IPAddress

# hostsファイルのパスを指定する。
$HostsPath = "C:\Windows\System32\drivers\etc\hosts"

# hostsファイルの内容を取得する。
$hostsContent = Get-Content $HostsPath

# 各NICのIPアドレスに対して処理を行う。
foreach ($nicIP in $nicIPs) {
  # 既に追加されているかどうかを示すフラグを初期化する。
  $isAlreadyAdded = $false

  # hostsファイルの各行に対して処理を行う。
  foreach ($line in $hostsContent) {
    # 現在の行がNICのIPアドレスを含むかどうかを確認する。
    if ($line -match [regex]::Escape($nicIP)) {
      # IPアドレスが既に追加されている場合、フラグを設定してループを抜ける。
      $isAlreadyAdded = $true
      break
    }
  }

  # IPアドレスがまだ追加されていない場合、hostsファイルに追加する。
  if (!$isAlreadyAdded) {
    Add-Content -Path $HostsPath -Value "$nicIP`t`t$(hostname)"
  }
}

4. 事後確認

4.1. hosts の内容確認

PS C:\Users\Administrator> Get-Content "C:\Windows\System32\drivers\etc\hosts"
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#       127.0.0.1       localhost
#       ::1             localhost
XXX.XXX.X.XX		XXXXXXXXXX
YYY.YYY.Y.YY		XXXXXXXXXX
PS C:\Users\Administrator>

4.2. 名前解決 (逆引き)

foreach ($nicIP in $nicIPs) {
  Resolve-DnsName -Type PTR -Name $nicIP
}

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
XX.X.XXX.XXX.in-addr.arpa.     PTR    1200  Question   XXXXXXXXXX
YY.Y.YYY.YYY.in-addr.arpa.     PTR    1200  Question   XXXXXXXXXX

4.3. 名前解決 (正引き)

PS C:\Users\Administrator> Resolve-DnsName -Type A -Name $(hostname)
Name                                         Type   TTL   Section    IPAddress
----                                         ----   ---   -------    ---------
XXXXXXXXXX                                   A      1200  Question   XXX.XXX.X.XX
XXXXXXXXXX                                   A      1200  Question   YYY.YYY.Y.YY
PS C:\Users\Administrator>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?