1
1

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 3 years have passed since last update.

【PowerShell】複数ホストにnslookup確認できるスクリプト

Posted at

はじめに

複数ホストに対してnslookup確認できるスクリプトをアウトプットします。
(実際に現場にて使用しています。)
※本記事は、「PowerShell ISE」を使用できる人向けの記事になります。

PowerShellスクリプト実行環境

  • OS
    WindowsServer2019

  • PowerShellバージョン

 PS C:\Users\Administrator> $PSVersionTable

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

スクリプトでできること

  • 複数ホストに対してnslookupコマンドを実行できる
  • nslookupの確認結果を実行結果として表示できる
  • コマンド実行結果をログとして出力することができる

コード

# 複数ホストにnslookupして結果を返すスクリプト

# ①変数/配列宣言
$count = 0
$machines = @()
$results = @()

# ②ファイル指定
$csvpath =  "<<csvのファイルパス>>"
$date = (Get-Date).ToString("yyyyMMdd-HHmmss")

# ③ログ取得開始
Start-Transcript  <<フォルダパス>>\Log_$date.log -append
# ログ保存先/ファイル名を指定し、ログ取得開始(ログ名は「Log_20200704-115959.log」のようになります。)

# ④構成ファイル読み込み
$configs = Import-Csv  $csvpath

# ⑤nslookup確認
foreach ($config in $configs)
{
  $server = $config.Server
  $machines += $server
  nslookup $server
  $results += echo $?

}

# ⑥結果出力処理
foreach ($result in $results)
{
   if( $result -eq "True"){
    Write-Host $machines[$count] ":True"
  }else{
    Write-Host $machines[$count] ":False" -ForegroundColor Red
  }
  $count++
}


# ⑦ログ出力停止
Stop-Transcript

使い方

①スクリプト実行に使用するcsvを用意する。

名前はserver.csvとします。

server.csv
server
確認したいホスト
確認したいホスト
確認したいホスト
確認したいホスト
確認したいホスト
~
~
~

作成例

server.csv
8.8.8.8
8.8.4.4
10.10.10.10

作成後、任意の場所に保存しておく。 (パスは控えておく)

※今回は下記ディレクトリへ保存とします。
C:\Users\Administrator\Desktop\scripts\dns_check

②ログ保存フォルダを用意する。

今回は下記フォルダとします。

C:\Users\Administrator\Desktop\scripts\log

③スクリプトへファイルパスの指定

下記のように指定する

# ②ファイル指定
$csvpath =  "<<csvのファイルパス>>" ←csvのファイルパスを指定
$date = (Get-Date).ToString("yyyyMMdd-HHmmss")

# ③ログ取得開始
Start-Transcript  <<フォルダパス>>\Log_$date.log -append ← <<フォルダパス>>にログ取得先フォルダを指定
# ログ保存先/ファイル名を指定し、ログ取得開始(ログ名は「Log_20200704-115959.log」のようになります。)

# ②ファイル指定
$csvpath =  "C:\Users\Administrator\Desktop\scripts\dns_check\server.csv" 
$date = (Get-Date).ToString("yyyyMMdd-HHmmss")

# ③ログ取得開始
Start-Transcript  C:\Users\Administrator\Desktop\scripts\log\Log_$date.log -append
# ログ保存先/ファイル名を指定し、ログ取得開始(ログ名は「Log_20200704-115959.log」のようになります。)

④スクリプト実行

実行結果は下記のようになる。
(8.8.8.88.8.4.4は見つかるが、10.10.10.10が見つからない。)

実行結果
PS C:\Users\Administrator> C:\Users\Administrator\Desktop\scripts\dns_check\nslookup.ps1
トランスクリプトが開始されました。出力ファイル: C:\Users\Administrator\Desktop\scripts\log\Log_20200704-150351.log
サーバー:  u2.ucloudlink.com
Address:  192.168.43.1

名前:    dns.google
Address:  8.8.8.8

サーバー:  u2.ucloudlink.com
Address:  192.168.43.1

名前:    dns.google
Address:  8.8.4.4

nslookup : *** u2.ucloudlink.com  10.10.10.10 を見つけられません: Non-existent domain
発生場所 C:\Users\Administrator\Desktop\scripts\dns_check\nslookup.ps1:21 文字:3
+   nslookup $server
+   ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (*** u2.ucloudli...existent domain:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
サーバー:  u2.ucloudlink.com
Address:  192.168.43.1

8.8.8.8 :True
8.8.4.4 :True
10.10.10.10 :False
トランスクリプトが停止されました。出力ファイル: C:\Users\Administrator\Desktop\scripts\log\Log_20200704-150351.log

PS C:\Users\Administrator> 

⑤ログ確認

ログも下記内容で自動保存される。

Log_20200704-150351.log
コンピューター: TSPAD01 (Microsoft Windows NT 10.0.17763.0)
ホスト アプリケーション: C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe
プロセス ID: 348
PSVersion: 5.1.17763.1007
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.1007
BuildVersion: 10.0.17763.1007
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
トランスクリプトが開始されました。出力ファイル: C:\Users\Administrator\Desktop\scripts\log\Log_20200704-150351.log
サーバー:  u2.ucloudlink.com
Address:  192.168.43.1

名前:    dns.google
Address:  8.8.8.8

サーバー:  u2.ucloudlink.com
Address:  192.168.43.1

名前:    dns.google
Address:  8.8.4.4

nslookup : *** u2.ucloudlink.com  10.10.10.10 を見つけられません: Non-existent domain
発生場所 C:\Users\Administrator\Desktop\scripts\dns_check\nslookup.ps1:21 文字:3
+   nslookup $server
+   ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (*** u2.ucloudli...existent domain:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
nslookup : *** u2.ucloudlink.com  10.10.10.10 を見つけられません: Non-existent domain
発生場所 C:\Users\Administrator\Desktop\scripts\dns_check\nslookup.ps1:21 文字:3
+   nslookup $server
+   ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (*** u2.ucloudli...existent domain:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError




サーバー:  u2.ucloudlink.com
Address:  192.168.43.1

8.8.8.8 :True
8.8.4.4 :True
10.10.10.10 :False
**********************
Windows PowerShell トランスクリプト終了
終了時刻: 20200704150351
**********************
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?