I'm creating a Windows Service that calls a Powershell script every minute. The Powershell script returns local system information.
function MachineInformation
{
[hashtable]$machine = @{}
$computerSystem = get-wmiobject Win32_ComputerSystem
$machine.machine = $computerSystem.Name
$machine.key = $computerSystem.Manufacturer
[String]$machine.value = Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
[DateTime]$machine.timestamp = Get-Date
Return $machine
}
MachineInformation
When I run in Powershell ISE it works.
My C# Windows Service then tries to invoke the script
PowerShell ps = PowerShell.Create();
ps.AddScript("C:\\Scripts\\SystemInfo.ps1");
Collection<PSObject> results = ps.Invoke();
foreach (PSObject result in results)
{
//Do something
}
When debugging, results is returning a count of 0. This was working fine a few days ago and now it has decided to stop. It has been driving me crazy for hours. What am i doing wrong?