이번엔 남의 걸 들여다보는 게 아니라 PowerShell 함수를 처음부터 직접 짜 봤는데, 설명이 안 되는 방식으로 말을 안 들어요. 경로 하나만 돌려주면 되는 함수인데 배열이 돌아와요.
function Get-ReportPath {
$names = [System.Collections.ArrayList]::new()
foreach ($f in Get-ChildItem -Filter *.xlsx) {
$names.Add($f.Name)
}
Write-Verbose "found $($names.Count) workbooks"
Join-Path $PWD 'report.xlsx'
}
$path = Get-ReportPath
$path.GetType().Name # Object[]
폴더에 통합 문서가 세 개 있으면 $path에 값이 네 개 들어 있어요. 마지막이 제가 원한 경로고, 앞의 세 개는 0, 1, 2예요(쓴 적도 없는 카운터처럼 보여서 한참을 쳐다봤어요). 이 숫자들은 어디서 나오는 거고, 함수 안에서 쓰는 보조 리스트가 결과로 새어 나가지 않게 하려면 보통 어떻게 하나요?
영어에서 번역·원문 보기Writing a PowerShell function from scratch for once (rather than squinting at someone else's), and it misbehaves in a way I can't explain. It's meant to hand back one path. It hands back an array.
function Get-ReportPath {
$names = [System.Collections.ArrayList]::new()
foreach ($f in Get-ChildItem -Filter *.xlsx) {
$names.Add($f.Name)
}
Write-Verbose "found $($names.Count) workbooks"
Join-Path $PWD 'report.xlsx'
}
$path = Get-ReportPath
$path.GetType().Name # Object[]
With three workbooks in the folder, $path holds four things. The last is the path I wanted. The first three are 0, 1 and 2 (which looked like a counter I never wrote, and I stared at it for a good while). Where do those numbers come from, and what's the usual way to stop a helper list leaking into the result?