Trying to run a powershell script in C# - c#

I've got a PS script that adds firewall rules.
I'm using Process.Start("powershell.exe", "-File "+"\"C:\Program Files (x86)\blah\blah\add-FirewallRule.ps1\"");
This command works fine on my local machine, but in testing I've been running the program on a Windows Server 2008 R2, and the PS script won't run. It runs and works fine if I run it manually from the command line, but my code won't work. All the file paths are correct (i've already checked that and had a co worker check it). When I run the code the PS console doesn't even pop up.
Any suggestions?

Since you are in C#, I'd recommend using the PowerShell Invoke API rather than Process.Start. The API is easy to use and hopefully will give you a better indication of what is going wrong.

Turns out that I wasn't accounting for the automatic redirect caused by running a 32-bit app on a 64-bit system. So all I had to do was go in and set the appropriate execution policy for the 32 bit version of powershell.

Have you tried running as Admin? I believe Process.Start requires elevated privileges that are not available in, say, sandbox mode.

Related

C# Run my visual studio executable as administrator

Hi i have created my visual studio program and if i run it as normal it works perfectly. But the problem comes in when i create a setup installation for this program. It successfully installs my program but i cannot get the program to run as administrator and thus my sql statements wont work. Any suggestions on how to fix this problem?
It is hard to pinpoint the fault but I have a feeling you have asked an XY problem question where running with and without elevated privileges (often wrongly called administrator). Making an installation program an installing probably has nothing to do with it. Maybe you are starting VS with elevated privileges without thinking about it?
Try to start the program directly and not through VS. Find the output folder (yourproject\bin\debug\yourprogram.exe) and run it. Does it work properly? Then it works.
Now try to start it with elevated privileges (admin) (ctrl-shift-enter or ctrl-shift-doubleclick). Does it work as it should? If it does, your problem has nothing to do with privileges.
So then you might have side effects by the installation. An installed file is in another directory. Do you have hard coded relative paths?

Error while trying to run project: Unable to start program. The request is not supported

I'm having problems launching an application with VS2017 on Windows 10. Starting in Debug mode, a popup apperars with the message:
Error while trying to run project: Unable to start program ''.
The request is not supported.
If I launch without debug, it runs. If I run it and then attach to the process, it attaches. If I run with administrative privileges, then it debugs (but I can't develop with administrative privileges). I'm owner of the folder where is source code and where project is built.
It works like a charm on previous PC with Windows 7.
Any idea?
You need to check "Use Managed Compatibility Mode" in Tools|Options|Debugging|General.
In the end, i found the solution. The problem is ConEmu set in "Aggressive mode" and as default terminal: it capture my application execution and do some unknown interference.
I removed that setting, and my app work like a charm.
I discovered this issue in Visual Studio 2017, and I found that excluding my code folders from my virus scanner (MalwareBytes) solved the problem in my case.
In my case this was the result of my virus software quarantining the executable as "Malware" because I had used a component in it that connected to the internet. Once I listed my project folder as excluded from malware detection everything ran just fine.

Powershell script runs fine on one computer but errors out on another

I wrote a PowerShell script that calls a C# exe file that works no problem on my machine and a couple other machines on our network, but for some reason it won't run on the computer it needs to. This is the error I get when I try and run it. Any ideas on what could be causing this?
Start-Process : This command cannot be executed due to the error: %1 is not a valid Win32 application.
It will run no problem on a couple other machines on the same network no problem, just not on this particular one. Any input would be helpful, thank you!
It sounds like your C# application was compiled as 64-bit, and you're trying to run it on a system with Windows 32-bit. 64-bit applications will not run on 32-bit operating systems.
Rebuild the C# application with either AnyCPU or Win32 as the target platform. You can do this through the Project menu; edit the properties of your application, go to the Build page, and change the Platform in the drop-down list at the top of that page, and then build your project.
Can't be sure, but I think your app is 64bit compiled, and you're trying to run it on a 32bit machine.

Execute C# code on Centos (Linux)?

I have simple C# console application developed in Visual Studio 2008, which writes information in MySQL database. Is it possible to run the application in my hosting environment (Cent OS, with local MySQL database)? I have only control panel or ftp access to my hosting space. The database is only accessible from local (from the hosting space).
I have read something about Mono/Monodevelop, but those need to be run on the Cent OS, right? I don't have access to install this or other staffs on the hosting environment.
Given what you've specified about the environment and your limitations, not you can't run it.
You will need to have Mono installed to run a .NET app on Linux. If you can't install it, you're probably out of luck.
You could check with your hosting provider to see if they have other options.
If your hosting provider allows it, has mono installed, and you've verified that your application runs under mono you may be able to invoke the program via a shell command from php.
<?php
$output = shell_exec('mono myprogram.exe');
echo "<pre>$output</pre>";
?>
If your host has mono installed, and you have the ability to execute arbitrary shell commands from PHP, then you can run it. Otherwise no. Do you have the source to the program? If so, I would just port the code to PHP (or whatever your host provides).

ASP.NET failing to call an unmanaged dll after initial request

So I have an unmanaged C++ dll which I am calling from my ASP.NET application, it has a single entry point and a couple of structures for passing data. If I create a C# console app to call the dll it works fine. If I hook it in to my asp.net app running on my local WinXP machine (IIS 5.1) then it works fine.
When I publish it to our development environement which is running Windows 2003 and IIS 6 then the first 1 or 2 calls works fine but then it simply stops responding. I'm getting no error messages, warnings etc... but I am fast running out of hair!
I've set the virtual directory which runs the asp.net app up inside of its own application pool but this seems to have had no impact. Any help would be greatly appreciated.
Cheers
What do you mean when you say that it stops responding? You could try putting logging statements that would trace the input/output around the call to see where exactly it blocks. Also make sure you check the server Event Log where unhandled ASP.NET errors are written. As a last resort you could install Visual Studio remote debugging tools (msvsmon.exe) on the server and step through the code by attaching to the corresponding w3wp process. Actually the debugging tools doesn't require installation, a simple copy is enough (C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Remote Debugger).
The failure appears actually to be with the C++ code itself, it has some code to handle locking for a threaded environment and this appears to be failing. Not sure why it works on my local machine and not the dev environment but that's another question.
Thanks all

Categories