In the other answer here Jason rightly notes that runas /netonly does pas support saving le credentials, et Microsoft intentionally made it hard to use runas avec a hard-coded password (from a batch script).
The suggestion to use the Windows Credential Manager that Stefano pointed to in leur comment is utile quand you want to always connect to le given service (i.e. myserver.mycompany.com:XXX) using le specified credentials.
For a command-line solution, avec behavior similaire to runas.exe, mais sans having to type le password, J'ai trouvé le RunAs powershell module (which seems to implement this advice) très useful:
-
Install depuis PowerShell Gallery by running le suivant in an elevated PowerShell prompt (requires Powershell v5 ou Windows 10):
Install-Module RunAs
-
Encrypt le password by running:
# change `domain\username` as needed:
ConvertFrom-SecureString (Get-Credential 'domain\username').Password
Cela va prompt you for le login et le password et print a long hexadecimal number, qui you'll have to copy.
-
Now you can do le equivalent of runas /netonly using:
Import-Module RunAs
# replace xxxx below with the encrypted password from step 2 (and `domain\username` too)
# you might want to put this into your profile: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7
$mycreds = New-Object Management.Automation.PSCredential('domain\username', (ConvertTo-SecureString 'xxxx'))
runas -netonly $mycreds "c:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE"
P.S. beaucoup de resources on le net suggest using psexec to run as a différent user. After looking up how that works under the hood, Je ne believe it to be a viable alternative to runas /netonly