Ps 命令未在 Windows 服务下运行

问题描述 投票:0回答:1

当尝试在 Windows 服务中运行 ps 命令时,它返回一个错误:

Get-LocalUser:“Get-LocalUser”未被识别为 cmdlet、函数、脚本文件或程序。验证文件路径是否正确,然后重试。 C:\用户\管理员\源 epos\ps.test in\Debug\GetLocalUserv2.ps1:1 字符:1

  • 获取本地用户
  •   + CategoryInfo          : ObjectNotFound: (Get-LocalUser:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    

但是当我将它作为控制台项目运行时,它运行正常!在 win 服务中运行某些 ps 命令是否有 Windows 限制?

using System.Diagnostics;
using System;
using System.ServiceProcess;
using System.Management.Automation;
using System.Timers;
using System.IO;
using System.Reflection;

namespace ps.test
{
    public partial class Service1 : ServiceBase
    {
        private static string localpath;
        private static string logPath;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            localpath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            logPath = Path.Combine(localpath, "log.txt");
            File.AppendAllText(logPath, "start->"+DateTime.Now.ToString("yyyyMMdd:HHmmss") + Environment.NewLine);
            // Set up a timer that triggers every minute.
            Timer timer = new Timer();
            timer.Interval = 60000; // 60 seconds
            timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
            timer.Start();
        }
        public void OnTimer(object sender, ElapsedEventArgs args)
        {
            File.AppendAllText(logPath, DateTime.Now.ToString("yyyyMMdd:HHmmss") + Environment.NewLine);
            string output;
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            string batFile = Path.Combine(localpath, "GetLocalUserv2.bat");
            startInfo.FileName = batFile;
            //if (script.Parameters != null && script.Parameters.Length > 0)
            //    startInfo.Arguments = string.Join(" ", script.Parameters);
            startInfo.WorkingDirectory = localpath;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;

            try
            {
                using (Process exeProcess = Process.Start(startInfo))
                {
                    output = exeProcess.StandardOutput.ReadToEnd();
                    exeProcess.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                output = ex.Message;
                // Log error.
                //Logger.Error($"Erro ao executar script {e.Message}");
            }
            File.AppendAllText(logPath, output + Environment.NewLine);
            try
            {
                var test = PowerShell.Create().AddScript("Get-LocalUser").Invoke();
                System.IO.File.AppendAllText(logPath, "test->" + test);
            }
            catch (Exception ex)
            {
                output = ex.Message;
            }
            File.AppendAllText(logPath, output + Environment.NewLine);
        }
        protected override void OnStop()
        {

            File.AppendAllText(logPath, "stop->" + DateTime.Now.ToString("yyyyMMdd:HHmmss") + Environment.NewLine);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
        }
    }
}
c# .net .net-4.0
1个回答
0
投票

我想我解决了问题。在网上搜索后,有一些 ps 命令,您必须在解决方案中启用 x64。该服务是“任何 CPU”。修复到 x64 后就可以了。

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localuser?view=powershell-5.1

“Microsoft.PowerShell.LocalAccounts 模块在 64 位系统上的 32 位 PowerShell 中不可用。”

© www.soinside.com 2019 - 2024. All rights reserved.