I am working on dashboard that shows live details about a set of machines. I have the below for my Powershell
param([string] $server)
$services = Get-WmiObject win32_service -ComputerName $server | Select -Property Name, ExitCode, State, Status
$disk = get-WmiObject win32_logicaldisk -Computername $server | Select-Object -Property DeviceID, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
$cpu = Get-WmiObject win32_processor -ComputerName $server | Measure-Object -property LoadPercentage -Average | Select Average
$server_summary = @{
services = $services
disk = $disk
cpu = $cpu
}
$json = $server_summary | ConvertTo-Json
return $json
I have the below for my c# function
public JsonResult test (string server)
{
string script = ControllerContext.HttpContext.Server.MapPath("~/Content/Powershell/Get-Server-Summary.ps1");
string cmd = script + " -server '" + server + "'";
using (var ps = PowerShell.Create())
{
ps.AddScript(cmd);
var results = ps.Invoke();
return Json(results);
}
}
In the script section of my HMTL I have
function test(server) {
$.ajax({
type: "POST",
url: '@Url.Action("test")',
dataType: "json",
data: "{'server':" + "'" + server + "'}",
contentType: "application/json; charset=utf-8",
success: function (data) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Error getting stats for ' + server + '.');
},
complete: function () {
}
});
}
$(document).ready(function(){
test('server_name');
});
Even though I have converted the result to JSON before returning to the results to the application, I receive and empty string.
Am I missing something? I was able to get results with a slightly different function, but it returns incorrect results.
I will post that in the comments because I have too much code to post my question...
The results on the server side look like the below.
You can see that I am getting one result, which is a string containing all the json.
It was hard to get the screenshot in time. But you can see the value is there, It is just REALLY buried... Do I really need to parse through all of this? There isn't a function for this already?