0
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で読む

Last updated at Posted at 2021-04-27

何をしたいか

PCの様子変ですね調べますよ
→チェックディスクかける
→イベントビューア開く
→対象コンピュータ名指定
→Applicationログ開く
→フィルタ wininit
→読む

めんどくさいので対象コンピュータ名指定したら結果読めるくらいにしたい。

どうしたらいいか

リモートレジストリサービスを有効にする

リモート先のイベントログをPowerShellで読む場合、相手側でリモートレジストリサービスRemoteRegistryが動いてないとエラーになり読めない。
Set-Serviceで設定を手動起動/実行中に変える。

Set-Service $ServiceName -Status Runningには同じ操作になる別のコマンドレットStart-Serviceがあるが、スタートアップの種類を変更するコマンドレットは他に無いようだ。

リモートレジストリを無効にする必要があればあとから戻すこと。Set-Service $ServiceName -Status Disabledで無効にできる。

リモートでサービス操作
# リモートレジストリサービス状態の取得
$Resp_ServiceStatus = Get-Service RemoteRegistry -ComputerName $ComputerName
$Resp_ServiceStatus.StartupType    #スタートアップの種類
$Resp_ServiceStatus.Status         #状態

# スタートアップの種類を 手動:Manualにする
Set-Service RemoteRegistry -ComputerName $ComputerName -StartupType Manual

# サービスの状態を 実行中:Runningにする
Set-Service RemoteRegistry -ComputerName $ComputerName -Status Running

リモート先のイベントログを読む

Get-Eventlogで読める。Newest 1を指定してやると最新のログから検索して1つだけ見つけてくれるので、チェックディスク後に探せばその最新のログが引っかかる。
ログが見つからない場合最古のログまで読むのでかなり時間をとられる。ログ300個読んで見つからなければ打ち切り、などとする必要があり若干工夫がいる。(具体例は省略)

見つからない場合はエラーになるのだが、見つからないのは見つからないという正常な結果であってエラーではないのでは?と思う…
止まらないエラーなのでcatchしたいときは-ErrorAction Stopをつける。

Get-Eventlog
$Param_GetEventlog = @{
  ComputerName = $ComputerName
  LogName = "Application"
  Source = "Wininit"
  Newest = "1"
}
try{
    (Get-EventLog @Param_GetEventlog -ErrorAction Stop).Message
}
catch{
    $_.CategoryInfo.Category
    $_.Exception
    Write-Host "chkdskログが見つかりませんでした。"
}

出来上がり

GetChkdskLog.ps1
Write-Host "対象コンピュータのイベントログからchkdsk結果を検索し表示します。"
$ComputerName = Read-Host "ComputerName"

# 応答チェック
$Resp_Ping = Test-Connection $ComputerName -Quiet
if($Resp_Ping -ne "True"){
    Write-Host "対象コンピュータから応答がありません。"
    Read-Host "キーを押すと終了します。[ENTER]"
    exit
}

# リモートレジストリサービスの操作
$Resp_ServiceStatus = Get-Service RemoteRegistry -ComputerName $ComputerName

if($Resp.StartType -eq "Disabled"){
    Set-Service RemoteRegistry -ComputerName $ComputerName -StartupType Manual
}

if($Resp_ServiceStatus.Status -ne "Running"){
    Set-Service RemoteRegistry -ComputerName $ComputerName -Status Running
}

# イベントログ表示
Write-Host "`r`nchkdskログ検索開始...`r`n"

$Param_GetEventlog = @{
  ComputerName = $ComputerName
  LogName = "Application"
  Source = "Wininit"
  Newest = "1"
}
try{
    (Get-EventLog @Param_GetEventlog -ErrorAction Stop).Message
}
catch{
    $_.CategoryInfo.Category
    $_.Exception
    Write-Host "chkdskログが見つかりませんでした。"
}

Read-Host "キーを押すと終了します。[ENTER]"
0
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
0
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?