<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PowerShell | DXSdata</title>
	<atom:link href="https://www.dxsdata.com/category/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dxsdata.com</link>
	<description>Software &#124; Network &#124; Administration</description>
	<lastBuildDate>Mon, 16 Aug 2021 11:19:53 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://www.dxsdata.com/wp-content/uploads/2023/08/cropped-logo-32x32.jpg</url>
	<title>PowerShell | DXSdata</title>
	<link>https://www.dxsdata.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[:de]PowerShell / NSClient / Nagios: Existenz bestimmter USB-Geräte ermitteln[:en]PowerShell / NSClient / Nagios: Check existence of certain USB devices[:]</title>
		<link>https://www.dxsdata.com/2021/08/powershell-nsclient-nagios-check-existence-of-certain-usb-devices/</link>
					<comments>https://www.dxsdata.com/2021/08/powershell-nsclient-nagios-check-existence-of-certain-usb-devices/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 16 Aug 2021 11:19:53 +0000</pubDate>
				<category><![CDATA[Administration]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Software]]></category>
		<guid isPermaLink="false">https://www.dxsdata.com/?p=2178</guid>

					<description><![CDATA[[:en] Scenario USB server devices, e.g. used for virtual appliances, can be error-prone when it comes to auto-reconnect USB client devices on startup. In addition, if you have to redirect USB license dongles to some virtual server, some devices might get &#8220;lost&#8221; from time to time for whatever reason. Solution / Help Using the following [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[:en]</p>
<h3>Scenario</h3>
<p>USB server devices, e.g. used for virtual appliances, can be error-prone when it comes to auto-reconnect USB client devices on startup. In addition, if you have to redirect USB license dongles to some virtual server, some devices might get &#8220;lost&#8221; from time to time for whatever reason.</p>
<h3>Solution / Help</h3>
<p>Using the following PowerShell script you can add some checks to your NSClient / Nagios or similar monitoring.</p>
<pre class="lang:ps decode:true " title="C:\Program Files\NSClient++\scripts\check_usb.ps1">#2021-08 www.dxsdata.com
#Checking existence of certain USB devices
#To get the IDs, execute this script with -Debug option, or execute below Get-... command

param(
[switch] $Debug
)

#adjust to your needs
$neededUsbDevices = @(
    [pscustomobject]@{NameFriendly='Dongle1';Id='USB\HASP\2&amp;1234'}
    [pscustomobject]@{NameFriendly='Dongle2'; Id='USB\HASP\2&amp;2345'}
    [pscustomobject]@{NameFriendly='Dongle3'; Id='USB\VID_0529&amp;PID_0001\1&amp;3456'}
    [pscustomobject]@{NameFriendly='Dongle4'; Id='USB\VID_0529&amp;PID_0001\1&amp;4567'}
)


#$existingUsbDevices = Get-WmiObject Win32_USBControllerDevice | ForEach-Object { [wmi]$_.dependent } | select-Object description,deviceid
$existingUsbDevices = Get-PnpDevice -PresentOnly -Class "USB" | Select-Object FriendlyName,DeviceID

if ($Debug) 
{
    "Debug output enabled. "
    "These devices will be checked: "
    $neededUsbDevices | fl
    "These devices are connected: "
    $existingUsbDevices
    ""
    ""
    "Comparing:"
}

$err = $False;

$output = "";
foreach($device in $neededUsbDevices)
{
    $result = $existingUsbDevices.Where({$_.DeviceID -eq $device.Id})
    $exists = $result.Count -gt 0
    $existsFriendly = If ($exists) {"OK"} Else {"MISSING"}
    $output += $device.NameFriendly + " " + $existsFriendly + ". " #cosmetics for nagios

    if (!$exists) { $err = $True; }
}
$output

$exitcode = if ($err) { 1 } Else { 0 }

if ($Debug) { "Exit code (Nagios Warning = 1, OK = 0) -&gt; " + $exitcode }

exit($exitcode);</pre>
<p>Save it into your NSClient&#8217;s scripts folder.</p>
<p>Modify nsclient.ini:</p>
<pre class="lang:sh decode:true ">; Below [/settings/external scripts/scripts]
check_usb = cmd /c echo scripts\check_usb.ps1; exit($lastexitcode) | powershell.exe -command -</pre>
<p>I assume you have several options already enabled, like CheckExternalScripts = 1, allow arguments etc.</p>
<p>Don&#8217;t forget to restart your NSClient service.</p>
<p>You might also want to check the result on your Nagios server like:</p>
<pre class="lang:sh decode:true ">./check_nrpe -H myserver -c check_usb</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>References</h3>
<p>https://docs.microsoft.com/en-us/powershell/module/pnpdevice/get-pnpdevice?view=windowsserver2019-ps</p>
<p>https://docs.nsclient.org/howto/external_scripts/[:]</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dxsdata.com/2021/08/powershell-nsclient-nagios-check-existence-of-certain-usb-devices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
