암호를 묻지 않고 PowerShell 자격 증명 사용
도메인에 속한 원격 컴퓨터를 다시 시작하고 싶습니다. 관리자 계정이 있지만 powershell에서 사용하는 방법을 모르겠습니다.
Restart-Computer
cmdlet이 있고 자격 증명을 전달할 수 있다는 것을 알고 있지만 도메인이 예를 들어 mydomain
내 사용자 이름이고 myuser
암호가 mypassword
올바른 구문 인 이유 는 무엇입니까?
비밀번호를 입력 할 필요가 없도록 재부팅을 예약해야합니다.
문제 Get-Credential
는 항상 암호를 요구한다는 것입니다. 그러나이 문제를 해결할 수있는 방법이 있지만 파일 시스템에 비밀번호를 보안 문자열로 저장해야합니다.
다음 기사에서는이 방법을 설명합니다.
요약하면 비밀번호를 암호화 된 문자열로 저장할 파일을 작성합니다. 다음 줄은 비밀번호를 묻는 메시지를 표시 한 다음 c:\mysecurestring.txt
암호화 된 문자열로 저장 합니다. 이 작업은 한 번만 수행하면됩니다.
read-host -assecurestring | convertfrom-securestring | out-file C:\mysecurestring.txt
-Credential
PowerShell 명령에 인수가 표시되면를 전달할 수 있다는 의미 PSCredential
입니다. 따라서 귀하의 경우 :
$username = "domain01\admin01"
$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>
-Authentication
환경을 모르기 때문에 다른 스위치 값 이 필요할 수 있습니다 .
다른 방법이 있지만 ...
스크립트 파일에 비밀번호를 원치 않으면이 방법을 사용하지 마십시오 (스크립트에 비밀번호를 저장하는 것은 좋지 않지만 일부는 방법을 알고 싶어합니다).
좋아요, 경고였습니다. 코드는 다음과 같습니다.
$username = "John Doe"
$password = "ABCDEF"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$cred
John Doe의 신임 정보에 비밀번호 "ABCDEF"가 있습니다.
비밀번호 사용 준비를위한 대체 수단 :
$password = convertto-securestring -String "notverysecretpassword" -AsPlainText -Force
자격 증명 저장과 관련하여 두 가지 기능을 사용합니다 (일반적으로 내 프로필에서로드되는 모듈에 있음).
#=====================================================================
# Get-MyCredential
#=====================================================================
function Get-MyCredential
{
param(
$CredPath,
[switch]$Help
)
$HelpText = @"
Get-MyCredential
Usage:
Get-MyCredential -CredPath `$CredPath
If a credential is stored in $CredPath, it will be used.
If no credential is found, Export-Credential will start and offer to
Store a credential at the location specified.
"@
if($Help -or (!($CredPath))){write-host $Helptext; Break}
if (!(Test-Path -Path $CredPath -PathType Leaf)) {
Export-Credential (Get-Credential) $CredPath
}
$cred = Import-Clixml $CredPath
$cred.Password = $cred.Password | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PsCredential($cred.UserName, $cred.Password)
Return $Credential
}
그리고 이것:
#=====================================================================
# Export-Credential
# Usage: Export-Credential $CredentialObject $FileToSaveTo
#=====================================================================
function Export-Credential($cred, $path) {
$cred = $cred | Select-Object *
$cred.password = $cred.Password | ConvertFrom-SecureString
$cred | Export-Clixml $path
}
당신은 이것을 다음과 같이 사용합니다 :
$Credentials = Get-MyCredential (join-path ($PsScriptRoot) Syncred.xml)
신임 정보 파일이 존재하지 않으면 처음 프롬프트가 표시되며, 이때 신임 정보가 XML 파일 내부의 암호화 된 문자열에 저장됩니다. 두 번째로 해당 행을 실행하면 xmlfile이 있으며 자동으로 열립니다.
다른 자격 증명이 필요한 원격 서버에서 SCOM 2012 기능을 실행해야합니다. 암호 해독 기능의 출력을 ConvertTo-SecureString에 입력으로 전달하여 일반 텍스트 암호를 피합니다. 명확성을 위해 여기에 표시되지 않습니다.
나는 나의 선언을 강력하게 입력하고 싶다. $ strPass에 대한 형식 선언이 올바르게 작동합니다.
[object] $objCred = $null
[string] $strUser = 'domain\userID'
[System.Security.SecureString] $strPass = ''
$strPass = ConvertTo-SecureString -String "password" -AsPlainText -Force
$objCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($strUser, $strPass)
재부팅을 예약하는 경우 다음 두 가지 방법을 수행 할 수 있습니다.
First you could create a task on one machine using credentials that have rights needed to connect and reboot another machine. This makes the scheduler responsible for securely storing the credentials. The reboot command (I'm a Powershell guy, but this is cleaner.) is:
SHUTDOWN /r /f /m \\ComputerName
The command line to create a scheduled task on the local machine, to remotely reboot another, would be:
SCHTASKS /Create /TN "Reboot Server" /TR "shutdown.exe /r /f /m \\ComputerName" /SC ONCE /ST 00:00 /SD "12/24/2012" /RU "domain\username" /RP "password"
I prefer the second way, where you use your current credentials to create a scheduled task that runs with the system account on a remote machine.
SCHTASKS /Create /TN "Reboot Server" /TR "shutdown.exe /r /f" /SC ONCE /ST 00:00 /SD "12/24/2012" /RU SYSTEM /S ComputerName
This also works through the GUI, just enter SYSTEM as the user name, leaving the password fields blank.
read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt
$pass = cat C:\securestring.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "test",$pass
$mycred.GetNetworkCredential().Password
Be very careful with storing passwords this way... it's not as secure as ...
I saw one example that uses Import/Export-CLIXML.
These are my favorite commands for the issue you're trying to resolve. And the simplest way to use them is.
$passwordPath = './password.txt'
if (-not (test-path $passwordPath)) {
$cred = Get-Credential -Username domain\username -message 'Please login.'
Export-Cli -InputObject $cred -Path $passwordPath
}
$cred = Import-CliXML -path $passwordPath
So if the file doesn't locally exist it will prompt for the credentials and store them. This will take a [pscredential]
object without issue and will hide the credentials as a secure string.
Finally just use the credential like you normally do.
Restart-Computer -ComputerName ... -Credentail $cred
Note on Securty:
Securely store credentials on disk
When reading the Solution, you might at first be wary of storing a password on disk. While it is natural (and prudent) to be cautious of littering your hard drive with sensitive information, the Export-CliXml cmdlet encrypts credential objects using the Windows standard Data Protection API. This ensures that only your user account can properly decrypt its contents. Similarly, the ConvertFrom-SecureString cmdlet also encrypts the password you provide.
Edit: Just reread the original question. The above will work so long as you've initialized the [pscredential]
to the hard disk. That is if you drop that in your script and run the script once it will create that file and then running the script unattended will be simple.
Solution
$userName = 'test-domain\test-login'
$password = 'test-password'
$pwdSecureString = ConvertTo-SecureString -Force -AsPlainText $password
$credential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $userName, $pwdSecureString
For Build Machines
In the previous code replace user name and password values by secret ("hidden from logs") environment variables of your build-machine
Test results by adding
'# Results'
$credential.GetNetworkCredential().Domain
$credential.GetNetworkCredential().UserName
$credential.GetNetworkCredential().Password
and you'll see
# Results
test-domain
test-login
test-password
why dont you try something very simple?
use psexec with command 'shutdown /r /f /t 0' and a PC list from CMD.
'Programing' 카테고리의 다른 글
HTML5 Canvas 뷰포트의 너비 100 % 높이? (0) | 2020.06.21 |
---|---|
jQuery를 사용하여 이미지를 비동기 적으로로드 (0) | 2020.06.21 |
TTY를 무시하지 않고 암호를 su / sudo / ssh에 전달하는 방법은 무엇입니까? (0) | 2020.06.21 |
Xcode가 하드웨어에서 프로젝트 실행을 갑자기 중단했습니다.“xxx.app을 시작할 수 없습니다 : .. 해당 파일이 없습니다.” (0) | 2020.06.21 |
Jquery 라디오 버튼이 선택된 경우 (0) | 2020.06.21 |