标题: [原创代码] 借助powershell提升操作权限 [打印本页]
作者: yiwuyun 时间: 2015-11-22 17:55 标题: 借助powershell提升操作权限
- if ($true){}# == ($true){}# goto ___yiwuyun
- <#BeginBatOperation#
- :___yiwuyun
- @echo off&setlocal&cls
- if "%~1"=="true" (
- (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- call :MainBatOperationAsAdmin
- ) else if "%~1"=="false" (
- (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- ) else (
- call :MainBatOpertionAsUser
- (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- )
- exit/b 0
-
- :MainBatOperationAsAdmin
- echo 以管理员权限运行的批处理操作
- timeout /T 10
- exit/b 0
-
- :MainBatOpertionAsUser
- echo 以普通用户运行的批处理操作
- timeout /T 10
- exit/b 0
-
- #EndBatOperation#>
-
-
- <#StartPowerShell#>
-
- Function RunAsAdmin{
- $AdminPrivilege=New-Object -ComObject "Shell.Application";
- $AdminPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName true",0,"runas",1);
- }
-
- Function RunAsUser{
- $UserPrivilege=New-Object -ComObject "Shell.Application";
- $UserPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName false");
- }
-
- Function RunPowerShellMainProgramAsAdmin{
- "以管理员权限运行的PowerShell程序";
- sleep 5;
- }
-
- Function RunPowerShellMainProgramAsUser{
- "以普通身分运行的PowerShell程序";
- sleep 5;
-
- }
-
- ###PowerShell主框架开始
- if($BoolUser){
- RunPowerShellMainProgramAsUser;
- sleep 5;
- RunAsUser;
- }else{
- if($BoolAdmin){
- RunAsAdmin;
- }else{
- RunPowerShellMainProgramAsAdmin;
- }
- }
-
- <#EndPowerShell#>
复制代码
作者: yiwuyun 时间: 2015-11-22 21:17
同步互联网时间。- if ($true){}# == ($true){}# goto ___yiwuyun
- <#BeginBatOperation#
- :___yiwuyun
- @echo off&setlocal&cls
- if "%~1"=="true" (
- (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- call :MainBatOperationAsAdmin
- ) else if "%~1"=="false" (
- (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- ) else (
- call :MainBatOpertionAsUser
- (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
- )
- exit/b 0
-
- :MainBatOperationAsAdmin
- rem echo 以管理员权限运行的批处理操作
- rem timeout /T 5
- exit/b 0
-
- :MainBatOpertionAsUser
- rem echo 以普通用户运行的批处理操作
- rem timeout /T 5
- exit/b 0
-
- #EndBatOperation#>
-
-
- <#StartPowerShell#>
-
- Function RunAsAdmin{
- $AdminPrivilege=New-Object -ComObject "Shell.Application";
- $AdminPrivilege.ShellExecute("cmd.exe","/c `"$yiwuyun_fileName`" true",0,"runas",1);
- }
-
- Function RunAsUser{
- $UserPrivilege=New-Object -ComObject "Shell.Application";
- $UserPrivilege.ShellExecute("cmd.exe","/c `"$yiwuyun_fileName`" false");
- }
-
- Function GetNetTime{
- $time=New-Object -ComObject "Microsoft.XMLHTTP";
- $time.open("get","http://open.baidu.com/special/time/",$False);
- $time.send();
- $CurrentDateTime=Get-Date;
- while($True){
- if($time.statusText -eq "OK"){break;};
- if($CurrentDateTime+"00:00:05" -lt $(Get-Date)){"timeout! exit";exit;};
- }
- $Content="";$time.responseText| %{$Content+=$_;};
- $Content=$Content -split "`n";
- $time=($Content|Select-String -Pattern "window.baidu_time\(" -CaseSensitive);
- $time=($time -split "\(|\)")[1];
- $CurrentTime="1970-1-1 08:00:00".ToDateTime($Null).AddMilliseconds($time.ToDecimal($Null));
- $CurrentTime;
- }
-
- Function RunPowerShellMainProgramAsAdmin{
- Set-Date -Date $(GetNetTime);
- }
-
- Function RunPowerShellMainProgramAsUser{
- ##以普通用户身分运行的PowerShell程序
- }
-
- ###PowerShell主框架开始
- if($BoolUser){
- RunPowerShellMainProgramAsUser;
- #sleep 5;
- RunAsUser;
- }else{
- if($BoolAdmin){
- RunAsAdmin;
- }else{
- RunPowerShellMainProgramAsAdmin;
- }
- }
-
- <#EndPowerShell#>
复制代码
作者: batps 时间: 2015-12-13 12:04 标题: 标题
这是两段很有用的代码,正在学习。
作者: sofood 时间: 2019-10-25 17:05
谢谢无私奉献,分享成果!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |