developer tip

Powershell에서 "@"기호는 무엇을합니까?

copycodes 2020. 8. 17. 09:12
반응형

Powershell에서 "@"기호는 무엇을합니까?


PowerShell에서 배열을 초기화하는 데 사용되는 @ 기호를 보았습니다. @ 기호는 정확히 무엇을 나타내며 이에 대한 자세한 내용은 어디에서 읽을 수 있습니까?


PowerShell은 실제로 쉼표로 구분 된 목록을 배열로 처리합니다.

"server1","server2"

따라서 @는 이러한 경우 선택 사항입니다. 그러나 연관 배열의 경우 @가 필요합니다.

@{"Key"="Value";"Key2"="Value2"}

공식적으로 @는 "배열 연산자"입니다. 이에 대한 자세한 내용은 PowerShell과 함께 설치 한 설명서 또는 공동 저작 한 "Windows PowerShell : TFM"과 같은 책에서 읽을 수 있습니다.


PowerShell V2에서 @는 Splat 연산자 이기도합니다 .

PS> # First use it to create a hashtable of parameters:
PS> $params = @{path = "c:\temp"; Recurse= $true}
PS> # Then use it to SPLAT the parameters - which is to say to expand a hash table 
PS> # into a set of command line parameters.
PS> dir @params
PS> # That was the equivalent of:
PS> dir -Path c:\temp -Recurse:$true

위의 답변은 대부분답변을 제공하지만 질문이 늦었더라도 전체 답변을 제공하는 것이 유용합니다.

배열 하위 표현식 ( about_arrays 참조 )

값이 단일 또는 null 인 경우에도 배열이되도록 강제합니다. 예 : $a = @(ps | where name -like 'foo')

해시 이니셜 라이저 ( about_hash_tables 참조 )

키-값 쌍으로 해시 테이블을 초기화합니다. 예 : $HashArguments = @{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }

스플래 (참조 about_splatting )

예를 들어 바로 위에있는 해시 테이블을 사용하여보다 관례적인 개별적으로 열거 된 매개 변수 대신 배열 또는 해시 테이블의 매개 변수를 사용하여 cmdlet을 호출 해 보겠습니다. Copy-Item @HashArguments

여기 문자열 ( about_quoting_rules 참조 )

일반적으로 여러 줄 문자열에 사용되는 쉽게 포함 된 따옴표로 문자열을 생성 해 보겠습니다. 예 :

$data = @"
line one
line two
something "quoted" here
"@

이러한 유형의 질문 ( PowerShell에서 'x'표기법은 무엇을 의미합니까? )이 여기 StackOverflow와 많은 독자 의견에서 매우 일반적이기 때문에 Simple-Talk.com에 방금 게시 한 PowerShell 구두점 어휘집을 작성했습니다. @, % 및 #, $ _ 및? PowerShell 구두점대한 전체 가이드 에서 더 많은 내용을 확인할 수 있습니다. 기사에 첨부 된이 월 차트는 한 장에 모든 것을 제공합니다.여기에 이미지 설명 입력


cmdlet (또는 파이프 라인)의 출력을 래핑하여 @()반환되는 항목이 단일 항목이 아닌 배열인지 확인할 수도 있습니다.

For instance, dir usually returns a list, but depending on the options, it might return a single object. If you are planning on iterating through the results with a foreach-object, you need to make sure you get a list back. Here's a contrived example:

$results = @( dir c:\autoexec.bat)

One more thing... an empty array (like to initialize a variable) is denoted @().


The Splatting Operator

To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol. Let's take the discussion above and use an array to connect to multiple remote computers:

$strComputers = @("Server1", "Server2", "Server3")<enter>

They are used for arrays and hashes.

PowerShell Tutorial 7: Accumulate, Recall, and Modify Data

Array Literals In PowerShell

참고URL : https://stackoverflow.com/questions/363884/what-does-the-symbol-do-in-powershell

반응형