I want to run command prompt arguments using a ASP.Net application. I am using visual studio 2013.
Is it possible?
the followings are the command prompt commands I want to execute using MVC application to create and run a job in jenkins
C:\Users\ .jenkins java -jar C:\Users.jenkins\war\WEB-INF\jenkins- cli.jar -s http://localhost create-job myjob < C:\Users\Desktop\config.xml
C:\Users\ .jenkins java -jar C:\Users.jenkins\war\WEB-INF\jenkins-cli.jar -s http://localhost / build myjob
Just to be sure, you want to run these command line tools on thee webserver, right? Not on the client connecting to the web application.
If the former, it's pretty straightforward, make sure that you IIS application pool is running under a user account that is allowed to execute these tools and use the Process class together with the ProcessStartInfo class to invoke the commandline and wait for the results:
string jobname = jobNameFromInput;
if (!Regex.IsMatch(jobname, "^(\"[a-z0-9-_ ]+\"|'[a-z0-9-_ ]+'|[a-z0-9-_]+)$", RegexOptions.IgnoreCase)
{
throw SecurityException("Invalid job name");
}
ProcessStartInfo startInfo = new ProcessStartInfo("C:\Users.jenkins\java.exe");
string argumentTemplate = "-jar C:\\Users.jenkins\\war\\WEB-INF\\jenkins-cli.jar -s http://localhost / build {0}";
startInfo.Arguments = string.Format(argumentTemplate, jobname);
Process.Start(startInfo);
Process.WaitForExit();
See:
Process.WaitForExit
Process.Start
Related
I have the following simple code that works and executes a powershell command in asp.net core website running on my cshtml.cs code behind page
using System.Management.Automation;
public void Execute()
{
using (var ps = PowerShell.Create())
{
ps.AddScript("Get-Date");
var results = ps.Invoke();
foreach (var result in results)
{
string test = result.ToString();
}
}
}
This works i get the current date reported as a string and results variable contains a count of 1.
"7/16/2020 4:38:27 PM"
If i then change it to another command say
ps.AddScript("Get-WmiObject");
or
ps.AddScript("Get-LocalGroupMember -group my_local_group_name");
Then i get no results, results comes back as count of 0 ?? The only thing i can determine is that Get-Date is a single string where as the other commands Get-WmiObject and Get-LocalGroupMember are multiple lines of text in the outputs when i execute them from a normal Powershell window.
my goal is to be able to execute the Get-LocalGroupMember command and get it's output. Is there something else i'm missing or not doing in terms of Powershell commands ? why is it some commands are working and producing output and others are not ?
UPDATE:
So i was getting error in the error stream of the ps object it states
{The 'Get-LocalGroupMember' command was found in the module 'Microsoft.PowerShell.LocalAccounts', but the module could not be loaded. For more information, run 'Import-Module Microsoft.PowerShell.LocalAccounts'.}
if i open a x86 Powershell window and run the Get-LocalGroupMember cmd it gives me error
Get-LocalGroupMember : The term 'Get-LocalGroupMember' 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.
running the Get-LocalGroupMember cmd in a regular Powershell window (that is x64) the cmd runs perfect.
My project is set to x64 :( so am failing to understand why asp.net is using what seems to me a 32bit version of powershell. That's what i need i need to be able to execute my powershell cmds as x64 because Get-LocalGroupMember cmd is only available in Powershell x64 .
Please Help ? thanks
so i ended up having to do the following for some reason in order to get certian commands like Get-LocalGroupMember to work, other commands like Get-Date don't require the import, and even though on my system i had execution policy unrestricted, whenever i use c# in asp.net and checked errors on my ps object it was acting like it wasn't and would not let me run a script using AddScript. So i had to be administrator running visual studio and use set-executionpolicy like below, then be able to add the localaccounts module to be able to use Get-LocalGroupMember.. again if i open a x64 Powershell window on the system i can run Get-LocalGroupMember fine. seems c# is doing some weird things with the Powershell shell that it is creating and running in memory. Anyway hopefully this saves someone else time if they run into the same situation.
using (var ps = PowerShell.Create())
{
ps.AddScript("Set-ExecutionPolicy Unrestricted -force");
ps.AddScript("Import-Module Microsoft.PowerShell.LocalAccounts");
ps.AddScript("Get-LocalGroupMember -group your_local_group_name | Out-String");
var results = ps.Invoke();
foreach (var result in results)
{
string test = result.ToString();
TestText = test;
}
}
I want to script the setup of a machine with powershell and I have no idea how to find the names of the module. I want to build the same script than the script bellow but for asp.net mvc 3, URL Rewrite 2.0, URL authorization...
I tried to search in google, web plateform installer... Am I missing something ?
Ex :
Import-Module ServerManager
$features = #(
"Web-WebServer",
"Web-Static-Content",
"Web-Http-Errors",
"Web-Http-Redirect",
"Web-Stat-Compression",
"Web-Filtering",
"Web-Asp-Net45",
"Web-Net-Ext45",
"Web-ISAPI-Ext",
"Web-ISAPI-Filter",
"Web-Mgmt-Console",
"Web-Mgmt-Tools",
"NET-Framework-45-ASPNET"
)
Add-WindowsFeature $features -Verbose
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
choco install -y webdeploy
Open a PowerShell prompt and use the Get-WindowsFeature cmdlet.
I believe this PS module is installed by default on servers. For client machines, running Windows 7 for example, you can install the Remote Server Management Tools - https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=7887
I have a symfony command to send emails. In the terminate listener, I run this command.
Below is the command I run,
php ../app/console insead:email:prepare <Base64 information> &
I passed & (Ampersand) sign to the command in order to spawn a new process on the shell.
The version of Symfony was 2.4 and it was working fine. Last week I updated the version to 2.7 and now it is not working.
It seems the command is not running in the shell. I am using Symfony Process.
$process = new Process($command, $this->rootDir);
$process->start();
I tried to get errorOutput, output and all of them return empty.
Did anybody face this kind of issue?
(Note - My operating system is Windows 7 and I am running my application on top of WAMP)
I have a Windows Azure startup task that I am trying to run for installing an MSI. I am trying to run the task via a powershell script. I can confirm that the powershell script runs when I have the following:
"Before powershell" >> C:\hello.txt
$msi = "E:\approot\bin\StartupScripts\AppInitalization_x64.msi"
if($true -eq [System.IO.File]::Exists($msi))
{
"file exists" >> C:\hello.txt
}
else
{
"file does not exist" >> C:\hello.txt
}
"After powershell" >> C:\hello.txt
If I run a deployment, my hello.txt contains "Before, file exists" and "after".
However if I try to change the file exists condition to be:
$arguments= ' /qn /l*v C:\AppInitalization_x64.txt'
Start-Process `
-file $msi `
-arg $arguments `
-passthru
The deployment hangs (i've let it wait up to five hours).
If I remote into the machine and try that same powershell snippet, it installs just fine.
Any ideas on why it would be hanging when I have this as part of the startup script?
Have you looked at AppInitialization_x64.txt to see where the Windows Installer package is hanging? That should give you a good indication, since you've already enabled verbose logging.
Can somebody please help me in executing the IISWeb.vbs commands from the c# code which I am trying to use for starting and stopping the websites on remote IIS servers (IIS 6.0).
I have tried other options like using DirectorServises with SyytemManagement or with WMI but in all cases code works fine for the local IIS server but displays error for the remote servers. I get 'Access denied' error for remote servers
I have got the all the servers in the same domian and network area and trying with a user account with administrative priviledge but nothing works for remote machine so giving it a try using IISWeb.vbs.
Please note that from my dev machine I am able to stop and start the websites on remote machines using this IISWeb.vbs commands without any problem when I run it from command prompt. So definitely there is no permission issues.
Code I am using:
Process p = new Process();
p.StartInfo.FileName = "cscript.exe";
p.StartInfo.Arguments = "iisweb.vbs /stop w3svc/87257621 test /s servername /u userid /p password";
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
int i= p.ExitCode;
Surprisingly I am not getting any error and i get return code '0' but nothing but website is not getting stopped.
Please help me out.
Figured out the problem, code was trying to find out the iisweb.vbs file inside c:\program files\Visual Studio\.........and was unable to find it out. So I amended the code to specifically look for the iisweb.vbs file at C:\Windows\System32 location and it is working fine now.
Here is the working version of code for stopping and starting a website on remote machine having IIS 6.0.
ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = #"C:\WINDOWS\system32\cscript.exe";
psi.Arguments = #"C:\WINDOWS\system32\iisweb.vbs /stop w3svc/1 test /s ServerName /u UserId /p Password";
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
//this.processResults.Text = output;
}