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
  • 1
    Try chainging the build from 32 bit to 64 bit, this worked for me. Commented May 15, 2014 at 10:50
  • I've added it as an answer, so you can check it now ;) Commented May 15, 2014 at 11:26

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Any information on why this helps to fix this problem?
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: as other Powershell commands runs perfectly in AnyCPU mode but why some fails like this? any particular reason to use 64-bit mode?
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".

1 Comment

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