0

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?

2 Answers2

3

Change the build type from 32 bit to 64 bit, this should solve your problem.

leskovar
  • 661
  • 3
  • 8
  • Any information on why this helps to fix this problem? – w5l May 15 '14 at 11:27
  • If an application is being run on a 64bit OS than an application calling the PowerShell script also has to be built in 64 bit mode. I am not sure why. – leskovar May 15 '14 at 11:33
  • @leskovar: as other Powershell commands runs perfectly in AnyCPU mode but why some fails like this? any particular reason to use 64-bit mode? – Nani Aug 10 '18 at 08:19
1

Actually I did a project and built in "Any CPU" mode and it works fine. My problema was about "Location". I figured out this trying execute the script from another folder, the problem is that in C# I couldn´t see the errors messages.

To solve my problem I inserted the "Set-Location" cmdlet before call my script, eg.: "Set-Location 'c:\myscriptlocation'; c:\myscriptlocation\myscript.ps1".

  • Complementing my answer, I had problem with "Install-Module". The module only was in "C:\Program Files\WindowsPowerShell\Modules", because this only works in x64 mode. I solved with a symbolic link in "C:\Program Files (x86)\WindowsPowerShell\Modules" folder – Éderson C. do Nascimento Apr 15 '16 at 19:35