首页 > 解决方案 > 脚本手动运行,但不通过 c#

问题描述

我写了一个简单的控制台应用程序,应用程序以管理员权限运行,代码基于此链接,用户输入.ps1(powershell)脚本路径,应用程序执行此路径中的脚本。

首先,我在“Hello World”脚本上尝试了该应用程序,它运行良好,但是当我尝试其他脚本时,powershell 给出了错误。

我在.ps1scipt 中有这个:

install-WindowsFeature smtp-server
Read-Host -Prompt “Press Enter to exit”

令人惊讶的是,如果我install-WindowsFeature smtp-server手动运行此命令,它可以正常工作。

但我需要应用程序来完成,而不是手动完成。

这是powershell错误:

install-WindowsFeature : The term 'install-WindowsFeature' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At c:\users\administrator\desktop\EnableSMTP.ps1:1 char:1
+ install-WindowsFeature smtp-server
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (install-WindowsFeature:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Press Enter to exit:

标签: c#powershellwindows-server-2012

解决方案


这在过去发生在我身上。

在解决方案资源管理器中,右键单击项目-->属性-->构建

将平台目标从“任何 CPU”更改为 x64

在此处输入图像描述


推荐阅读