using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var newProcessInfo = new System.Diagnostics.ProcessStartInfo();
newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
newProcessInfo.Verb = "runas";
System.Diagnostics.Process.Start(newProcessInfo);
newProcessInfo.Arguments = @"sfc /scannow";
}
}
}
所以我的代码可以解决问题 . 你单击Windows窗体应用程序按钮,它将以64位作为管理员运行Windows Powershell但不会运行.ps1脚本“c:\ path \ script.ps1”或直接写出的命令,如“sfc / scannow”以上 .
我正在读到,如果在代码开头的某处没有加载“Set-ExecutionPolicy Unrestricted”,那么powershell命令有时会无效 .
请帮忙!我一直在寻找答案 .
1 回答
首先,您需要在启动进程之前指定
Arguments
属性:其次,您需要告诉PowerShell
sfc /scannow
是一个命令,而不是命令行开关 .在命令行上,您将执行
powershell.exe -Command "sfc /scannow"
,因此在您的情况下,正确的Arguments
值将是(
""
是逐字字符串文字中"
的转义序列)对于
.ps1
文件,请使用-File
开关:如果您不知道目标系统上的执行策略,则可以绕过它而不会影响机器范围的策略
-ExecutionPolicy Bypass
: