LogicMonitorでのPowerShellスクリプトの使用

最終更新日: 30 年 2020 月 XNUMX 日

概要

PowerShellスクリプトをLogicMonitorに統合して、WMIまたはPerfmonから利用できない特定のWindows統計を監視できます。

Windowsセットアップ

LogicMonitorでPowerShellを利用する最も便利な方法は、リモートPowerShellスクリプトを使用することです。 これを可能にするには、Windowsでいくつかのセットアップが必要です。

ステップ 1:監視する各PCでPowerShellウィンドウを開き、次のように入力します(*はコレクターIPに置き換えることができます)。

注: 2行目のコマンドを使用して、監視対象のデバイスをコレクターの信頼できるホストに追加する必要がある場合があります。

Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts * -Force
Restart-Service WinRM -Force
winrm set winrm/config/winrs '@{MaxShellsPerUser="50"}'

2次のサブステップを実行します リモートPowershellが機能していて、LogicMonitorで使用できるユーザー名とパスワードへのアクセスが許可されていることを確認します。 コレクターが実行されているマシンでPowerShellを開き、次のように入力します。

$hostname= (enter hostname here in quotes)
$pass= (enter password here in quotes)
$user= (enter username here in quotes)
$remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force
$remotecredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $remotepass
Invoke-Command -ComputerName $hostname -ScriptBlock {@(Get-Process -ea silentlycontinue "svchost").Count} -credential $remotecredential

ステップ3: これが完了したら、agent.confファイルで無制限のPowershellスクリプトを許可するようにコレクターを設定する必要があります。 このファイルの編集については、を参照してください。 Collector構成ファイルの編集。

# powershell execution policy, could be one of "Restricted, AllSigned, RemoteSigned, Unrestricted"
powershell.policy=Unrestricted

PowerShellメソッド

PowerShellでスクリプトを作成するときは、最初に接続に使用する資格情報を定義する必要があります。これは、次の方法で実行できます。

# Pass credentials and hostname as arguments
param ([string] $hostname, [string] $user, [string] $pass)
# Change the password into a secure string to be used in the credentials
$remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force
# Build the credential
$UserCredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$remotepass

資格情報が作成されると、これを使用してPowerShellセッションを作成したり、リモートコンピューターでコマンドを呼び出したりできます。 単一のコマンドを実行する必要がある場合、複数のコマンドを実行する必要がある場合、または特定のアプリケーションを(Exchange)に接続している場合は、Invokeコマンドを単独で使用して、再利用可能なPowerShellセッションを作成できます。

これは、PowerShellを使用してExchangeに接続し(Kerberos認証では完全修飾ドメイン名を使用する必要があります)、Exchangeデータベースのデータベース名とサーバーを収集する例です。 数に限りがありますので、セッションを終了します。

# Get credentials from arguments
param ([string] $fqdn, [string] $user, [string] $pass)
# Build credentials
$remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force
$UserCredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$remotepass
# Create and import the session, import only grabs a single cmdlet in order to speed the connection and execution time up.
$Session= New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:// $fqdn /PowerShell/ -Authentication Kerberos -Credential $UserCredential
$importsession= Import-PSSession $Session -CommandName Get-MailboxDatabase -AllowClobber
#Create output which logicmonitor understands for instance names
$database= Get-MailboxDatabase -Status
for each ($database in $database){
Write-Host "$($database.Name)##$($database.Name)##$($database.Server)"
}
# Remove the PowerShell session
Remove-PSSession $Session

PowerShellを使用してリモートコマンドを実行する別の方法は、Invoke-Commandコマンドレットを使用することです。 以下は、Veeam Backupsデータソースでアクティブに検出するための例です(VeeamコマンドにはVeeam PSsnapinが必要です)

セッションが作成された場合、Invoke-Commandを複数回使用して複数のコマンドを実行できます。

# Get credentials from arguments
param ([string] $hostname, [string] $user, [string] $pass)
# Build Credential
$remotepass= ConvertTo-SecureString -String $pass -AsPlainText -Force
$remotecredential= new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$remotepass
# Create Session
$Session= New-PSSession -ComputerName $hostname -Credential $remotecredential
# Invoke a command in the session created above and store the output in a variable for later processing. When using a Invoke-Command variables need to be passed, unlike Import-PSSession.
$jobs= Invoke-Command -Session $session -ScriptBlock {
Add-PSSnapin VeeamPSSnapin
Get-VBRJob -WarningAction silentlyContinue
}
# Create instance names from Veeam backup jobs
foreach ($jobs in $jobs){
$jobName= $jobs.Name
$jobDescription= $jobs.Description
Write-Host "$jobName##$jobName##$jobDescription"
}
# Remove the session
Remove-PSSession $Session

Invoke-コマンドはグローバル変数にアクセスしません。コマンド内で渡す必要があります。 例については、以下を参照してください。

$jobs= Invoke-Command -Session $session -ScriptBlock {
@($wildvaluePassed= $args [0];
Add-PSSnapin VeeamPSSnapin
Get-VBRJob -WarningAction silentlyContinue |  Where-Object {$_.Name -eq $wildvaluePassed})
} -argumentlist $wildvalue

アクティブディスカバリー

PowerShellでインスタンスを検出するのは簡単な作業です。 これは通常、foreachループを使用してPowerShellオブジェクト内の各名前を検索し、それらを次の形式で出力することで構成されます。 LogicMonitorは解釈できます。

$database= Get-MailboxDatabase -Status
foreach ($database in $database){
# Prints the instances in the format wildvalue##wildalias##description
Write-Host "$($database.Name)##$($database.Name)##$($database.Server)"
}

コレクション

LogicMonitorのデータを収集するには、ActiveDiscoveryで定義されている## WILDVALUE ##を使用して、特定のインスタンスのデータを検索します。

# Retrieve the WILDVALUE and credentials needed
param ([string] $fqdn, [string] $user, [string] $pass, [string] $wildvalue)
# Use the WILDVALUE to filter data in a powershell cmdlet$mailbox = Get-MailboxDatabaseCopyStatus | Where-Object {$_.Name -eq $wildvalue} | Select-Object Name,Status,CopyQueueLength,ReplayQueueLength,ContentIndexState

ロジックモニターにデータを送信する場合、これを実現する最善の方法は数値を使用することです。これは、ブール値または状態を整数に変換することを意味する場合がありますが、これは スイッチ。 データをLogicMonitorにプッシュする良い方法は、以下の形式に従うことです。 次に、正規表現またはキーと値のペアを次のように使用できます。 解釈方法 データポイント内。

Write-Host "Result=$Result"
Write-Host "Working=$Working"
Write-Host "Completed=$Completed"
Write-Host "AvgSpeed=$AvgSpeed"
Write-Host "Duration=$DurationSeconds"
Write-Host "FinishedAgo=$FinishedAgo"
Write-Host "StartedAgo=$StartedAgo"
記事上で