IT STORYs
프로비저닝 WS_mailsupport.cs 본문
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Text;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections;
using System.IO;
namespace HiFiLM.SIL.Extention
{
/// <summary>
/// Summary description for WS_MailSupport
/// </summary>
[WebService(Namespace = "http://localhost/HiFiLM/OrgInfra/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [Systehttp://m.Web.Script.Services.ScriptService]
public class MailSupport : System.Web.Services.WebService
{
#region 변수선언
/// <summary>
/// 작업대상 도메인 컨트롤러 DNS명
/// </summary>
public string __dedicatedDcDnsName;
/// <summary>
/// 작업대상 도메인 명 (dnikk.local)
/// </summary>
public string domain = string.Empty;
/// <summary>
/// 익스체인지 스키마 경로 (http://schemas.microsoft.com/powershell/Microsoft.Exchange)
/// </summary>
public string exSchema = string.Empty;
/// <summary>
/// 파워쉘 경로 (http://dnikkts.dnikk.local/PowerShell)
/// </summary>
public string powershellUri = string.Empty;
/// <summary>
/// 관리자 계정 아이디
/// </summary>
public string userAccount = string.Empty;
/// <summary>
/// 관리자 계정 비밀번호
/// </summary>
public string password = string.Empty;
#endregion
public WebServiceHeader webServiceHeader = null;
public MailSupport()
{
//Uncomment the following line if using designed components
//InitializeComponent();
//string dedicatedDcDnsName = ConfigurationManager.AppSettings["DefaultDedicatedDcDnsName"];
__dedicatedDcDnsName = ConfigurationManager.AppSettings["DefaultDedicatedDcDnsName"];
}
/// <summary>
/// get. 기본 작업 대상 도메인 컨트롤러 FQDN
/// </summary>
private WebServiceHeader WebServiceHeader
{
get
{
if (webServiceHeader == null)
{
webServiceHeader = new WebServiceHeader();
webServiceHeader.ClientAuthorizationKey = Guid.Empty;
webServiceHeader.DedicatedDcFQDN = ConfigurationManager.AppSettings["DefaultDedicatedDcFQDN"];
}
return webServiceHeader;
}
}
#region 보호속성
/// <summary>
/// get. 작업대상 도메인 컨트롤러 DNS명 (ex. dnikkts.dnikk.local)
/// </summary>
protected string DedicatedDcDnsName
{
get
{
return __dedicatedDcDnsName;
}
}
#endregion
/// <summary>
/// 특정 메일사서함에 대하여 관리용 접근권한 추가
/// </summary>
/// <param name="identity">사용 권한이 필요한 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="administratorIdentity">사용 권한이 부여되는 다른 사서함의 사용자 사서함ID</param>
[WebMethod(Description = "특정 메일사서함에 대하여 관리용 접근권한 추가"), SoapHeader("webServiceHeader")]
public void AddMailBoxPermission(string identity, string administratorIdentity)
{
Command shellCommand = BuildShellCommandWithIdentity("Add-MailboxPermission", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("User", administratorIdentity);
shellCommand.Parameters.Add("AccessRights", "FullAccess");
shellCommand.Parameters.Add("InheritanceType", "All");
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
}
/// <summary>
/// 사서함 SMTP 주소 설정 & 용량 설정 : GS건설 사용자 용 (gsconst.co.kr 주소를 추가한다.)
/// </summary>
/// <param name="identity">메일주소를 설정할 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="PrimarySmtpAddress">설정 주소</param>
[WebMethod(Description = "사서함 SMTP 주소 설정 & 용량 설정"), SoapHeader("webServiceHeader")]
public void SetSmtpAddressAndQuota(string identity, string strEmailAddress, string employ_yn, string IssueWarningQuota, string ProhibitSendQuota, string ProhibitSendReceiveQuota)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("PrimarySmtpAddress", strEmailAddress);
shellCommand.Parameters.Add("EmailAddressPolicyEnabled", false);
shellCommand.Parameters.Add("CustomAttribute1", employ_yn);
shellCommand.Parameters.Add("CustomAttribute2", "GSENC");
shellCommand.Parameters.Add("UseDatabaseQuotaDefaults", false);
shellCommand.Parameters.Add("IssueWarningQuota", IssueWarningQuota);
shellCommand.Parameters.Add("ProhibitSendQuota", ProhibitSendQuota);
shellCommand.Parameters.Add("ProhibitSendReceiveQuota", ProhibitSendReceiveQuota);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
WriteLog("SetSmtpAddressAndQuota", "identity : " + identity + ", IssueWarningQuota : " + IssueWarningQuota + ", ProhibitSendQuota : " + ProhibitSendQuota + ", ProhibitSendReceiveQuota : " + ProhibitSendReceiveQuota);
StringBuilder sbScript = new StringBuilder();
sbScript.AppendFormat("Set-Mailbox -Identity {0} ", identity);
sbScript.AppendFormat("-DomainController {0} ", this.DedicatedDcDnsName);
sbScript.AppendFormat("-EmailAddresses @{{Add='{0}@gsconst.co.kr'}} ", identity);
PSCommand psCmd = new PSCommand();
psCmd.AddScript(sbScript.ToString());
InvokeManagementShellCommandByPSCommand(psCmd);
}
/// <summary>
/// 사서함 SMTP 주소 설정 & 용량 설정 : GS건설 사용자를 제외한 다른 사용자 용 (ex. Xi S&D 등...)
/// </summary>
/// <param name="identity">메일주소를 설정할 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="PrimarySmtpAddress">설정 주소</param>
[WebMethod(Description = "사서함 SMTP 주소 설정 & 용량 설정"), SoapHeader("webServiceHeader")]
public void SetSmtpAddressXiAndQuota(string identity, string strEmailAddress, string employ_yn, string IssueWarningQuota, string ProhibitSendQuota, string ProhibitSendReceiveQuota)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("PrimarySmtpAddress", strEmailAddress);
shellCommand.Parameters.Add("EmailAddressPolicyEnabled", false);
shellCommand.Parameters.Add("CustomAttribute1", employ_yn);
shellCommand.Parameters.Add("CustomAttribute2", "GSENC");
shellCommand.Parameters.Add("UseDatabaseQuotaDefaults", false);
shellCommand.Parameters.Add("IssueWarningQuota", IssueWarningQuota);
shellCommand.Parameters.Add("ProhibitSendQuota", ProhibitSendQuota);
shellCommand.Parameters.Add("ProhibitSendReceiveQuota", ProhibitSendReceiveQuota);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
WriteLog("SetSmtpAddressXiAndQuota", "identity : " + identity + ", IssueWarningQuota : " + IssueWarningQuota + ", ProhibitSendQuota : " + ProhibitSendQuota + ", ProhibitSendReceiveQuota : " + ProhibitSendReceiveQuota);
}
/// <summary>
/// 사서함 SMTP 주소 설정 & 용량 설정 : GS건설 사용자 용 (gsconst.co.kr 주소를 추가한다.)
/// </summary>
/// <param name="identity">메일주소를 설정할 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="PrimarySmtpAddress">설정 주소</param>
[WebMethod(Description = "사서함 SMTP 주소 설정"), SoapHeader("webServiceHeader")]
public void SetSmtpAddress(string identity, string strEmailAddress, string employ_yn)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("PrimarySmtpAddress", strEmailAddress);
shellCommand.Parameters.Add("EmailAddressPolicyEnabled", false);
shellCommand.Parameters.Add("CustomAttribute1", employ_yn);
shellCommand.Parameters.Add("CustomAttribute2", "GSENC");
shellCommand.Parameters.Add("UseDatabaseQuotaDefaults", false);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
WriteLog("SetSmtpAddress", "PrimarySmtpAddress : " + strEmailAddress);
}
/// <summary>
/// 사서함 SMTP 주소 설정 & 용량 설정 : GS건설 사용자를 제외한 다른 사용자 용 (ex. Xi S&D 등...)
/// </summary>
/// <param name="identity">메일주소를 설정할 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="PrimarySmtpAddress">설정 주소</param>
[WebMethod(Description = "사서함 SMTP 주소 설정"), SoapHeader("webServiceHeader")]
public void SetSmtpAddressXi(string identity, string strEmailAddress, string employ_yn)
{
bool isUseDatabaseQuotaDefaults = false;
if (strEmailAddress.Contains("xicna.com") || strEmailAddress.Contains("zeitcna.com")) isUseDatabaseQuotaDefaults = true;
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("PrimarySmtpAddress", strEmailAddress);
shellCommand.Parameters.Add("EmailAddressPolicyEnabled", false);
shellCommand.Parameters.Add("CustomAttribute1", employ_yn);
shellCommand.Parameters.Add("CustomAttribute2", "GSENC");
//shellCommand.Parameters.Add("UseDatabaseQuotaDefaults", false);
shellCommand.Parameters.Add("UseDatabaseQuotaDefaults", isUseDatabaseQuotaDefaults);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
WriteLog("SetSmtpAddressXi", "PrimarySmtpAddress : " + strEmailAddress);
}
/// <summary>
/// 사서함 용량 설정
/// </summary>
/// <param name="identity"></param>
/// <param name="IssueWarningQuota"></param>
/// <param name="ProhibitSendQuota"></param>
/// <param name="ProhibitSendReceiveQuota"></param>
[WebMethod(Description = "사서함 용량 설정"), SoapHeader("webServiceHeader")]
public void SetMailboxQuota(string identity, string IssueWarningQuota, string ProhibitSendQuota, string ProhibitSendReceiveQuota)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("UseDatabaseQuotaDefaults", false);
shellCommand.Parameters.Add("IssueWarningQuota", IssueWarningQuota);
shellCommand.Parameters.Add("ProhibitSendQuota", ProhibitSendQuota);
shellCommand.Parameters.Add("ProhibitSendReceiveQuota", ProhibitSendReceiveQuota);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
WriteLog("SetMailboxQuota", "identity : " + identity + ", IssueWarningQuota : " + IssueWarningQuota + ", ProhibitSendQuota : " + ProhibitSendQuota + ", ProhibitSendReceiveQuota : " + ProhibitSendReceiveQuota);
}
/// <summary>
/// Lync 활성화 및 정책 적용
/// </summary>
/// <param name="identity">메일주소를 설정할 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="PrimarySmtpAddress">설정 주소</param>
[WebMethod(Description = "Lync 활성화 및 정책 적용"), SoapHeader("webServiceHeader")]
public void SetLyncInfo(string identity)
{
// Enable-CSUser -IDENTITY $SIPURI -RegistrarPool $RegistrarPool -SipAddress $sipaddress
// Grant-CsClientPolicy -IDENTITY $SIPURI -policyname 'Employee2'
// Grant-CsMobilityPolicy -IDENTITY $SIPURI -policyname 'Employee1'
// Grant-CSExternalAccessPolicy -IDENTITY $SIPURI -policyname 'Employee1'
// Grant-CsConferencingPolicy -IDENTITY $SIPURI -policyname 'Employee1'
//test
//string AdminID = ConfigurationManager.AppSettings["AdminID"];
//string AdminPW = ConfigurationManager.AppSettings["AdminPW"];
//SecureString securedPassword = new SecureString();
//foreach (char character in AdminPW)
//{
// securedPassword.AppendChar(character);
//}
//PSCredential loginInfo = new PSCredential("gsenc\\" + AdminID, securedPassword);
////test
//WSManConnectionInfo connection = new WSManConnectionInfo(new Uri("https://lyncfe01.gsconst.local/ocspowershell"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", loginInfo);
////connection.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
//connection.AuthenticationMechanism = AuthenticationMechanism.Negotiate;//test
//connection.ProxyAuthentication = AuthenticationMechanism.Negotiate;
//string sipAddress = identity + "@gsconst.co.kr";
//using (Runspace runspace = RunspaceFactory.CreateRunspace(connection))
//{
// using (PowerShell powershell = PowerShell.Create())
// {
// powershell.AddScript(String.Format("Enable-CSUser -Identity {0} -RegistrarPool {1} -SipAddress {2}; ", identity, "GSPool01.GSCONST.CO.KR", sipAddress));
// powershell.AddScript(String.Format("Grant-CsClientPolicy -Identity {0} -PolicyName 'Employee2'; ", sipAddress));
// powershell.AddScript(String.Format("Grant-CsMobilityPolicy -Identity {0} -PolicyName 'Employee1'; ", sipAddress));
// powershell.AddScript(String.Format("Grant-CSExternalAccessPolicy -Identity {0} -PolicyName 'Employee1'; ", sipAddress));
// powershell.AddScript(String.Format("Grant-CsConferencingPolicy -Identity {0} -PolicyName 'Employee1'; ", sipAddress));
// runspace.Open();
// powershell.Runspace = runspace;
// powershell.Invoke();
// runspace.Close();
// }
//}
}
/// <summary>
/// 특정 메일사서함에 대하여 위임 권한 설정
/// </summary>
/// <param name="identity">위임이 필요한 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="administratorIdentity">사용 권한이 부여되는 다른 사서함의 사용자 사서함ID</param>
[WebMethod(Description = "메일사서함 관리용 위임권한 설정"), SoapHeader("webServiceHeader")]
public void SetMailBoxDelegate(string identity, string administratorIdentity)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("GrantSendOnBehalfTo", administratorIdentity);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
}
/// <summary>
/// 특정 메일사서함에 대하여 보존정책 설정
/// </summary>
/// <param name="identity">보존 정책 설정이 필요한 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
/// <param name="strPolicyName">보존 정책명</param>
[WebMethod(Description = "메일사서함 보존정책 설정"), SoapHeader("webServiceHeader")]
public void SetMailBoxRetentionPolicy(string identity, string strPolicyName)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-MailBox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("RetentionPolicy", strPolicyName);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
}
/// <summary>
/// 메일 사서함 ActiveSync Enabled False 설정
/// </summary>
/// <param name="identity">ActiveSync 대상 사용자 사서함ID. 유효형식:GUID, DN, 도메인\계정, UPN, LegacyExchangeDN, SMTP주소, 별칭</param>
[WebMethod(Description = "메일 사서함 ActiveSync Enabled False 설정"), SoapHeader("webServiceHeader")]
public void SetCasMailBoxActiveSync(string identity)
{
Command shellCommand = BuildShellCommandWithIdentity("Set-CASMailbox", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("ActiveSyncEnabled", false);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 기존 디렉토리 연락처 삭제
/// </summary>
/// <param name="identity">연락처ID. 유효형식:ADObjectID, DN, LegacyExchangeDN, GUID, Domain\SamAccountName, UPN, 이메일 주소, 별칭</param>
[WebMethod(Description = "기존 디렉토리 연락처 삭제"), SoapHeader("webServiceHeader")]
public void RemoveMailContact(string identity)
{
Command shellCommand = BuildShellCommandWithIdentity("Remove-MailContact", true);
shellCommand.Parameters.Add("Identity", identity);
shellCommand.Parameters.Add("Confirm", false);
InvokeManagementShellCommand(shellCommand);
}
/// <summary>
/// 디렉토리 연락처 속성 출력
/// </summary>
/// <param name="identity">연락처ID. 유효형식:ADObjectID, DN, LegacyExchangeDN, GUID, Domain\SamAccountName, UPN, 이메일 주소, 별칭</param>
[WebMethod(Description = "새 디렉토리 연락처 생성 및 활성화"), SoapHeader("webServiceHeader")]
public DataSet GetMailContact(string identity)
{
DataSet dsResult = null;
Command shellCommand = BuildShellCommandWithIdentity("Get-MailContact", true);
shellCommand.Parameters.Add("Identity", identity);
//shellCommand.Parameters.Add("Confirm", false);
Runspace managementShellRunSpace = null;
try
{
managementShellRunSpace = OpenManangementShellRunsSpace();
Pipeline pipeLine = managementShellRunSpace.CreatePipeline();
//제공된 쉘명령 추가 및 실행
pipeLine.Commands.Add(shellCommand);
Collection<PSObject> commandResults = pipeLine.Invoke();
dsResult = BuildPropertyDataSet(commandResults);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (managementShellRunSpace != null)
managementShellRunSpace.Close();
}
return dsResult;
}
/// <summary>
/// 새 디렉토리 연락처 생성 및 활성화.<br/>
/// 제공된 대상 이메일주소를 기본 SMTP주소로 설정하고, 전자메일주소 정책을 비활성화
/// </summary>
/// <param name="name">CN</param>
/// <param name="displayName">표시이름</param>
/// <param name="externalEmailAddress">외부 메일주소. 예)hz.lginnotek.com</param>
/// <param name="baseOuDN">기준 OU DN</param>
[WebMethod(Description = "새 디렉토리 연락처 생성 및 활성화"), SoapHeader("webServiceHeader")]
public void NewMailContact(string name, string displayName, string externalEmailAddress, string baseOuDN)
{
Runspace exchangeRunspace = null;
try
{
exchangeRunspace = OpenManangementShellRunsSpace();
//exchangeRunspace.Open();
//create command pipeline
Pipeline pipeLine = exchangeRunspace.CreatePipeline();
//add command and parameters
Command command = new Command("New-MailContact");
command.Parameters.Add("Name", name);
command.Parameters.Add("Alias", name);
command.Parameters.Add("DisplayName", displayName);
command.Parameters.Add("ExternalEmailAddress", externalEmailAddress);
command.Parameters.Add("OrganizationalUnit", baseOuDN);
command.Parameters.Add("DomainController", this.DedicatedDcDnsName);
command.Parameters.Add("Confirm", false);
pipeLine.Commands.Add(command);
//execute command
Collection<PSObject> commandResults = pipeLine.Invoke();
//파이프라인 호출결과 확인
VerifyPipeLineInvokeResult(pipeLine);
string contactDN = String.Format("CN={0},{1}", name, baseOuDN);
//SetMailContactPrimarySmtpAddress(contactDN, externalEmailAddress);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (exchangeRunspace != null)
exchangeRunspace.Close();
}
}
/// <summary>
/// Contact 조회, ADUser 의 Contact 이 존재하는 경우 LegacyExchangeDN 을 Return 함.
/// </summary>
/// <param name="aliasName">메일별칭</param>
/// <returns>LegacyExchangeDN. null= Contact 이 존재하지 않음</returns>
[WebMethod(Description = "Contact LegacyExchangeDN 획득"), SoapHeader("webServiceHeader")]
public string GetMailContactLegacyExchangeDN(string aliasName)
{
Collection<PSObject> invokeResults = null;
string strLegacyExchangeDN = null;
try
{
Command shellCommand = BuildShellCommandWithIdentity("Get-MailContact", aliasName, true);
invokeResults = InvokeManagementShellCommand(shellCommand);
DataSet contactInformation = BuildPropertyDataSet(invokeResults);
foreach (PSObject cmdlet in invokeResults)
{
foreach (PSPropertyInfo propertyInfo in cmdlet.Properties)
{
if (propertyInfo.Name == "LegacyExchangeDN")
{
strLegacyExchangeDN = propertyInfo.Value.ToString();
}
}
}
}
catch
{
//예외 발생시 Contact 이 존재하지 않는 것으로 간주
invokeResults = null;
}
if (invokeResults == null)
return null;
//return "/o=Dnikk/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=59e5b2464a014ab7bcce42cbc726d179-Test Contact";
return strLegacyExchangeDN;
}
/// <summary>
/// Mail LegacyExchangeDN 변경
/// </summary>
/// <param name="aliasName">메일별칭</param>
/// <param name="LegacyExchangeDN">변경 LegacyExchangeDN</param>///
[WebMethod(Description = "Mail LegacyExchangeDN 변경"), SoapHeader("webServiceHeader")]
public void ChangeSetMailLegacyExchangeDN(string aliasName, string strLegacyExchangeDN)
{
string strDistinguishedName = GetDistinguishedName(aliasName);
StringBuilder sbScript = new StringBuilder();
sbScript.Append("Set-AdObject -Identity '#aliasNm' ").Replace("#aliasNm", strDistinguishedName);
sbScript.Append("-Replace @{LegacyExchangeDN = '#legExDN'}").Replace("#legExDN", strLegacyExchangeDN);
//string strScript = "Set-AdObject -Identity 'CN=Test Contact 2,OU=직원,OU=Dotnetsoft,DC=dnikk,DC=test' -Replace @{LegacyExchangeDN = '/o=Dnikk/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=d09e20622ca04eb4a6fa2d94d7691e4d-Test Contact 8'}";
InitialSessionState InitialSS = InitialSessionState.CreateDefault();
InitialSS.ImportPSModule(new string[] { "ActiveDirectory" });
Runspace myRunSpace = null;
myRunSpace = RunspaceFactory.CreateRunspace(InitialSS);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
//제공된 쉘명령 추가 및 실행
pipeLine.Commands.AddScript(sbScript.ToString());
Collection<PSObject> invokeResults = null;
invokeResults = pipeLine.Invoke();
}
// 내부메소드 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region 내부메소드
/// <summary>
/// 익스체인지 관리쉘 구동환경 생성 및 오픈
/// </summary>
/// <returns>익스체인지 관리쉘 구동 환경</returns>
private Runspace OpenManangementShellRunsSpace()
{
Runspace remoteRunspace = null;
SecureString ssPassword = new SecureString();
exSchema = ConfigurationManager.AppSettings["exSchema"];
powershellUri = ConfigurationManager.AppSettings["powershellUri"];
domain = ConfigurationManager.AppSettings["domain"];
userAccount = ConfigurationManager.AppSettings["AdminID"];
string password = ConfigurationManager.AppSettings["AdminPW"];
try
{
foreach (char c in password.ToCharArray())
{
ssPassword.AppendChar(c);
}
PSCredential credential = new PSCredential(domain + "\\" + userAccount, ssPassword);
WSManConnectionInfo wsmInfo = new WSManConnectionInfo(new Uri(powershellUri), exSchema, credential);
wsmInfo.OperationTimeout = 5 * 60 * 1000; // 5min
wsmInfo.OpenTimeout = 1 * 6 * 1000; // 1min
wsmInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
wsmInfo.ProxyAuthentication = AuthenticationMechanism.Negotiate;
remoteRunspace = RunspaceFactory.CreateRunspace(wsmInfo);
remoteRunspace.Open();
}
catch (RunspaceOpenModuleLoadException ex)
{
throw ex;
}
return remoteRunspace;
}
/// <summary>
/// 파이프라인 호출결과 확인. 오류발생시 예외 발생
/// </summary>
/// <param name="pipeLine">파이프라인 개체</param>
private static void VerifyPipeLineInvokeResult(Pipeline pipeLine, String strMessage)
{
string message = string.Empty;
if (pipeLine.Error == null || pipeLine.Error.Count == 0)
{
return;
}
foreach (object item in pipeLine.Error.ReadToEnd())
message += String.Format("{0}{1}", item, Environment.NewLine);
throw new Exception(message);
}
/// <summary>
/// 파이프라인 호출결과 확인. 오류발생시 예외 발생
/// </summary>
/// <param name="pipeLine">파이프라인 개체</param>
private static void VerifyPipeLineInvokeResult(Pipeline pipeLine)
{
string message = string.Empty;
if (pipeLine.Error == null || pipeLine.Error.Count == 0) return;
foreach (object item in pipeLine.Error.ReadToEnd())
message += String.Format("{0}{1}", item, Environment.NewLine);
throw new Exception(message);
}
/// <summary>
/// 파이프라인 호출결과를 데이터셋으로 변환
/// </summary>
/// <param name="invokeResults">파이프라인 호출결과</param>
/// <returns>결과집합. *개의 레코드</returns>
private static DataSet BuildPropertyDataSet(Collection<PSObject> invokeResults)
{
DataSet dsResult = new DataSet();
DataTable dtResult = dsResult.Tables.Add();
dtResult.BeginInit();
dtResult.Columns.Add("Name", typeof(string));
dtResult.Columns.Add("Value", typeof(string));
DataRow drRecord;
foreach (PSObject cmdlet in invokeResults)
{
foreach (PSPropertyInfo propertyInfo in cmdlet.Properties)
{
drRecord = dtResult.NewRow();
drRecord["Name"] = propertyInfo.Name;
drRecord["Value"] = propertyInfo.Value;
dtResult.Rows.Add(drRecord);
}
}
dtResult.EndInit();
return dsResult;
}
#endregion
#region 공개메소드
/// <summary>
/// aliasName 으로 GetDistinguishedName 조회
/// </summary>
/// <param name="aliasName">메일별칭</param>
public string GetDistinguishedName(string aliasName)
{
string strScript = string.Empty;
string strReturn = string.Empty;
Runspace managementShellRunSpace = null;
Collection<PSObject> invokeResults = null;
//strScript = "(Get-Mailbox '" + aliasName + "').DistinguishedName";
Command shellCommand = BuildShellCommandWithIdentity("Get-Mailbox", aliasName, true);
invokeResults = InvokeManagementShellCommand(shellCommand);
try
{
managementShellRunSpace = OpenManangementShellRunsSpace();
Pipeline pipeLine = managementShellRunSpace.CreatePipeline();
//제공된 쉘명령 추가 및 실행
pipeLine.Commands.Add(shellCommand);
//pipeLine.Commands.AddScript(strScript.ToString());
invokeResults = pipeLine.Invoke();
//호출결과 확인
//VerifyPipeLineInvokeResult(pipeLine);
foreach (PSObject cmdlet in invokeResults)
{
foreach (PSPropertyInfo propertyInfo in cmdlet.Properties)
{
if (propertyInfo.Name == "DistinguishedName")
{
strReturn = propertyInfo.Value.ToString();
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (managementShellRunSpace != null)
managementShellRunSpace.Close();
}
return strReturn;
}
/// <summary>
/// 익스체인지 관리쉘 커맨드 생성
/// </summary>
/// <param name="shellCommandName">커맨드명</param>
/// <param name="assignDomainController">도메인 컨트롤러 지정 여부</param>
/// <returns>관리쉘 커맨드</returns>
public Command BuildShellCommand(string shellCommandName, bool assignDomainController)
{
Command shellCommand = new Command(shellCommandName);
if (assignDomainController)
shellCommand.Parameters.Add("DomainController", this.DedicatedDcDnsName);
return shellCommand;
}
/// <summary>
/// 식별값에 대한 기본 파라메터를 가진 익스체인지 관리쉘 커맨드 생성
/// </summary>
/// <param name="shellCommandName">커맨드명</param>
/// <param name="identityValue">식별값</param>
/// <param name="assignDomainController">도메인 컨트롤러 지정 여부</param>
/// <returns>관리쉘 커맨드</returns>
public Command BuildShellCommandWithIdentity(string shellCommandName, bool assignDomainController)
{
Command shellCommand = BuildShellCommand(shellCommandName, assignDomainController);
//shellCommand.Parameters.Add("Identity", identityValue);
return shellCommand;
}
/// <summary>
/// 식별값에 대한 기본 파라메터를 가진 익스체인지 관리쉘 커맨드 생성
/// </summary>
/// <param name="shellCommandName">커맨드명</param>
/// <param name="identityValue">식별값</param>
/// <param name="assignDomainController">도메인 컨트롤러 지정 여부</param>
/// <returns>관리쉘 커맨드</returns>
public Command BuildShellCommandWithIdentity(string shellCommandName, string identityValue, bool assignDomainController)
{
Command shellCommand = BuildShellCommand(shellCommandName, assignDomainController);
shellCommand.Parameters.Add("Identity", identityValue);
return shellCommand;
}
/// <summary>
/// 익스체인지 관리쉘 커맨드 실행
/// </summary>
/// <param name="shellCommand">커맨드명</param>
/// <returns>파워쉘 명령 결과</returns>
public Collection<PSObject> InvokeManagementShellCommand(Command shellCommand)
{
Runspace managementShellRunSpace = null;
Collection<PSObject> invokeResults = null;
try
{
managementShellRunSpace = OpenManangementShellRunsSpace();
Pipeline pipeLine = managementShellRunSpace.CreatePipeline();
//제공된 쉘명령 추가 및 실행
pipeLine.Commands.Add(shellCommand);
invokeResults = pipeLine.Invoke();
//호출결과 확인
VerifyPipeLineInvokeResult(pipeLine);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (managementShellRunSpace != null)
managementShellRunSpace.Close();
}
return invokeResults;
}
/// <summary>
/// 2018.11.27 mwjin7@dotnetsoft.co.kr 추가 : Exchange Management Shell Command Run by PSCommand
/// </summary>
/// <param name="psCmd"></param>
/// <returns></returns>
private Collection<PSObject> InvokeManagementShellCommandByPSCommand(PSCommand psCmd)
{
Collection<PSObject> invokeResults = null;
string errMsg = String.Empty;
using (Runspace rs = OpenManangementShellRunsSpace())
{
PowerShell psh = PowerShell.Create();
psh.Runspace = rs;
psh.Commands = psCmd;
invokeResults = psh.Invoke();
if (psh.Streams.Error.Count > 0)
{
foreach (ErrorRecord err in psh.Streams.Error)
{
errMsg += String.Format("{0}{1}", err.Exception.ToString(), Environment.NewLine);
}
rs.Close();
}
}
if (String.IsNullOrEmpty(errMsg) == false)
throw new Exception(errMsg);
return invokeResults;
}
#endregion
/// <summary>
/// <b>로그를 파일에 기록합니다.</b><br/>
/// </summary>
/// <param name="fileName">파일명</param>
/// <param name="message">로그메시지</param>
public static void WriteLog(string fileName, string message)
{
string strLogDir = string.Empty;
string strFullPath = string.Empty;
FileStream fs = null;
StreamWriter sw = null;
try
{
// 로그 파일이 위치할 디렉토리 경로
//strLogDir = Systehttp://m.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\Log";
//strLogDir = ConfigurationManager.AppSettings["LogFilePath"].ToString();
strLogDir = @"D:\HiFiLM\SIL\Log";
// 로그 파일명
strFullPath = string.Format(@"{0}\{1}_{2}.log", strLogDir, DateTime.Now.ToString("yyyyMMdd"), fileName);
//로그 디렉토리가 존재하지 않으면 로그를 남기지 않는다.
if (!Directory.Exists(strLogDir))
{
return;
}
fs = new FileStream(strFullPath, FileMode.Append, FileAccess.Write, FileShare.Write);
sw = new StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " | " + message);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sw != null) sw.Close();
if (fs != null) fs.Close();
}
}
}
}