Board logo

标题: [原创代码] 借助powershell提升操作权限 [打印本页]

作者: yiwuyun    时间: 2015-11-22 17:55     标题: 借助powershell提升操作权限

  1. if ($true){}# == ($true){}# goto ___yiwuyun
  2. <#BeginBatOperation#
  3. :___yiwuyun
  4. @echo off&setlocal&cls
  5. if "%~1"=="true" (
  6. (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  7. call :MainBatOperationAsAdmin
  8. ) else if "%~1"=="false" (
  9. (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  10. ) else (
  11. call :MainBatOpertionAsUser
  12. (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  13. )
  14. exit/b 0
  15. :MainBatOperationAsAdmin
  16. echo 以管理员权限运行的批处理操作
  17. timeout /T 10
  18. exit/b 0
  19. :MainBatOpertionAsUser
  20. echo 以普通用户运行的批处理操作
  21. timeout /T 10
  22. exit/b 0
  23. #EndBatOperation#>
  24. <#StartPowerShell#>
  25. Function RunAsAdmin{
  26. $AdminPrivilege=New-Object -ComObject "Shell.Application";
  27. $AdminPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName true",0,"runas",1);
  28. }
  29. Function RunAsUser{
  30. $UserPrivilege=New-Object -ComObject "Shell.Application";
  31. $UserPrivilege.ShellExecute("cmd.exe","/c $yiwuyun_fileName false");
  32. }
  33. Function RunPowerShellMainProgramAsAdmin{
  34. "以管理员权限运行的PowerShell程序";
  35.   sleep 5;
  36. }
  37. Function RunPowerShellMainProgramAsUser{
  38. "以普通身分运行的PowerShell程序";
  39.   sleep 5;
  40. }
  41. ###PowerShell主框架开始
  42. if($BoolUser){
  43.   RunPowerShellMainProgramAsUser;
  44.   sleep 5;
  45.   RunAsUser;
  46. }else{
  47.   if($BoolAdmin){
  48.     RunAsAdmin;
  49.   }else{
  50.     RunPowerShellMainProgramAsAdmin;
  51.   }
  52. }
  53. <#EndPowerShell#>
复制代码

作者: yiwuyun    时间: 2015-11-22 21:17

同步互联网时间。
  1. if ($true){}# == ($true){}# goto ___yiwuyun
  2. <#BeginBatOperation#
  3. :___yiwuyun
  4. @echo off&setlocal&cls
  5. if "%~1"=="true" (
  6. (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$false&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  7. call :MainBatOperationAsAdmin
  8. ) else if "%~1"=="false" (
  9. (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$false&echo $BoolAdmin=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  10. ) else (
  11. call :MainBatOpertionAsUser
  12. (echo $yiwuyun_fileName="%~f0"&echo $BoolUser=$true&echo $strPath="%~dp0"&type "%~f0")|powershell -command -
  13. )
  14. exit/b 0
  15. :MainBatOperationAsAdmin
  16. rem echo 以管理员权限运行的批处理操作
  17. rem timeout /T 5
  18. exit/b 0
  19. :MainBatOpertionAsUser
  20. rem echo 以普通用户运行的批处理操作
  21. rem timeout /T 5
  22. exit/b 0
  23. #EndBatOperation#>
  24. <#StartPowerShell#>
  25. Function RunAsAdmin{
  26. $AdminPrivilege=New-Object -ComObject "Shell.Application";
  27. $AdminPrivilege.ShellExecute("cmd.exe","/c `"$yiwuyun_fileName`" true",0,"runas",1);
  28. }
  29. Function RunAsUser{
  30. $UserPrivilege=New-Object -ComObject "Shell.Application";
  31. $UserPrivilege.ShellExecute("cmd.exe","/c `"$yiwuyun_fileName`" false");
  32. }
  33. Function GetNetTime{
  34. $time=New-Object -ComObject "Microsoft.XMLHTTP";
  35. $time.open("get","http://open.baidu.com/special/time/",$False);
  36. $time.send();
  37. $CurrentDateTime=Get-Date;
  38. while($True){
  39.    if($time.statusText -eq "OK"){break;};
  40.    if($CurrentDateTime+"00:00:05" -lt $(Get-Date)){"timeout! exit";exit;};
  41. }
  42. $Content="";$time.responseText| %{$Content+=$_;};
  43. $Content=$Content -split "`n";
  44. $time=($Content|Select-String -Pattern "window.baidu_time\(" -CaseSensitive);
  45. $time=($time -split "\(|\)")[1];
  46. $CurrentTime="1970-1-1 08:00:00".ToDateTime($Null).AddMilliseconds($time.ToDecimal($Null));
  47. $CurrentTime;
  48. }
  49. Function RunPowerShellMainProgramAsAdmin{
  50. Set-Date -Date $(GetNetTime);
  51. }
  52. Function RunPowerShellMainProgramAsUser{
  53. ##以普通用户身分运行的PowerShell程序
  54. }
  55. ###PowerShell主框架开始
  56. if($BoolUser){
  57.   RunPowerShellMainProgramAsUser;
  58.   #sleep 5;
  59.   RunAsUser;
  60. }else{
  61.   if($BoolAdmin){
  62.     RunAsAdmin;
  63.   }else{
  64.     RunPowerShellMainProgramAsAdmin;
  65.   }
  66. }
  67. <#EndPowerShell#>
复制代码

作者: batps    时间: 2015-12-13 12:04     标题: 标题

这是两段很有用的代码,正在学习。
作者: sofood    时间: 2019-10-25 17:05

谢谢无私奉献,分享成果!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2