Powershell – 원격 접속을 위한 Credential 만들기

원격 접속을 위해 Enter-PSSession 또는 Invoke-Command 를 수행 할 때 -Credential 파라메터가 필요하다. 사용자 이름만 적으면 자동으로 대화 상자가 뜨면서 입력 할 수 있다.

Enter-PSSession -ComputerName [대상] -Credential [대상의 사용자]

직접 Password 를 입력 할 수 없고 대화상자가 뜨는 이유는 -Credential 파라메터에 PSCredential 타입의 개체가 필요한데 이것을 만드는데 사용되는 SecureString 개체가 대화상자를 통해서만 만들수 있다.

SecureString 만들기

아래와 같이 직접 만들 수 있다.

$secureString = Read-Host -AsSecureString

명령을 입력하면 사용자의 입력을 기다릴 것이다.
이 입력은 파라메터나 파이프라인으로 전달 할 수 없고 오로지 직접 입력해야 하는 것이다.

그러나 스크립트로 자동화 하기 위해서는 사용자의 입력없이 자동으로 돌아가야 하기 때문에 일반적으로 다음과 같이 처리한다.

$secureString = Read-Host -AsSecureString
$secureString | ConvertFrom-SecureString

ConvertFrom-SecureString를 사용하면 SecureString 개체를 문자열로 볼 수 있는데 물론 원본 string은 아니고 알아 볼수 없는 암호화된 string 이다.

ConvertFrom-SecureString 명령을 통해서 만들어진 결과물은 다시 ConvertTo-SecureString 으로 다시 SecureString 타입으로 만들 수 있다.

따라서 암화화된 string 을 파일(또는 어디든) 에 저장 해 두고 필요할때 ConvertTo-SecureString 으로 다시 SecureString 개체로 만들고 사용하는 것이다.

Read-Host -AsSecureString | ConvertFrom-SecureString | Set-Content "credential.txt"
# creadential.txt 에 암호화된 문자열이 저장되어 있다. 이것을 재사용함.
$secureString = Get-Content "credential.txt" | ConvertTo-SecureString

이 방식의 단점은 $secureString을 저장한 파일을 미리 만들어놔야 한다는 것이다. 즉, 동적으로 스크립트 상에서 SecureString을 만들어 낼 수 없다.

때문에 나는 직접 다음과같은 스크립트를 작성하여 사용한다.

New-SecureString

Function New-SecureString {
	param
	(
	[parameter(Mandatory=$true)]
	[string]$Value
	)

	$secureString = New-Object System.Security.SecureString
	$Value.ToCharArray() | %{ $secureString.AppendChar($_) }

	return $secureString
} Export-ModuleMember -Function New-SecureString

파라메터로 일반 String을 받아 SecureString으로 만든다.

# 즉시 만들어짐.
$secureString1 = New-SecureString -Value "1234qwer"

# 입력과정이 한번더 들어감.
$secureString2 = Read-Host -AsSecureString
"1234qwer"

# $secureString1 과 $secureString2 는 같다.

PSCredential를 생성하는 스크립트도 만들어두면 편하다.

New-PSCredential

Function New-PSCredential {
	param
	(
	[parameter(Mandatory=$true)]
	[string]$Username,
	[parameter(Mandatory=$true)]
	[string]$Password
	)

	New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(New-SecureString -Value $Password)
} Export-ModuleMember -Function New-PSCredential

SecureString 을 만들어 PSSession을 만드는 예

# 접속에 필요한 정보를 아래와 같이 가정함.
# computername - talsu.net
# username - Administrator
# password - 1234qwer

$cred = New-PSCredential -Username "Administrator" -Password "1234qwer"
$session = New-PSSession -ComputerName talsu.net -Credential $cred

#session 이 만들어 졌다. Invoke-Command 하거나 Enter-PSSession 할 수 있다.

# Invoke-Command
Invoke-Command -Session $session -ScriptBlock {Get-Process}

# Enter-PSSession
Enter-PSSession -Session $session

이것들을 잘 활용하면 수백 수천대의 서버를 장난감처럼 다룰수 있다.

2 thoughts on “Powershell – 원격 접속을 위한 Credential 만들기

  1. beren

    좋은 예를 보여주셔서 감사합니다.
    수박겉핥기한 PS 로는 안되겠다는 걸 이 글 보고 느끼고 갑니다.
    PS를 제대로 좀 공부해봐야겠습니다.

답글 남기기

이메일 주소는 공개되지 않습니다.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.