以下の内容はhttps://tech.guitarrapc.com/entry/2013/10/26/120027より取得しました。


PowerShell で インストールされているアンチウィルスソフトを取得する

PowerShellを用いたPC管理の需要が高いようですね。 今回は、PowerShellでのアンチウィルスソフト検出を見てみましょう。結構前からWMIでのやり方はあるのですが、注意点が余り説明されていないようなのでいい機会です。

取得方法

セキュリティセンターを利用します。

ご存知の通り、セキュリティセンターにはアンチウィルスソフトの情報が表示されます。

そのため、ここの情報を取得すれば欲しい情報は得られます。

対象バージョン

PowerShell バージョン

今回は、CIMインスタンスを利用しています。

そのため、対象PowerShellバージョンはPowerShell 3.0以上です。

OS

上記の取得方法は、CIM (WMI) のNameSpace SecurityCenter2を利用しています。 (XPの場合はSecurityCenterです)

対象OSは、非サーバー系OSのWindows 7以降となります。

対象のまとめ

対象OS 対象PowerShell
Windows 7以上 (非サーバー系OS) PowerShell 3.0以上

なぜサーバー系OSが対象にならないのか

理由は、CIM(WMI)にNameSpace SecurityCenter2を含んでいないためです。

Windows Server 2012の名前空間一覧を見てみましょう。

PS> (Get-CimInstance -namespace "root" -className "__Namespace").Name

subscription
DEFAULT
MicrosoftWmiNet
CIMV2
msdtc
Cli
nap
MicrosoftIISv2
SECURITY
RSOP
WebAdministration
StandardCimv2
WMI
Amazon
AccessLogging
directory
Policy
Interop
Hardware
ServiceModel
Microsoft
aspnet

ないですね。Windows 8.1の名前空間一覧を見てみましょう。

PS> (Get-CimInstance -namespace "root" -className "__Namespace").Name

subscription
DEFAULT
CIMV2
msdtc
Cli
nap
SECURITY
SecurityCenter2
RSOP
StandardCimv2
WMI
directory
Policy
virtualization
Interop
Hardware
ServiceModel
SecurityCenter
Microsoft
aspnet

あります。つまり取得できるわけです。

コードサンプル

GitHubに挙げておきます。

PowerShellUtil / Get-AntiSpyware / Get-AntiSpyware.ps1

#Requires -Version 3.0

function Get-AntiSpyware
{

    [CmdletBinding()]
    Param
    (
        # Input ComputerName you want to check
        [Parameter(Mandatory = 0,
                   ValueFromPipeline,
                   ValueFromPipelineByPropertyName,
                   Position=0)]
        [ValidateNotNullOrEmpty()]
        [string]
        $computerName = [System.Environment]::MachineName,

        # Input PSCredential for $ComputerName
        [Parameter(Mandatory = 0,
                   Position=1)]
        [System.Management.Automation.PSCredential]
        $credential
    )

    Begin
    {
        $nameSpace = "SecurityCenter2"
        $className = "AntiSpywareProduct"
    }

    Process
    {
        if ($PSBoundParameters.count -eq 0)
        {
            if ((Get-CimInstance -namespace "root" -className "__Namespace").Name -contains $nameSpace)
            {
                Write-Verbose ("localhost cim session")
                Get-CimInstance -Namespace "root\$nameSpace" -ClassName $className
            }
            else
            {
                Write-Warning ("You can not check AntiSpyware with {0} as it not contain SecutiryCenter2" -f $OSName)
            }
        }
        else
        {
            try
            {
                Write-Verbose ("creating cim session for {0}" -f $computerName)
                $cimSession = New-CimSession @PSBoundParameters
                if ((Get-CimInstance -namespace "root" -className "__Namespace" -cimsession $cimSession).Name -contains $nameSpace)
                {
                    Get-CimInstance -Namespace "root\$nameSpace" -ClassName $className -CimSession $cimSession
                }
                else
                {
                    Write-Warning ("{0} not contains namespace {1}, you can not check {2}." -f $computerName, $nameSpace, $className)
                }
            }
            finally
            {
                $cimSession.Dispose()
            }
        }
    }

    End
    {
    }
}

実行例

複数のPCに行う場合 (credentialが同一ならですが)、パイプライン越しに渡してください。

$credential = Get-Credential
"10.0.0.10","10.0.0.11" | Get-AntiSpyware -credential $credential

モジュールに入れたり、ps1に入れておけばvalentiaと連携できて簡単です。

PS> valea 10.0.0.10,10.0.0.11 {Get-AntiSpyware}

isplayName               : Windows Defender
instanceGuid             : {D68DDC3A-831F-4fae-9E44-DA132C1ACF46}
pathToSignedProductExe   : %ProgramFiles%\Windows Defender\MSASCui.exe
pathToSignedReportingExe : %ProgramFiles%\Windows Defender\MsMpeng.exe
productState             : 397568
timestamp                : Fri, 25 Oct 2013 14:31:11 GMT
PSComputerName           : 127.0.0.1

まとめ

CIMを利用すると、cimsessionが利用できていいのですよ。 wmiからcimに移行しましょう。




以上の内容はhttps://tech.guitarrapc.com/entry/2013/10/26/120027より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14