i want to execute this commands from c#:
@echo off
PowerShell "ForEach($v in (Get-Command -Name \"Set-ProcessMitigation\").Parameters[\"Disable\"].Attributes.ValidValues){Set-ProcessMitigation -System -Disable $v.ToString() -ErrorAction SilentlyContinue}"
pause
i put the it in cmd file and run it using :
Process.Start(CMDFilePath);
but error occurre:
C:\Users\New-hwid\Desktop\Debug2>PowerShell "ForEach($v in (Get-Command -Name \"Set-ProcessMitigation\").Parameters[\"Disable\"].Attributes.ValidValues){Set-ProcessMitigation -System -Disable $v.ToString() -ErrorAction SilentlyContinue}"
Get-Command : The term 'Set-ProcessMitigation' 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 line:1 char:16
ForEach($v in (Get-Command -Name "Set-ProcessMitigation").Parameters[ ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (Set-ProcessMitigation:String) [Get-Command], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
Cannot index into a null array.
At line:1 char:15
... rEach($v in (Get-Command -Name "Set-ProcessMitigation").Parameters["D ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : NullArray
when i open cmd file normally it worked fine , but when i run it via Process.Start(...) this error happen, my OS is windows 10 64 bit, how to run CMD file without error? Thanks
CodePudding user response:
from this answer , The issue that you're seeing is because of the File System Redirector which is occurring because you're running your program as 32-bit on your 64-bit OS. Therefore, you're executing %windir%\SysWOW64\SystemPropertiesProtection.exe (ex: C:\Windows\SysWOW64\SystemPropertiesProtection.exe). so i add In app.manifest, and replace
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
with
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
and modified the code to work with powershell:
public static void OpenPowerShell(string Command)
{
try
{
string filename = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "System32", "WindowsPowerShell","v1.0","powershell.exe");
//environment variable windir has the same value as SystemRoot
//use 'Sysnative' to access 64-bit files (in System32) if program is running as 32-bit process
//use 'SysWow64' to access 32-bit files on 64-bit OS
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
//filename = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "SysNative", "PowerShell.exe");
filename =System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "SysNative", "WindowsPowerShell", "v1.0", "powershell.exe");
}
ProcessStartInfo startInfo = new ProcessStartInfo(filename);
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(filename);
startInfo.Arguments = "-NoExit " Command ;
Process.Start(startInfo);
}
catch (Exception ex)
{
FRM_MSG f = new FRM_MSG();
f.ShowDLG(" ",
ex.Message "\n" ex.StackTrace.ToString(),
FRM_MSG.MSGIcon.Error,
FRM_MSG.BTNS.One,
new string[] { "Ok" });
throw ex;
}
}