5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PowerShell】 サービス一覧を出力するスクリプト【忘備録】

Last updated at Posted at 2019-06-14

はじめに

Windows Serverなど構築する過程で、どうしても設計書などに サービスを出力する という工程が必要になる事があります。
このスクリプトはそのWindowsのServiceをCSVに一覧として出力するものになります。

通常のスクリプトだとトリガー開始や遅延開始などでないものありますが、こちらは詳細を出力することができるようにしています。

このPSスクリプトは記事の下部に記載された参考資料を基に組み込んだ。
コード系の開発は詳しく無いので、もっと良い方法があれば教えてください。

ソースコード

構築の都合でログオン名を取得する必要が出たため、取得できるように追記しました。
大きな変更はしてません。
※ID付きサービスは取れない…

ExportServiceList.PS1
#Script     : Windows Services List Export Scripts
#Creater    : 海里
#Version    : 2.1
#CreateData : 2019.06.14
#UpdateData : 2021.02.04
#Updated by : 海里

$triggers = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services" |
    Where-Object { $_.GetSubkeyNames().Contains("TriggerInfo") } |
    ForEach-Object { $_.Name.Split("\")[-1] }

$startMode = @{ Manual = "手動"; Disabled = "無効"; Auto = "自動"; Unknown = "不明" }
$startOption = @{ 01 = " (トリガー開始)"; 10 = " (遅延開始)"; 11 = " (遅延開始、トリガー開始)" }

$serviceData = Get-CimInstance -ClassName Win32_Service | Select-Object @(
    @{ n = "表示名";               e = { $_.DisplayName } }
    @{ n = "サービス名";           e = { $_.Name } }
    @{ n = "スタートアップの種類"; e = { $startMode[$_.StartMode] + $startOption[10 * ($_.StartMode -eq "Auto" -and $_.DelayedAutoStart) + $triggers.Contains($_.Name)] } }
    @{ n = "状態";                 e = { if($_.State -eq "Running") { "実行" } else { "停止" } } }
    @{ n = "ログオン";             e = { $_.StartName } }

)

$serviceData."表示名"
$serviceData | Export-Csv Outputlog.csv -NoTypeInformation -Encoding Default

"`nサービス一覧を記録しました。"

過去バージョン

Ver2.0

toruntaさんから提案頂いたもの。
Ver1よりも処理速度が速い。

ServiceCSVoutput(Ver2).ps1
#Script     : Windows Services List Export Scripts
#Creater    : 海里
#Version    : 2.0
#CreateData : 2019.06.14
#UpdateData : 2019.06.15
#Updated by : torunta

$triggers = Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services" |
    Where-Object { $_.GetSubkeyNames().Contains("TriggerInfo") } |
    ForEach-Object { $_.Name.Split("\")[-1] }

$startMode = @{ Manual = "手動"; Disabled = "無効"; Auto = "自動"; Unknown = "不明" }
$startOption = @{ 01 = " (トリガー開始)"; 10 = " (遅延開始)"; 11 = " (遅延開始、トリガー開始)" }

$serviceData = Get-CimInstance -ClassName Win32_Service | Select-Object @(
    @{ n = "表示名";              e = { $_.DisplayName } }
    @{ n = "サービス名";          e = { $_.Name } }
    @{ n = "スタートアップの種類"; e = { $startMode[$_.StartMode] + $startOption[10 * ($_.StartMode -eq "Auto" -and $_.DelayedAutoStart) + $triggers.Contains($_.Name)] } }
    @{ n = "状態";                e = { if($_.State -eq "Running") { "実行" } else { "停止" } } }
)

$serviceData."表示名"
$serviceData | Export-Csv Outputlog.csv -NoTypeInformation -Encoding Default

"`nサービス一覧を記録しました。"

Ver1.0

ServiceCSVoutput(Ver1).ps1

#Script     : Windows Services List Export Scripts
#Creater    : 海里
#Version    : 1.0
#CreateData : 2019.06.14


$OutputSVS= @()

Get-WmiObject -Class Win32_Service | Foreach{
    $ts = 0
    $ds = 0

    $Properties=@{
        "DisplayName" = $_.DisplayName
        "Name"        = $_.Name
        "StartMode"   = $_.StartMode
        "State"       = $_.State
    }
    switch($Properties.StartMode) {
        "Manual"   { $Properties.StartMode = "手動" }
        "Disabled" { $Properties.StartMode = "無効" }
        "Auto"     { $Properties.StartMode = "自動" }
    }
    switch($Properties.State) {
        "Running" {$Properties.State="実行"}
        "Stopped" {$Properties.State="停止"}
    }

    if (Test-Path -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$($Properties.Name)\TriggerInfo\") {
        $ts = 1
    }
    if ($Properties.StartMode -eq "Auto" -and $Properties.DelayedAutoStart -eq 1){ 
        $ds = 1
    }
    if ($ts -eq 1 -Or $ds -eq 1) {
        if ($ts -eq 1 -and $ds -eq 1) {
            $Properties.StartMode = "$($JS) (遅延開始、トリガー開始)"
        }
        Elseif ($ts -eq 1) {
            $Properties.StartMode = "$($JS) (トリガー開始)"
        }
        Elseif ($ds -eq 1) {
            $Properties.StartMode = "$($JS) (遅延開始)"
        }
    }

   		$OutputSVS += (New-Object -typename PSObject -property $Properties|Select @{name="表示名";Expression={$Properties.DisplayName}},`
		@{name="サービス名";Expression={$Properties.Name}},@{name="スタートアップの状態";Expression={$Properties.StartMode}},`
        @{name="状態";Expression={$Properties.State}})
		Write-Host $Properties.DisplayName 
}
$OutputSVS |Export-CSV Outputlog.csv  -NoTypeInformation -encoding Default
Write-Host "サービス一覧を記録しました。"

参考資料

Windowsの謎 ~サービス一覧を出すコマンド~

ファイアーウォールの送受信規則をCSVでエクスポートする方法

Powershell:サービスのログオンアカウントを取得、抽出

5
6
2

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
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?