たぶん、 用語を正しく使えていないからだと思うのですが、 こんな簡単なことをどれだけ調べても出てこなかったのでメモ。
TL;DR
toggleRTProtection.ps1
$status = Get-MpComputerStatus
$rtp_enabled = $status.RealTimeProtectionEnabled
if ($rtp_enabled) {
Set-MpPreference -DisableRealtimeMonitoring $True
} else {
Set-MpPreference -DisableRealtimeMonitoring $False
}
背景
- WSLを使うとき、 Windows Defenderのリアルタイム保護のせいでパフォーマンスが激落ち1するので、 我慢できなくて外したくなる
- ポチポチクリックするのが面倒になったので、 スクリプト一発でトグルしたい
- きっと PowerShell でできるだろう → できそう
- PowerShell使い方わからなすぎる
環境
- Windows 10 Pro
> echo $PSVersionTable
Name Value
---- -----
PSVersion 5.1.16299.251
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.16299.251
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
調査
現在の状態の取得
Get-MpComputerStatus でできるらしいです。
> Get-MpComputerStatus
AMEngineVersion : 1.1.14600.4
AMProductVersion : 4.12.17007.18022
AMServiceEnabled : True
AMServiceVersion : 4.12.17007.18022
AntispywareEnabled : True
AntispywareSignatureAge : 0
AntispywareSignatureLastUpdated : 2018/04/02 7:14:37
AntispywareSignatureVersion : 1.263.1915.0
AntivirusEnabled : True
AntivirusSignatureAge : 0
AntivirusSignatureLastUpdated : 2018/04/02 7:14:39
AntivirusSignatureVersion : 1.263.1915.0
BehaviorMonitorEnabled : True
ComputerID : ****
ComputerState : 0
FullScanAge : 4294967295
FullScanEndTime :
FullScanStartTime :
IoavProtectionEnabled : True
LastFullScanSource : 0
LastQuickScanSource : 2
NISEnabled : True
NISEngineVersion : 2.1.14600.4
NISSignatureAge : 0
NISSignatureLastUpdated : 2018/04/02 16:01:05
NISSignatureVersion : 119.0.0.0
OnAccessProtectionEnabled : True
QuickScanAge : 0
QuickScanEndTime : 2018/04/02 13:45:07
QuickScanStartTime : 2018/04/02 13:32:07
RealTimeProtectionEnabled : True
RealTimeScanDirection : 0
PSComputerName :
RealTimeProtectionEnabled というのがそれっぽいです。 マウスでクリックポチポチすればこれがTrueとFalseを行き来するので間違いないはず。
欲しい情報にフィルタ
PSってコマンドラインででてくるデータも全部オブジェクト?なんでしょ!知ってる知ってる!
こうすれば欲しい情報が得られそうです。
> Get-MpComputerStatus | Select RealTimeProtectionEnabled
RealTimeProtectionEnabled
-------------------------
True
もうTrueだけ出てきたし終わったも同然な気がする
真偽値を取得
終わったも同然と思ったのになぜかここでしばらくつまっていました。 色々試したのですが結局これで良かった。
# 変数に入れる (入れずにやる方法がわかっていない)
> $a = Get-MpComputerStatus | Select RealTimeProtectionEnabled
# 出力 (この方法なら別にSelectする必要もない)
> $a.RealTimeProtectionEnabled
True
スクリプト作成
最終的に作ったスクリプトはこちら。
toggleRTProtection.ps1
$status = Get-MpComputerStatus
$rtp_enabled = $status.RealTimeProtectionEnabled
if ($rtp_enabled) {
Set-MpPreference -DisableRealtimeMonitoring $True
} else {
Set-MpPreference -DisableRealtimeMonitoring $False
}
ノートン先生とかにしようかな...
-
gitのソースコードを
./configure
するときに2.53s user 12.97s system 95% cpu 16.179 total
vs3.78s user 20.50s system 33% cpu 1:11.68 total
くらい。 ↩