I am managing an ASP.NET site providing users with managed access to various functionality on an IIS Server. The application is running on the .NET Framework 4.6.1 and the users are very happy with the features, mostly running SQL stored procedures. Now, there is "one more thing".
There is a need to, basically, see the state of three tasks in Task Scheduler Library running on the same box. The author of those tasks I have credentials to. Then, the desire is to enable or disable at will.
My first approach is to write PowerShell scripts that can be called from the ASP.NET site. Running as Windows PowerShell ISE as Administrator on the box, I have a script that provides the desired output:
# https://stackoverflow.com/questions/1313673/execute-powershell-as-an-administrator-from-c-sharp
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
# https://blog.danskingdom.com/Run-PowerShell-as-another-user/
$securePassword = ConvertTo-SecureString "*******" -AsPlainText -force
$credential = New-Object System.Management.Automation.PsCredential("domain\username",$securePassword)
# After specifying your credentials, you can then use them in your call to create a New-PSSession using the PowerShell script below.
$s = New-PSSession -credential $credential -ComputerName localhost
Enter-PSSession -Session $s
# https://bobcares.com/blog/manage-scheduled-tasks-with-powershell/
$results= Get-ScheduledTask | Where-Object {$_.TaskPath -eq "\"} | Where-Object { $_.TaskName -like "STAGE_DEMO_*"}
Exit-PSSession
Write-Output $results
Now my Page_Load
comes from the accepted answer to this question: Binding output from powershell script to gridview in asp.net c#
protected void Page_Load(object sender, EventArgs e)
{
this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
// Initialize PowerShell engine
var shell = PowerShell.Create();
// Add the script to the PowerShell object
string rootPath = Server.MapPath("~");
var scriptPath = rootPath + @"\Scripts\PowerShell\*****.ps1";
shell.Commands.AddCommand(scriptPath);
try
{
var results = shell.Invoke();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
Executing, I do not get usable output:
In my case, the actual credentials are validated, since they work in the ISE, running as Administrator.
However, if I replaced my script with this one:
write-output @(
(new-object PSCustomObject -Property ([ordered] @{
"MyProperty1" = "MyValue1.1"
"MyProperty2" = "MyValue2.1"
"MyProperty3" = "MyValue3.1"
})),
(new-object PSCustomObject -Property ([ordered] @{
"MyProperty1" = "MyValue1.2"
"MyProperty2" = "MyValue2.2"
"MyProperty3" = "MyValue3.2"
}))
);
I get very nice results I can see working with:
So, I am convinced I am running into an issue with at least one of these things:
- My script itself in some way I do not see.
- The security I am trying to support - that is supporting the equivalent of running as administrator in the PS ISE.
- The format of my own output
I have been thoroughly looking at these and am not finding a path forward.
Please, can someone tell me: am I encountering an insurmountable issue?
If so, what is the advice for managed Task Scheduler access to list/disable/enable tasks of another author, preferable through an ASP.NET site?
Thank you