1. 기본 스크립트


$cred = Get-Credential

$session = New-PsSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://{exchange server}/PowerShell -Credential $cred -Authentication Basic -AllowRedirection

Import-PSSession $session -AllowClobber


New-PSSession 명령 실행 시 가능하면 -AllowRedirection 옵션을 항상 붙여주면 불필요한 오류를 원천 차단할 수 있다. 특히 Office 365 Exchange Online에 연결하는 경우에는 필수!

Import-PSSesion 명령 실행 시 가능하면 -AllowClobber 옵션을 항상 붙여주자. 이미 시스템에 같은 명령어가 있을 때 새로 불러오는 명령을 덮어써서 우선 순위를 올려준다.



2. New-PsSession 명령에서 오류가 발생하는 경우


[{exchange server}] 다음 오류 메시지가 발생하여 원격 서버에 연결하지 못했습니다. WinRM 클라이언트가 HTTP 잘못된 요청 상태(400)를 받았으나 원격 서비스에 오류 원인에 대한 어떠한 정보도 포함되어 있지 않습니다. 자세한 내용은 about_Remote_Troubleshooting 도움말 항목을 참조하십시오.

    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [], PSRemotingTransportException

    + FullyQualifiedErrorId : PSSessionOpenFailed


(영문 버전)

[{exchange server}] Connecting to remote server failed with the following error message : The WinRM client received an HTTP bad request status (400), but the remote service did not include any other information about the cause of the failure. For more information, see the about_Remote_Troubleshooting Help topic.

+ CategoryInfo : OpenError: (System.Manageme….RemoteRunspace:RemoteRunspace) [], PSRemotingTransportException

+ FullyQualifiedErrorId : PSSessionOpenFailed


이런 오류가 발생하는 원인은 두 가지 중 하나일 가능성이 높다.



첫째, WinRM 서비스가 중지되어 있을 경우


확인은 아래 명령으로 해 보면 알 수 있다.


Get-service winrm


명령 실행 후 아래에 표시되는 Status가 Stopped이면 중지된 상태.

그런 경우에는 아래 명령으로 winrm 서비스를 실행해 주면 된다.


Enable-PSremoting -Force


>> 참조: http://www.hackandsecure.com/2014/07/22/how-to-connect-to-exchange-online-via-powershell/



둘째, 사용자 계정 형식을 서버에서 인식할 수 없는 경우


WinRM 서비스는 실행 중(Running)이지만 같은 오류가 계속 반복된다면 사용자 계정 지정이 잘못되었을 가능성이 높다. 즉, Get-Credential 명령으로 입력한 사용자 아이디 형식 말이다.


보통, 사용자 아이디 형식을 그냥 일반 이름 형식(예: kdhong)로 쓰는 경우가 많은데 이것을 도메인\사용자 아이디 형식(예: domain\kdhong) 또는 이메일 주소 형식(예: kdhong@domain.com)으로 입력하면 바로 해결되는 경우가 많다.



다음으로 많이 발생하는 문제.


New-PsSession : [{exchange server}] 다음 오류 때문에 원격 서버 {exchange server}에 연결하지 못했습니다. WS-Management 서비스가 요청을 지원하지 않습니다. 자세한 내용은 about_Remote_Troubleshooting 도움말 항목을 참조하십시오.

위치 줄:1 문자:12

+ $session = New-PsSession -ConfigurationName microsoft.exchange -ConnectionUri ht ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException

    + FullyQualifiedErrorId : WinRMOperationNotSupportedOnServer,PSSessionOpenFailed


(영문 버전)

New-PSSession : [{exchange server}] Connecting to remote server {exchange server} failed with the following error message : The WS-Management service does not support the request. For more information, see the about_Remote_Troubleshooting Help topic. 

At line:1 char:1 

+ $session = New-PsSession -ConfigurationName microsoft.exchange -ConnectionUri ht ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException

    + FullyQualifiedErrorId : WinRMOperationNotSupportedOnServer,PSSessionOpenFailed


이런 문제는 인증(Authentication) 형식이 잘못된 경우에 발생하는 경우가 대부분.

옵션으로 -Authentication Basic 등을 명시적으로 지정해서 쓰지 않는 경우 기본적으로 "Default" 인증 방식이 사용되게 되는데, 이 방식으로는 엥간해서는 인증이 될 턱이 없다. 서버 구성에 맞게 Basic, Digest, Negotiate, Kerberos, Credssp, NegotiateWithImplicitCredential 중 하나를 골라 써야 한다. 최근엔 SSL 기반의 Basic 인증이 대세다.



New-PsSession : [{exchange server}] 다음 오류 때문에 원격 서버 {exchange server}에 연결하지 못했습니다. WinRM에서 요청을 처리할 수 없습니다. Kerberos 인증을 사용하는 동안 다음 오류가 발생했습니다. owa.pnpeople.com 컴퓨터가 Kerberos에 알려지지 않았습니다. 컴퓨터가 네트워크에 있는지, 제공한 이름의 철자가 올바른지, 컴퓨터에 액세스하는 Kerberos 구성이 올바른지 확인하십시오. 가장 일반적인 Kerberos 구성 문제는 대상에 대해 HTTP/{exchange server} 형식의 SPN이 구성되지 않았다는 것입니다. Kerberos가 필요한 경우 협상 인증 메커니즘을 지정한 후 작업을 다시 제출하십시오. 자세한 내용은 about_Remote_Troubl

eshooting 도움말 항목을 참조하십시오.

위치 줄:1 문자:12

+ $session = New-PsSession -ConfigurationName microsoft.exchange -ConnectionUri ht ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException

    + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionOpenFailed


이런 오류가 발생하는 경우는 Kerberos 인증으로 지정했지만 접속한 서버 이름(도메인 포함)이 SPN으로 정상 등록되어 있지 않은 경우. Exchange 2010 버전까지는 같은 도메인에 소속된 컴퓨터에서 Kerberos 인증으로만 접속되었었는데(경험 상. 그러나 확인은 불가.) Exchange 2013 버전부터는 같은 도메인이 아닌 경우라도, 또 Basic 인증으로도 접속이 되기 때문에 이제부터는 Kerberos 인증 설정은 거의 사용될 일이 없을 듯.



New-PsSession : [{exchange server}] 다음 오류 때문에 원격 서버 {exchange server}에 연결하지 못했습니다.

액세스가 거부되었습니다. 자세한 내용은 about_Remote_Troubleshooting 도움말 항목을 참조하십시오.

위치 줄:1 문자:12

+ $session = New-PsSession -ConfigurationName microsoft.exchange -ConnectionUri ht ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException

    + FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed


역시나 인증이 정상적으로 되지 않은 경우. PowerShell은 IIS 인증 설정을 따르는데 그 설정과 다른 경우에도 이런 오류가 발생하면서 정상적으로 세션을 만들지 못한다. 적절한 인증 형식으로 지정하는 것이 중요!


이외의 문제 발생 시에는 구글링으로 찾아봐야 할 듯.



3. Import-PSSesion 명령에서 오류가 발생하는 경우


이 때 오류가 발생하는 경우는 경험적으로 딱 한 가지 경우뿐이었다.


Import-Module : 형식 데이터 파일을 로드하는 동안 오류가 발생했습니다.

Microsoft.PowerShell, , C:\Users\Administrator\AppData\Local\Temp\2\tmp_2109ac30-0b23-4888-99e6-25214d1bb92a_0pgkw2da.2

p1\tmp_2109ac30-0b23-4888-99e6-25214d1bb92a_0pgkw2da.2p1.format.ps1xml: 다음 유효성 검사 예외로 인해 파일을 건너뛰었습니다. 이 시스템에서 스크립트를 실행할 수 없으므로 C:\Users\Administrator\AppData\Local\Temp\2\tmp_2109ac30-0b23-4888-99

e6-25214d1bb92a_0pgkw2da.2p1\tmp_2109ac30-0b23-4888-99e6-25214d1bb92a_0pgkw2da.2p1.format.ps1xml 파일을 로드할 수 없습니다. 자세한 내용은 "get-help about_signing"을 참조하십시오..

위치 줄:3 문자:30

+                 Import-Module <<<<  -Name $name -Alias * -Function * -Prefix $prefix -DisableNameChecking:$disableNameChecking -PassThru -ArgumentList @($session)

    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException

    + FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Commands.ImportModuleCommand


(영문 버전)

Import-Module : There were errors in loading the format data file:

Microsoft.PowerShell, , C:\Users\Administrator\AppData\Local\Temp\tmp_7f0bdca2-c061-4637-8954-d5845eda534a_iopk53sm.e4l\tmp_7f0b

dca2-c061-4637-8954-d5845eda534a_iopk53sm.e4l.format.ps1xml : File skipped because of the following validation exception: File C:\Users\Administrator\AppData\Local\Temp\tmp_7f0bdca2-c061-4637-8954-d5845eda534a_iopk53sm.e4l\tmp_7f0bdca2-c061-4637-8

954-d5845eda534a_iopk53sm.e4l.format.ps1xml cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details..

At line:3 char:30

+                 Import-Module <<<<  -Name $name -Alias * -Function * -Prefix $prefix -DisableNameChecking:$disableNameChecking -PassThru -ArgumentList @($session)

    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException

    + FullyQualifiedErrorId : FormatXmlUpateException,Microsoft.PowerShell.Commands.ImportModuleCommand


"이 시스템에서 스크립트를 실행할 수 없으므로(because the execution of scripts is disabled on this system)" 라는 문구를 주의해서 보면 바로 감이 딱 온다.


바로 PowerShell 실행 규칙(ExecutionPolicy)이 "Restricted"로 되어 있기 때문이다. 이것을 "RemoteSigned"로 바꿔주면 해결.


PS C:\> Get-ExecutionPolicy

Restricted


PS C:\> Set-ExecutionPolicy RemoteSigned


실행 규칙 변경

실행 정책은 신뢰하지 않는 스크립트로부터 사용자를 보호해 줍니다. 실행 정책을 변경하면 about_Execution_Policies 도움말 항목에 설명된 보안 위험에 노출될 수 있습니다. 실행 정책을 변경하시겠습니까?

[Y] 예(Y)  [N] 아니요(N)  [S] 일시 중단(S)  [?] 도움말 (기본값은 "Y"임):




4. 참고로, Office 365에 원격 연결하는 PowerShell 명령은 아래와 같은 두 가지 방법이 있다.

(첫 번째 방법은 "Microsoft Online Services Sign-In Assistant"이 설치되어 있어야 하는 방법으로 보인다.)


Import-Module MSOnline

$O365Cred = Get-Credential

$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection

Import-PSSession $O365Session -AllowClobber

Connect-MsolService -Credential $O365Cred



$O365Cred = Get-Credential

$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365Cred -Authentication Basic -AllowRedirection

Import-PSSession $O365Session -AllowClobber


아마도 원래는 첫 번째 방법으로 사용하다가

최근 업데이트 되어 두 번째 방법으로 사용하도록 된 것이 아닐까 생각된다.







Posted by 떼르미
,


자바스크립트를 허용해주세요!
Please Enable JavaScript![ Enable JavaScript ]