在C#中调用Powershell脚本返回0 [英] Invoking a Powershell script in C# results returning 0

查看:266
本文介绍了在C#中调用Powershell脚本返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Windows服务,该服务每分钟都会调用Powershell脚本. Powershell脚本返回本地系统信息.

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

当我在Powershell ISE中运行时,它可以工作.

When I run in Powershell ISE it works.

然后我的C#Windows服务尝试调用脚本

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
                }

在调试时,结果返回的计数为0.几天前此操作正常,现在已决定停止.几个小时使我发疯.我在做什么错了?

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?

推荐答案

将构建类型从32位更改为64位,这应该可以解决您的问题.

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

这篇关于在C#中调用Powershell脚本返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆