Hi everyone,
Ever since I started working with SAM I have been looking for a way to auto-discover the applications installed on Windows servers. Has anyone come up with a solution for this?
I created a the custom PowerShell script bellow that can return up to 10 services and their status ( up or down). Can you guys take a look and maybe help improving it?
here is the code:
$server = "${Node.DNS}"
$c = Get-Credential -credential ${CREDENTIAL}
Function SecureStringToString($value) {
[System.IntPtr] $bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($value);
try {
[System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr);
}
finally { [System.Runtime.InteropServices.Marshal]::FreeBSTR($bstr); }
}
[string] $username = $c.Username
[string] $password = SecureStringToString $c.Password
$securepass = $password | ConvertTo-SecureString -AsPlainText -Force
$securecred = new-object System.Management.Automation.PSCredential($username, $securepass);
$services = Invoke-Command -ComputerName $server -credential $securecred -ScriptBlock { Get-CimInstance -ClassName Win32_Service -Filter "StartMode = 'Auto'" -ErrorAction SilentlyContinue }
$CustomObject = @()
foreach ($service in $services | Sort-Object Name ) {
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty -Name ServiceName -Value $service.Name
$obj | Add-Member -MemberType NoteProperty -Name DisplayName -Value $service.DisplayName
$obj | Add-Member -MemberType NoteProperty -Name State -Value $service.State
$obj | Add-Member -MemberType NoteProperty -Name ProcessID -Value $service.ProcessId
$obj | Add-Member -MemberType NoteProperty -Name PathName -Value $service.PathName
$obj | Add-Member -MemberType NoteProperty -Name Description -Value $service.Description
if ($obj.PathName -notlike "C:\Windows\System32\*" -and $obj.PathName -notlike "*VMWare*" -and $obj.PathName -notlike "*\Trend Micro\*" -and $obj.PathName -notlike "C:\Program Files (x86)\SolarWinds\Agent\*" -and $obj.PathName -notlike "C:\windows\SysWOW64\ContegoSPOP\*") { $CustomObject += $obj }
}
$i = 0
$CustomObject | Sort-Object ServiceName | ForEach-Object {
if ($i -lt 10) {
Write-host "Message.$($i) : $($_.ServiceName ) "
if ($_.State -eq "Running") { [int]$status = '0' } else { [int]$status = '1' }
Write-host "Statistic.$($i): $($status)"
$i++
}
}
Here is how the template looks like on my SAM:

Here is how the application looks like when assigned to a node:

Post