So, spent most of the day on this and am admittedly not a Powershell guru by any means. Unfortunately, I just cannot figure out how to get it to translate the output into anything meaningful. I don’t get the failure notices that I was getting before which makes me think it’s actually using remote PS but I also don’t know how to actually check that. I just want some verification that it’s actually seeing the response to (Get-Command java | Select-Object -ExpandProperty Version).toString() . Because the servers that I have Java installed on, I can run that command and get a response of what version number it is. But I can’t get LM to interpret that data. I’m guessing I’m missing sometime of “write/output” command but I find a lot of documentation about that online and am not sure how to use that again, in a way that LM knows how to use it.
Script “borrowed” and modified from @Stuart Weenig:
# Clears the CLI of any text
Clear-Host
# Clears memory of all previous variables
Remove-Variable * -ErrorAction SilentlyContinue
#------------------------------------------------------------------------------------------------------------
# Initialize Variables
$wmi_pass = '##WMI.PASS##'
$wmi_user = '##WMI.USER##'
$hostname = '##SYSTEM.SYSNAME##'
$collectorName = hostname
# Insert additional variables here
# If the hostname is an IP address query DNS for the FQDN
if ($hostname -match "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b") {
$hostname = [System.Net.Dns]::GetHostbyAddress($hostname).HostName
}
## This script block should contain all code that you want to execute remotely on the target host.
$scriptBlock = {
(Get-Command java | Select-Object -ExpandProperty Version).toString()
}
#------------------------------------------------------------------------------------------------------------
try {
#-----Determin the type of query to make-----
# check to see if this is monitoring the localhost collector, as we will not need to authenticate.
if ($hostname -like $collectorName) {
$response = Invoke-Command -ScriptBlock $scriptBlock
}
# are wmi user/pass set -- e.g. are these device props either not substiuted or blank
elseif (([string]::IsNullOrWhiteSpace($wmi_user) -and [string]::IsNullOrWhiteSpace($wmi_pass)) -or (($wmi_user -like '*WMI.USER*') -and ($wmi_pass -like '*WMI.PASS*'))) {
# no
$response = Invoke-Command -ComputerName $hostname -ScriptBlock $scriptBlock
} else {
# yes. convert user/password into a credential string
$remote_pass = ConvertTo-SecureString -String $wmi_pass -AsPlainText -Force;
$remote_credential = New-Object -typename System.Management.Automation.PSCredential -argumentlist $wmi_user, $remote_pass;
$response = Invoke-Command -ComputerName $hostname -Credential $remote_credential -ScriptBlock $scriptBlock
}
exit 0
} catch {
# exit code of non 0 will mean the script failed and not overwrite the instances that have already been found
throw $Error[0].Exception
exit 1
}