When I comment the Invoke-Command line the script completes successfully when not return Timeout PE018

My code:

##USB MONITOR

##param(
##[string]$DEVICE
##)

$DEVICE = "USB_HUB_01"

## CREDENTIALS
$username = "test.local\testprtg"
$password = Get-Content "C:\PRTG\Custom Sensors\EXEXML\Key.txt" | ConvertTo-SecureString 
$credential = New-Object System.Management.Automation.PsCredential($username,$password)
$server = 'server.test.local'
$RESULT = "0"


$ScriptBlockContent = {

		$MORPHOUSBREMOTE = gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)}
		return $MORPHOUSBREMOTE.DeviceID
}


##$session = New-PSSession -computerName $server -credential $credential
$MORPHOUSBLIST = Invoke-command -credential $credential -scriptblock $ScriptBlockContent -computerName $server
##Remove-PsSession $session.id


foreach ($i in $MORPHOUSBLIST){
	
	
		if ($DEVICE -eq $i){
			$RESULT = "1"
			break;
		}else{
			$RESULT = "0"
		}
	
}

	"<prtg>"
			"<result>"
			"<channel>MSG</channel>"
			"<value>$RESULT</value>"
			"</result>"
	"</prtg>"


Article Comments

WMI and PowerShell - this might be an issue. You might possibly face a x32 and x64 issue here.

If so, you can work around it - look at the following link - it talks about a Parser-Script to bypass this issue. https://www.it-admins.com/monitoring-shadow-copies-with-prtg/

In the end it is a similar situation where WMI is in use.

Btw. - you have code you wouldn't need in your loop - the ELSE case is obsolete cause you set the variable way above to 0 and break it the second you change it anyways.. This should be sufficient:

foreach ($i in $MORPHOUSBLIST){
	if ($DEVICE -eq $i){
		$RESULT = "1"
		break;
	}
}

Regards

Florian Rossmark

www.it-admins.com


Aug, 2018 - Permalink