JAVA “Run as administrator” “UAC disabled” alternative solution

Technorati 标签: psexec,run as administrator,UAC

java.io.IOException: Cannot run program "psexec.exe": CreateProcess error=740, The requested operation requires elevation
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at com.gdn.BuildHostNow.main(BuildHostNow.java:14)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation

old script :

http://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file

update script:

http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator/28467343#28467343

@ECHO OFF
setlocal EnableDelayedExpansion

NET FILE 1>NUL 2>NUL
if ‘%errorlevel%‘ == ‘0‘ ( goto START ) else ( goto getPrivileges ) 

:getPrivileges
if ‘%1‘==‘ELEV‘ ( goto START )

set "batchPath=%~f0"
set "batchArgs=ELEV"

::Add quotes to the batch path, if needed
set "script=%0"
set script=%script:"=%
IF ‘%0‘==‘!script!‘ ( GOTO PathQuotesDone )
    set "batchPath=""%batchPath%"""
:PathQuotesDone

::Add quotes to the arguments, if needed.
:ArgLoop
IF ‘%1‘==‘‘ ( GOTO EndArgLoop ) else ( GOTO AddArg )
    :AddArg
    set "arg=%1"
    set arg=%arg:"=%
    IF ‘%1‘==‘!arg!‘ ( GOTO NoQuotes )
        set "batchArgs=%batchArgs% "%1""
        GOTO QuotesDone
        :NoQuotes
        set "batchArgs=%batchArgs% %1"
    :QuotesDone
    shift
    GOTO ArgLoop
:EndArgLoop

::Create and run the vb script to elevate the batch file
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "cmd", "/c ""!batchPath! !batchArgs!""", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B

:START
::Remove the elevation tag and set the correct working directory
IF ‘%1‘==‘ELEV‘ ( shift /1 )
cd /d %~dp0

::Do your adminy thing here...

psexec.exe -s cmd.exe /c dir >d:\output2.log 2>error2.log
@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if ‘%errorlevel%‘ NEQ ‘0‘ (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------put your command below

psexec.exe -s cmd.exe /c dir >d:\output.log 2>error.log
时间: 2024-10-13 22:52:34

JAVA “Run as administrator” “UAC disabled” alternative solution的相关文章

Tomcat – java.lang.OutOfMemoryError: PermGen space Cause and Solution

Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ixzz3QDWa3Zqi Tomcat web server often suffers from java.lang.OutOfMemoryError: PermGen space whenever you deploy and undeploy your web application coupl

java.security.cert.CertificateException: No subject alternative names present

背景:在开发一个项目中,要调用一个webservice服务,之前设置的是http协议,项目中采用jdk自带的wsimport工具生成的客户端代码; 后来,需求变更要求兼容https协议的webservice,开始以为只需要简单的将服务地址的连接改为https就可以了:但不行,总是报错 javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names

To Run as Administrator in WPF

WPF以管理员身份运行 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录.系统注册表等可能影响系统正常运行的设置进行写操作.这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要以 Administrator 的方式运行,如何实现这样的功能呢? 启用ClickOnce安全设置 首先打开解决方案资源

java编译命令工具javac

Reads Java class and interface definitions and compiles them into bytecode and class files. Synopsis javac [ options ] [ sourcefiles ] [ classes] [ @argfiles ] Arguments can be in any order: options Command-line options. See Options. sourcefiles One

Architecture options to run a workflow engine

This week a customer called and asked (translated into my own words and shortened): “We do composite services, orchestrating two or three CRUD-Services to do something more useful. Our architects want to use your workflow engine for this because the

Java Performance Optimization Tools and Techniques for Turbocharged Apps--reference

Java Performance Optimization by: Pierre-Hugues Charbonneau reference:http://refcardz.dzone.com/refcardz/java-performance-optimization Java is among the most widely used programming languages in the software development world today. Java applications

LeetCode 004 Median of Two Sorted Arrays - Java

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

Java Unit Testing - JUnit & TestNG

转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignificant programming notes...   |   HOME TABLE OF CONTENTS (SHOW) Java Unit Testing -  & TestNG 1.  Introduction to Unit Testing Framework The various type o

SWIG - 同Java的混合编程

同Java的混合编程 - SWIG 最后更新日期:2014-04-20 阅读前提:推荐已经阅读<同C#的混合编程_SWIG入门>, 有Eclipse下编写JavaProject的经验. 作者: Kagula 环境:Windows 8.1 64bit(英文版).VisualStudio 2013 Update1(英文版).SWIG Win 3.0.0 .JDK 1.6.0_45 64bit.eclipse-jee-kepler-SR1-win32-x86_64 正文: 新建C++工程用于测试Ja