回答自动打开管理员模式的powershell窗口执行命令


方法一:
$strPass =”mypassword”
$secPass = $strPass| ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object system.management.automation.pscredential(“administrator”,$secPass)
Start-Process powershell -ArgumentList “-file c:/hello.ps1″ -Credential $cred
这种方法不支持远程。
方法二:
$strPass =”mypassword”
$secPass = $strPass| ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object system.management.automation.pscredential(“administrator”,$secPass)
$s= New-PSSession -ComputerName 192.168.1.100 -Credential $secPass
invoke-command -session $s -scriptblock {Get-Wuinstall}
这种方法是我常用的远程执行命令方法,必须在两段配置好PS远程才可以使用。

×用微信扫描并分享