Windows script opening cmd and running a command - windows

So i'm trying to write a script which checks if the cmd is with administrative privileges. If it is I just ran the command - which is a jar file. If it isn't what I did was create a link to the cmd and in that link I made it always run as administrator.
The problem is that after it runs the command it opens the second cmd as administrator but then the command needs to be written again. Is there any chance to save the command written and as the second cmd opens run it?
net session >nul
if %errorLevel% == 0 (
java -jar %~dp0/myjar.jar%*
) else (
echo Administrative permissions required
cmdadmin.lnk
)
cmdadmin.lnk is as I said a link to cmd which in the advanced properties I set as run as administrator.
Now what I want is to pass a command when opening the link. Is it possible?

Related

Run script on Windows Container as administrator

I am writing a Dockerfile on top of mcr.microsoft.com/windows/servercore which is supposed to run a script in administrator mode and then start CMD. The CMD shell should not be elevated. How do I achieve the same?
If I add USER ContainerAdministrator to my Dockerfile, the first script is run without any issues but the CMD shell is then elevated.
The other option might have been to use USER ContainerAdministrator and then spawn CMD with runas /user:ContainerUser, but runas needs password to be entered via a prompt (or using /savecred but that won't work here), so it's not possible either.
Thanks in advance!

Batch - check if given process is running with admin privileges

I'm writing a batch script, where I need to check if a specific process is run with admin privileges and I have trouble finding a proper command to do so. Tasklist command does not give me that information.
Is there any way to get info about privileges of given process (not cmd.exe, but any process) with a command (and possibly without 3rd party software)?
Edit:
I don't need to determine which account has launched the process, nor do I need to check if cmd.exe process is running with admin privileges. I need to check if a given process is running with admin privileges or not, because later i want to call program which needs to have the same privileges as a given process.
Admin is easily confirmed by calling a PowerShell script from a .bat file script. Place these two (2) files in the same directory. Running Confirm-Admin.bat will return True in the output and 1 as the ERRORLEVEL if the process is being run as admin. If not being run as admin, it will return False as the output and zero (0) as the ERRORLEVEL.
=== Confirm-Admin.ps1
function ExitWithCode($exitcode) {
$host.SetShouldExit($exitcode)
exit $exitcode
}
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Output "True"
ExitWithCode 1
} else {
Write-Output "False"
ExitWithCode 0
}
=== Confirm-Admin.bat
powershell -NoLogo -NoProfile -File "%~dp0%~n0.ps1"
To run Confirm-Admin.ps1 in PowerShell, invoke another Powershell to run it.
C:>powershell -NoLogo -NoProfile -File "Confirm-Admin.ps1"
True
PS 10:59 C:\src\t
C:>$LASTEXITCODE
1
If you only want to stay in the cmd.exe world, see https://stackoverflow.com/a/11995662/447901
Okay, so after some research and experimenting, I've managed to partially solve my problem.
Firstly, I check if batch script is running with admin privileges:
fsutil dirty query %SYSTEMDRIVE% >nul
if %errorLevel% == 0 (
set isBatchElevated=true
) else (
set isBatchElevated=false
)
Then, I look for a specific process by getting list of all tasks running by current user and finding a process by name:
tasklist /fi "username eq %USERDOMAIN%\%USERNAME%" | find /i "processname" > nul
if errorlevel 1 (
:: batch script doesn't have admin privileges, but the process has
)
So, there are four possible scenarios of running script and process with or without admin privileges.
1) Batch script without admin privileges, process with admin privileges
Running tasklist command won't find the given process with elevated rights and will set exit code to 1. Therefore, you can be 100% sure, that the process is running with elevated privileges, but only if batch script is running without admin privileges.
2) Batch script without admin privileges, process without admin privileges
Running tasklist command will find the given process. Basing on the 1) outcome, you can be 100% sure, that the process is running without elevated privileges, but only if batch script is running without admin privileges.
3)/4) Batch script with admin privileges, process with/without admin privileges
Those are problematic scenarios. When the script is running with elevated privileges, then the given process will be found, but there would be no difference between process with and without elevated rights when running tasklist.
After the privileges check I need to run a program with the same rights as the given process. The difference in the privileges will cause an error and the given won't be running without admin privileges only, so running the batch script without admin rights only won't solve the issue.

Task Scheduler Action parameter should generate log, but doesn't

As the title suggests, I have an added parameter in my Task Scheduler Actions that logs stdout and stderr to a log.txt file. The logging works when the action is run through the command prompt, but not when the action is run by the actual Task Scheduler (at its specified time). Task scheduler reports the action runs successfully, but I can't be sure it does because there's no logging:)
Command looks like this
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
I'm unfortunately not a native Windows user, so any help would be appreciated. I'm running Windows Server 2008 R2 Enterprise.
Thanks!
Cmd.exe handles command redirection. You have to run it under cmd.exe. Powershell probably also can do redirection but in your script (.NET can).
A black window just means a console program is running. Only if cmd is running does cmd features become available. By starting cmd or by putting it in a batch you canget redirection from cmd.
cmd /c powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1
See for Help
cmd /?
Place the command you listed in a batch file and then schedule the batch file.
If you are doing so and it fails, then try it with your account credentials as authentication in task scheduler, to see if it is a permissions issue.
#echo off
powershell.exe -file "D:\Scripts\TimeSync2.ps1" > "D:\Scripts\timeSync_log.txt" 2>&1

Windows: Batch File: Only Run as Administrator

I have a batch file to start an application as a Windows service. It is called start.bat
#ECHO off
START c:\Ruby193\bin\ruby c:\Ruby193\bin\thin start -R c:\coolapp\config.ru -p 4321 -a localhost -e production
My challenge is that this program only runs properly if it is "Run as Administrator" with admin privileges. So, I would like to add a line to check if this script is actually run with administrative privileges, and only execute if it is being run as administrator.
How can I do that from within the script?
Something like this might be what you need:
set isadmin=0
whoami /all | findstr /c:" S-1-16-12288 ">nul && set isadmin=1
That should result in the %isadmin% variable being either 1 or 0 depending on whether the shell was run as administrator or not.
This assumes the existance of the whoami utility which won't necessarily be available on older versions of Windows - I believe it was included from Windows Vista onwards though.
Two options:
Provoke elevation from a WSH script, like documented in the blog post Scripting Elevation on Vista.
Use an external executable that provokes the UAC prompt, such as Elevate32.exe/Elevate64.exe.
For your scenario, #2 may be preferable because you can detect whether the elevation prompt was canceled (exit code 1223) and you can also wait for the launched executable to finish before continuing (-w parameter).
Bill
It would probably be easier to convert the script to VBScript, then you can more easily check for Admin privileges and even elevate the script to Admin.
See here for how to do the check in VBScript: VBScript: Check if the script has administrative permissions

start external cmd prompt and update it concurrently with java application

I want to be able to run an external cmd prompt concurrently with my Java code. While running java application need to start external cmd prompt ,and this command prompt need to updated from my java application as well as i need to get values from this command prompt into java application.Please provide suitable suggestion
Try this
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c start command");
UPDATION 2
To execute commands try this
rt.exec("cmd.exe /c start cmd.exe /k \"ping localhost\"");

Resources