Hi,
I'm running round in circles. I have an application that assembles a powershell script (Get-ChildItems | Remove-Item on UNC directory) on the local computer that has the application installed on.
The application assembles the .ps1 file successfully, this file runs perfectly when I open ps as administrator.
However, I cannot find any way to get the Powershell.Create() to open as Administrator so that the script runs. It simply does nothing now...
The above code has two versions of it. Initially I simply started with the console being created, the script added, and the invoke executing... Now I've got all this stuff everywhere from trying out what everyone suggests, but nothing works...
I've also added this line into my App Manifest so that the application itself is started as administrator:
Any help on how to do this would be greatly appreciated.
Cheers,
Steve
I'm running round in circles. I have an application that assembles a powershell script (Get-ChildItems | Remove-Item on UNC directory) on the local computer that has the application installed on.
The application assembles the .ps1 file successfully, this file runs perfectly when I open ps as administrator.
However, I cannot find any way to get the Powershell.Create() to open as Administrator so that the script runs. It simply does nothing now...
C#:
private void Execute_Script_CMD(int _i, string _cmd)
{
_i = _i - 1;
jobs.Add(false);
WSManConnectionInfo _conInfo = new WSManConnectionInfo();
_conInfo.ComputerName = serverHost;
Runspace _psRun = RunspaceFactory.CreateRunspace(_conInfo);
using (PowerShell _pscon = PowerShell.Create())
{
_psRun.Open();
_pscon.Runspace = _psRun;
var re = _pscon.AddScript(@"'" + _cmd + @"'");
var results = re.Invoke();
_pscon.Runspace.Close();
_pscon.Dispose();
}
/*
RunspaceMode rsM = RunspaceMode.NewRunspace;
PowerShell _pscon = PowerShell.Create(rsM);
_pscon.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -Force");
_pscon.AddScript(@"'" + _cmd + @"'");
_pscon.Invoke();
_pscon.Runspace.Close();
*/
jobs[_i] = true;
}
The above code has two versions of it. Initially I simply started with the console being created, the script added, and the invoke executing... Now I've got all this stuff everywhere from trying out what everyone suggests, but nothing works...
I've also added this line into my App Manifest so that the application itself is started as administrator:
HTML:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Any help on how to do this would be greatly appreciated.
Cheers,
Steve