MSBuild에서 ODBC DSN 생성하고 삭제하기

  • Post author:
  • Post category:
  • Post last modified:February 8, 2020

준비물: MSBuildTasks

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Import Project="BuildTool\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

<!-- 테스트용 DSN 정보 -->
<ItemGroup>
	<DSNToCreate Include="Item">
		<Name>DSN_NAME1</Name>
		<Server>DatabaseServer1</Server>
		<Database>InitialCatalog1</Database>
		<Description>ODBC DSN for the Database: DatabaseServer1</Description>
		<Driver>SQL Native Client</Driver>
		<LastUser>sa</LastUser>
		<Trusted_Connection>Yes</Trusted_Connection>
	</DSNToCreate>
	<DSNToCreate Include="Item">
		<Name>DSN_NAME2</Name>
		<Server>DatabaseServer2</Server>
		<Database>InitialCatalog2</Database>
		<Description>ODBC DSN for the Database: DatabaseServer2</Description>
		<Driver>SQL Native Client</Driver>
		<LastUser>sa</LastUser>
		<Trusted_Connection>Yes</Trusted_Connection>
	</DSNToCreate>
</ItemGroup>


<!-- DSN 삭제 -->
<PropertyGroup>
    <DelDSN>
		<![CDATA[
			public static string ScriptMain() {
				System.Collections.Specialized.StringCollection dsnNames = new System.Collections.Specialized.StringCollection();
	            dsnNames.Add("DSN_NAME1");
	            dsnNames.Add("DSN_NAME2");

	            string returnMsg = string.Empty;

	            using (Microsoft.Win32.RegistryKey OurKey = Microsoft.Win32.Registry.LocalMachine)
				{
	                foreach (string dsnName in dsnNames)
	                {
	                    using (Microsoft.Win32.RegistryKey dataSourcesKey = OurKey.OpenSubKey(@"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\", true))
	                    {
	                        try
	                        {
	                            dataSourcesKey.DeleteValue(dsnName);
	                        }
	                        catch (Exception ex)
	                        {
	                            returnMsg += Environment.NewLine;
	                            returnMsg += "While deleting a registry value " + @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\" + dsnName;
	                            returnMsg += Environment.NewLine;
	                            returnMsg += ex.ToString();
	                            returnMsg += Environment.NewLine;
	                        }
	                    }

	                    try
	                    {
	                        OurKey.DeleteSubKeyTree(@"SOFTWARE\ODBC\ODBC.INI\" + dsnName);
	                    }
	                    catch (Exception ex)
	                    {
	                        returnMsg += Environment.NewLine;
	                        returnMsg += "While deleting a registry key " + @"SOFTWARE\ODBC\ODBC.INI\" + dsnName;
	                        returnMsg += Environment.NewLine;
	                        returnMsg += ex.ToString();
	                        returnMsg += Environment.NewLine;
	                    }
					}
				}

	            return returnMsg;
			}
		]]>
    </DelDSN>
</PropertyGroup>


<!-- 깔끔하게 정리 -->
<Target Name="Clean">
	<Script Language="C#" Code="$(DelDSN)" Imports="System">
		<Output TaskParameter="ReturnValue" PropertyName="DelDSNResult" />
	</Script>
	<Message Text="DSN deletion: $(DelDSNResult)" />
</Target>


<!-- 테스트용 DSN 생성 -->
<Target Name="CreateDSN">
	<RegistryRead
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\%(DSNToCreate.Driver)"
		ValueName="Driver">
		<Output TaskParameter="Value" PropertyName="DriverPath" />
	</RegistryRead>
	<Message Text="DriverPath: $(DriverPath)" />

	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Server"
		Value="%(DSNToCreate.Server)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Database"
		Value="%(DSNToCreate.Database)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Description"
		Value="%(DSNToCreate.Description)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Driver"
		Value="$(DriverPath)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="LastUser"
		Value="%(DSNToCreate.LastUser)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\%(DSNToCreate.Name)"
		ValueName="Trusted_Connection"
		Value="%(DSNToCreate.Trusted_Connection)" />
	<RegistryWrite
		KeyName="HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\"
		ValueName="%(DSNToCreate.Name)"
		Value="%(DSNToCreate.Driver)" />
</Target>
</Project>
Author Details
Kubernetes, DevSecOps, AWS, 클라우드 보안, 클라우드 비용관리, SaaS 의 활용과 내재화 등 소프트웨어 개발 전반에 도움이 필요하다면 도움을 요청하세요. 지인이라면 가볍게 도와드리겠습니다. 전문적인 도움이 필요하다면 저의 현업에 방해가 되지 않는 선에서 협의가능합니다.