サーバーリプレースをおこなっており、旧サーバーと新サーバーで簡単に設定を比較したいなあと思ってちょっと考え、サーバーの各種設定を JSON で出力して比較することにしました。
対象 OS は Windows Server です。
サーバーの各種設定を JSON 形式で出力するスクリプト
ハッシュテーブルにサーバーの各種設定を格納して JSON 形式で出力します。
$scriptPath = Split-Path $MyInvocation.MyCommand.Path -Parent
$jsonPath = "$scriptPath\SvrSettings.json"
$hash = [ordered]@{
OSVersion = (Get-WmiObject Win32_OperatingSystem).Version
OSEdition = (Get-WindowsEdition -Online).Edition
HostName = hostname
TimeZone = (tzutil /g)
WindowsFirewall = (Get-NetFirewallProfile | select Name, @{Name="Enabled";Expression={[System.Convert]::ToBoolean($_.Enabled)}})
IIS = (Get-WindowsFeature -Name Web-Server | select Name, DisplayName, @{N="Installed";E={[System.Convert]::ToBoolean($_.Installed)}})
SSLServerCertificate = (Get-ChildItem Cert:\LocalMachine\My | where Subject -match "\*\.sample\.jp" | select Subject,Issuer,@{N="NotBefore";E={[Datetime]::Parse($_.NotBefore).ToString("yyyy/MM/dd HH:mm:ss")}},@{N="NotAfter";E={[Datetime]::Parse($_.NotAfter).ToString("yyyy/MM/dd HH:mm:ss")}})
ASPNETStateService = (Get-WmiObject Win32_Service -filter "Name='aspnet_state'" | select Name, StartMode, State, @{N="Started";E={[System.Convert]::ToBoolean($_.Started)}}, DisplayName)
MSE = Get-ChildItem -Path('HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall') | % { Get-ItemProperty $_.PsPath | where DisplayName -eq "Microsoft Security Essentials" | select DisplayName, DisplayVersion, Publisher, InstallDate }
Task = (Get-ScheduledTask | where taskname -like *sample* | select TaskName, @{N="Execute";E={$_.Actions|%{$_.Execute}}},State, @{N="DaysInterval";E={$_.Triggers|%{$_.DaysInterval}}}, @{N="StartBoundary";E={$_.Triggers|%{$_.StartBoundary}}})
App = [ordered]@{
Release = @{ Path="C:\release"; Exists = (Test-Path -Path "C:\release") }
Dev = @{ Path="C:\dev"; Exists = (Test-Path -Path "C:\dev") }
}
}
$hash | ConvertTo-Json >> $jsonPath
旧サーバーと新サーバーでスクリプトを叩いてJSONをWinMerge等で簡単に比較できます。
{
"OSVersion": "6.3.9600",
"OSEdition": "ServerStandard",
"HostName": "SAMPLE_SVR"
"TimeZone": "Tokyo Standard Time",
"WindowsFirewall": [
{
"Name": "Domain",
"Enabled": false
},
{
"Name": "Private",
"Enabled": false
},
{
"Name": "Public",
"Enabled": false
}
],
"IIS": {
"Name": "Web-Server",
"DisplayName": "Web サーバー (IIS)",
"Installed": true
},
"SSLServerCertificate": {
"Subject": "CN=*.sample.jp",
"Issuer": "CN=SAMPLESSL SHA256 CA, O=SAMPLE Inc., C=US",
"NotBefore": "2017/01/01 00:00:00",
"NotAfter": "2020/02/01 00:00:00"
},
"ASPNETStateService": {
"Name": "aspnet_state",
"StartMode": "Auto",
"State": "Running",
"Started": true,
"DisplayName": "ASP.NET State Service"
},
"MSE": {
"DisplayName": "Microsoft Security Essentials",
"DisplayVersion": "4.10.209.0",
"Publisher": "Microsoft Corporation",
"InstallDate": "20190507"
},
"Task": [
{
"TaskName": "Sample_01",
"Execute": "C:\Sample\Bat\Sample_01.bat",
"State": 1,
"DaysInterval": 1,
"StartBoundary": "2018-01-01T01:00:00"
},
{
"TaskName": "Sample_02",
"Execute": "C:\Sample\Bat\Sample_02.bat",
"State": 1,
"DaysInterval": 1,
"StartBoundary": "2018-01-01T01:00:00"
}
],
"App": {
"Release": {
"Exists": true,
"Path": "C:\\release"
},
"Dev": {
"Exists": true,
"Path": "C:\\dev"
}
}
}
各種設定を取得するコマンドたち
Windows Server の OS バージョン
> (Get-WmiObject Win32_OperatingSystem).Version
6.3.9600
# Windows Server 2012 R2
OS | バージョン | ビルド番号 |
---|---|---|
Windows Server 2016 | 10.0 | 14393 |
Windows Server 2012 R2 | 6.3 | 9600 |
Windows Server 2012 | 6.2 | 9200 |
Windows Server 2008 R2 SP1 | 6.1 | 7601 |
Windows Server 2008 R2 | 6.1 | 7600 |
Windows Server 2008 SP2 | 6.0 | 6002 |
Windows Server 2008 SP1 | 6.0 | 6001 |
Windows Server の OS エディション
> (Get-WindowsEdition -Online).Edition
ServerStandard
ホスト名
> hostname
SAMPLE_SVR
タイムゾーン
> tzutil /g
Tokyo Standard Time
FireWall
> Get-NetFirewallProfile
Name : Domain
Enabled : False
...
Name : Private
Enabled : False
...
Name : Public
Enabled : False
...
IIS
> Get-WindowsFeature -Name Web-*
Display Name Name Install State
------------ ---- -------------
[x] Web サーバー (IIS) Web-Server Installed
[x] Web サーバー Web-WebServer Installed
...
SSL サーバー証明書
> Get-ChildItem Cert:\LocalMachine\My | where Subject -match "\*\.sample\.jp" | Format-List
Subject :CN=*.sample.jp
...
ASP.NET 状態サービス
> Get-WmiObject Win32_Service -filter "Name='aspnet_state'"
ExitCode : 0
Name : aspnet_state
ProcessId : 1144
StartMode : Auto
State : Runnning
Status : OK
アプリケーションがインストール済みか
> Get-ChildItem -Path('HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall') | % { Get-ItemProperty $_.PsPath | where DisplayName -eq "Microsoft Security Essentials" }
DisplayName : Microsoft Security Essentials
InstallDate : 20190507
...
# Microsoft Security Essentialsがインストール済みか確認する例
スケジュールタスク
> Get-ScheduledTask | where taskname -like *sample* | Format-Table
TaskPath TaskName State
-------- -------- -----
\ Sample_01 Enabled
\ Sample_02 Enabled
...
ファイル存在チェック
> Test-Path -Path C:\sample
True