Build Events, permission when run a script - c#

I have a .bat script with I configure to run in my post-build event call $(ProjectDir)scripts\install.bat, this script to install a service in my local machine.
When I open a cmd and run as administrator, this script works, but if I run in normal cmd doesn't work. Exist some way I run call $(ProjectDir)scripts\install.bat in administrator mode in visual studio in post-build event command line or someone other way to script works in normal command line.
Note: I have administrador privileges

Assuming this is Windows 7 or later:
Even though you have administrator privileges on the machine, you still need to run Visual Studio with administrative privileges to do things that a normal user can't do, because of UAC. Right-click on your VS icon, and select "Run as Administrator".

Maybe a few years too late, but here is the solution for anyone still searching.
Just add this to the top of your batch file and it will ask UAC for admin rights before executing the rest of the script.
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)

Related

c# winforms Application, run a cmd command as system

I have written a program in the last days which automatically changes the proxy server. This runs as a Windows Forms Tray Application on the user level.
Now came a request which caused me some problems. A cmd command "ipconfig /registerdns" should be executed but this requires higher permissions.
I'm now looking for a way to easily execute this command in normal user context. Is there a way to run this one command as a system? Or any other possibility?
Or is there another function to re-register the dns without admin priviledge?
I hope there is a easy way.
Thank you very much!
As i know admin processes can only be created by another admin processes but still you can try adding this parameter to your process's start info
StartInfo.Verb = "runas";
if it doesnt work you set your application to run always as admin by adding app.manifest to your project and editing line 19 as below.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
Create a script (.cmd or .bat) containing required command (here: "ipconfig /registerdns"), then flag it as "Run as administrator". Finally, run this script from your app instead of original command.
This should help in flagging as "Run as administrator": How to code a BAT file to always run as admin mode?
Okay so I found no solution for my former problem, because it is not possible to launch a elevated command from a user Application. But I found a powershell command: Register-DnsClient. This command does the same and does not need elevated priviledges. Only downside is it works only on Windows 8.1 and newer. So Windows 7 is not supported.

Can't open the service control manager despite running command prompt as administrator

I created a service in C# which generated an executable "GodzillaService.exe". The service itself is just a basic template generated EXACTLY according to this link, other than the name: https://www.c-sharpcorner.com/article/create-windows-services-in-c-sharp/
The issue comes when I try to run InstallUtil. Per the article above, I execute
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
on an elevated command prompt, run as administrator. I then type in
InstallUtil.exe C:\Path\To\My\EXE\Godzilla.exe
and I get hit with this error:
An exception occurred during the Install phase.
System.InvalidOperationException: Cannot open Service Control Manager
on computer '.'. This operation might require other privileges. The
inner exception System.ComponentModel.Win32Exception was thrown with
the following error message: Access is denied.
Looking at the error message, experience tells me that I need to grant access to something or someone, but what? I'm already running the program as an administrator. I've tried these things so far:
Run command prompt as administrator.
Run Developer Command Prompt for VS 2017 as administrator.
Log out of my user account, log in to my admin account, and try running it.
Move the GodzillaService.exe file into Program Files directory in case applocker is messing with it.
Run VS 2017 as admin, rebuild the exe file, then try it.
Try using the full path of both the exe file and InstallUtil.
Made sure my admin account was in the local admin group.
Tried looking at Event Viewer for more information (did not find any logs related to this issue, maybe I'm looking in the wrong place?)
Tried changing the account in the service program from LocalSystem to User, LocalService, and to NetworkService
I'm fresh out of ideas... Does anyone have any suggestions for what I can do to further troubleshoot this problem? I appreciate any advice given!
It took me a while, but I was able to finally figure this out! The problem was that even though I said I was running it as an administrator, I was running it as a user with administrator privileges. Completely different.
In my environment (which is a domain environment), for some reason we have the "Run as Administrator" option blocked for running programs. Thus, to run things as an administrator, we actually have to run it as a different user, and then select our admin account. This works for 99% of things, but not for this case.
The solution was that I had to log completely out of my regular user account and log in with my administrative account. Then, I had to right click on command prompt and Run As Administrator. Upon doing this, I no longer received the error!

is it appropriate to implement auto update for a c# application just by passing download url to msiexec

I have a project in c# which requires auto update functionality, since it has quite a lot of dependencies and runs a Windows service, ClickOnce installer is not suitable for this work. So I created a setup project for this application. Now I want to include an auto update feature.
My auto-update strategy is:
From the main application, check for updates every morning.
If an update is found, get the msi link
Application passes control to the msiexec and quits
Ex.
msiexec /i http://www.example.com/share/package.msi /L*V "C:\log\example.log"
Since my application is running on administrator account UAC prompt will not occur.
Are there any pitfalls that I haven't seen? Or there any superior way to do auto update gracefully?
Note: The setup project of the application is marked with the property RemoveAllPreviousVersion = True so it will take care of the uninstallation of previous versions.
A couple of comments/answers:
"Note: The setup project of the application is marked with the property RemoveAllPreviousVersion = True so it will take care of the uninstallation of previous versions."
But only if you increment the Version property of the setup project, accept the change in ProductCode, keep the UpgradeCode the same, ensure that the value of EveryOne or Just me is the same in the older product and the upgrade, and increase the file versions of files you need to update.
It's also not obvious to me that you won't get a UAC prompt because "running on an administrator account" does not mean the same as "running with administrator privilege". If the running process is not running elevated a prompt will occur, because UAC means that even administrators are running limited unless explicitly elevated. Even if you are elevated, calling msiexec as an external shell call will not result in msiexec being elevated. A shell execute does not transfer privilege to the offspring process. The other thing to consider is whether it's the current interactive user that is effectively doing this update, because there may be an issue trying to show UI if another user owns the desktop.
Anyway in general it's ok to do that, it's quite common, and signing the MSI would be more secure. Also, Windows Installer will sometimes prompt a repair, so that install URL needs to remain valid. A better approach would be for you to download the MSI to a location on the client system that you own and prompt the user to run it, or you run it - it's more friendly to offer that than pull the rug out and just do it.

Start WPF Application with RunAs Prompt

So I have a WPF application and here is what I want it to do. I can right click on the executable, select Run As Different user, and get the Windows Security box with User Name and Password, as well as the Smart Card logon. I was wondering if there was a way to launch the Sysinternals Run as Different User to come up automatically, without having to right click and select Run As Different User. Any thoughts?
I was able to go into the Assembly Manifest and force it to run as administrator, but that isn't what I need. I really need the option to run it with Smart Card credentials. Thank you in advance!
AFAIK, the only way to do it that works is by using the RunAs exe as follows. The /savecred will cache credentials, but your user will still have to enter it the very 1st time (which might be annoying for your use-case).
runas.exe /savecred /user:<localmachinename>\administrator "path to your WPF exe"
or, if you're on a domain:
runas.exe /savecred /user:<DomainName>\<AdministratorAccountName> "path to your WPF exe"
Example: To Run Visual Studio 2012 in Admin Mode, I tried the following and it worked. I was prompted to enter my password just once, for the 1st time.
runas.exe /savecred /user:<My_DomainName>\<My_AdministratorAccountName> "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"
Further Reading: Runas Documentation.

How to run application as administrator in debug with Visual Studio?

I have a c# application where I have to have read/write access to the root of the C drive. I realize I can compile the code and run the executable as administrator and it works. But I need to debug it and I am unsure as to how one would start the app within Visual Studio.
I have tried adding:
<requestedExecutionLevel level="asInvoker" uiAccess="true" />
to my manifest but I still get access denied error.
Here is the line of code that fails:
MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(#"c:\somemapnamefile.data", System.IO.FileMode.OpenOrCreate, "somemapname", 1000);
For now I have a work around but I'd like to know for the future.
Just run visual studio itself as an administrator. Any program you debug from there will also be run as an administrator.
VS must be run with admin right. however, a more elegant way is in the requiredExecutionLevel in manifest should set to 'requireAdministrator'.
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
When you open the project and try to debug, the VS2012 will warn about the admin right and restart itself to admin right. And also the exe file will be marked as requiring admin right at the first place therefore when deploy you don't need to configure admin right requirement in file properties.
You can also set this administrator option automatically:
To answer the question in your title, you can just select Run as Administrator from the context menu when starting VS.
Now the checked answer will not working.
You should find an option for this in project properties Linker -> Manifest File -> UAC Execution Level. Set this to requireAdminstrator.
This will cause the default generated manifest to include the requestedExecutionlevel that you need, so that your users will be prompted automatically to elevate their privileges if they are not already elevated.
The "This task requires the application to have elevated permissions" error occurs because of The current user didn’t have a sufficient privilege to open Visual Studio.
As a temporary solution
You can overcome this issue by right-clicking on visual studio and select run as administrator at every time you intend to open it
As a permanent solution,
You can check the compatibility troubleshooting
Right, Click on Visual Studio > select Troubleshoot compatibility.
Select Troubleshoot Program.
Check The program requires additional permissions.
Click on Test the program.
Wait for a moment until the program launch. Click Next.
Select Yes, save these settings for this program.
For the detail steps with images, please check Visual Studio requires the application to have elevated permissions

Categories