The problem with Get-Credential
is that it will always prompt for a password. There is a way around this however but it involves storing the password as a secure string on the filesystem.
The following article explains how this works:
Using PSCredentials without a prompt
In summary, you create a file to store your password (as an encrypted string). The following line will prompt for a password then store it in c:mysecurestring.txt
as an encrypted string. You only need to do this once:
read-host -assecurestring | convertfrom-securestring | out-file C:mysecurestring.txt
Wherever you see a -Credential
argument on a PowerShell command then it means you can pass a PSCredential
. So in your case:
$username = "domain01admin01"
$password = Get-Content 'C:mysecurestring.txt' | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $username, $password
$serverNameOrIp = "192.168.1.1"
Restart-Computer -ComputerName $serverNameOrIp `
-Authentication default `
-Credential $cred
<any other parameters relevant to you>
You may need a different -Authentication
switch value because I don't know your environment.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…