about_Data_Sections

업데이트 날짜: 2014년 5월

적용 대상: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

여기에 소개를 삽입합니다.

항목

about_Data_Sections

간단한 설명

텍스트 문자열 및 기타 읽기 전용 데이터를 스크립트 논리와 분리하는 Data 섹션에 대해 설명합니다.

자세한 설명

Windows PowerShell®용으로 설계된 스크립트에는 데이터만 포함된 Data 섹션이 하나 이상 있을 수 있습니다. 스크립트, 함수 또는 고급 함수에는 Data 섹션을 하나 이상 포함할 수 있습니다. Data 섹션의 내용은 Windows PowerShell 스크립트 언어의 지정된 하위 집합으로 제한됩니다.

데이터를 코드 논리와 분리하면 논리와 데이터를 모두 쉽게 식별하고 관리할 수 있습니다. 그러면 오류 메시지 및 도움말 문자열과 같은 별도의 텍스트용 문자열 리소스 파일을 사용할 수 있습니다. 보안 및 유효성 검사 테스트를 쉽게 수행할 수 있도록 코드 논리도 분리됩니다.

Windows PowerShell에서는 Data 섹션을 사용하여 스크립트 국제화를 지원합니다. Data 섹션을 사용하여 여러 UI(사용자 인터페이스) 언어로 변환될 문자열을 보다 쉽게 격리하거나 찾거나 처리할 수 있습니다.

데이터 섹션은 Windows PowerShell 2.0 기능입니다. Data 섹션이 있는 스크립트를 Windows PowerShell 1.0에서 실행하려면 스크립트를 수정해야 합니다.

구문

Data 섹션에 대한 구문은 다음과 같습니다.

DATA [-supportedCommand <cmdlet-name>] {

            <Permitted content>
        }

Data 키워드가 필요합니다. 이 키워드는 대/소문자를 구분하지 않습니다.

허용된 내용은 다음 요소로 제한됩니다.

- All Windows PowerShell operators, except -match 
           
        - If, Else, and ElseIf statements
           
- The following automatic variables: $PsCulture,  $PsUICulture,  $True,
          $False, and $Null

        - Comments

        - Pipelines

        - Statements separated by semicolons (;)

        - Literals, such as the following:

            a

            1
  
            1,2,3

            "Windows PowerShell 2.0"

            @( "red", "green", "blue" )

            @{ a = 0x1; b = "great"; c ="script" }

            [XML] @'
             <p> Hello, World </p>
            '@

        - Cmdlets that are permitted in a Data section. By default, only the 
          ConvertFrom-StringData cmdlet is permitted.

        - Cmdlets that you permit in a Data section by using the 
          SupportedCommand parameter.

Data 섹션에서 ConvertFrom-StringData cmdlet을 사용하면 키/값 쌍을 작은따옴표 또는 큰따옴표 문자열이나 작은따옴표 또는 큰따옴표 here-string으로 묶을 수 있습니다. 그러나 변수와 하위 식이 포함된 문자열은 변수가 확장되지 않고 하위 식이 실행 가능하지 않도록 작은따옴표 문자열 또는 작은따옴표 here-string으로 묶어야 합니다.

SUPPORTEDCOMMAND

SupportedCommand 매개 변수를 사용하면 cmdlet 또는 함수에서 데이터만 생성됨을 나타낼 수 있습니다. 이 변수는 사용자가 작성하거나 테스트한 데이터 섹션에 cmdlet과 함수를 포함할 수 있도록 설계되었습니다.

SupportedCommand의 값은 cmdlet 또는 함수 이름 하나 이상을 쉼표로 구분한 목록입니다.

예를 들어 다음 데이터 섹션에는 XML 파일의 데이터 서식을 지정하는, 사용자 작성 cmdlet인 Format-XML이 포함됩니다.

  DATA -supportedCommand Format-XML 
          {    
             Format-XML -strings string1, string2, string3
          }
       

DATA 섹션 사용

Data 섹션의 내용을 사용하려면 이 섹션을 변수에 할당하고 변수 표기법을 사용하여 내용에 액세스합니다.

예를 들어 다음 데이터 섹션에는 here-string을 해시 테이블로 변환하는 ConvertFrom-StringData 명령이 있습니다. 해시 테이블은 $TextMsgs 변수에 할당됩니다.

$TextMsgs 변수는 데이터 섹션의 일부가 아닙니다.

$TextMsgs = DATA {
              ConvertFrom-StringData -stringdata @'
                Text001 = Windows 7
                Text002 = Windows Server 2008 R2
          '@
          }

$TextMsgs에서 해시 테이블의 키와 값에 액세스하려면 다음 명령을 사용합니다.

$TextMsgs.Text001      
          $TextMsgs.Text002

예제

단순 데이터 문자열입니다.

        DATA {
            "Thank you for using my Windows PowerShell Organize.pst script."
            "It is provided free of charge to the community."
            "I appreciate your comments and feedback."
        }

허용되는 변수가 포함된 문자열입니다.

        DATA {
            if ($null) {
       "To get help for this cmdlet, type get-help new-dictionary."
            }
        }

ConvertFrom-StringData cmdlet을 사용하는 작은따옴표로 묶인 here-string입니다.

        DATA {
          ConvertFrom-StringData -stringdata @'
            Text001 = Windows 7
            Text002 = Windows Server 2008 R2
        '@
        }

ConvertFrom-StringData cmdlet을 사용하는 큰따옴표로 묶인 here-string입니다.

        DATA  {
          ConvertFrom-StringData -stringdata @"
            Msg1 = To start, press any key.
            Msg2 = To exit, type "quit".
        "@
        }

다음 데이터를 생성하는 사용자 작성 cmdlet이 포함된 데이터 섹션입니다.

DATA -supportedCommand Format-XML {    
           Format-XML -strings string1, string2, string3
        }

참고 항목

about_Automatic_Variables

about_comparison_operators

about_Hash_Tables

about_If

about_Operators

about_Quoting_Rules

about_Script_Internationalization

ConvertFrom-StringData

Import-LocalizedData