Running a PowerShell script as administrator without typing in passwords

旧街凉风 提交于 2019-11-29 08:57:52

You can start a new, elevated PowerShell process to run your script e.g.:

Start-Process PowerShell -verb runas -ArgumentList '-noexit','-File','path-to-script'

If you don't want the PowerShell window to hang around then get rid of the '-noexit' but for debugging the launch of your script, it is useful.

If you had access to an admin account username/password, you could do this:

# Capture encrypted password once and store to file
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd
$encpwd > $path\password.bin

# Afterwards always use this to start the script
$encpwd = Get-Content $path\password.bin
$passwd = ConvertTo-SecureString $encpwd
$cred = new-object System.Management.Automation.PSCredential 'domain\username',$passwd
Start-Process PowerShell -Cred $cred -ArgumentList '-noexit','-File','path-to-script'   
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!