除非你絕對需要一個控制檯界面,你可以,你可以使用Out-GridView
讓用戶選擇,如:
Get-ChildItem C:\PROD\transfert | Out-GridView -Title 'Choose a file' -PassThru | ForEach-Object { $_.FullName }
編輯 ...在一個變量來存儲。 ..
$filenames = @(Get-ChildItem C:\PROD\transfert | Out-GridView -Title 'Choose a file' -PassThru)
@()
的包裝確保總是返回的文件名的陣列(即使選擇一個文件或沒有文件)。
(在Passthru
依賴於你具有的PowerShell 3或更大)
編輯2
選擇菜單下面將改變顯示類型,這取決於是否在控制檯或GUI (如ISE)。我沒有通過WinRM進行測試,但是當它通過普通的PowerShell控制檯調用時,它不應該產生GUI。
$files = Get-ChildItem -Path C:\PROD\transfert -File
$fileChoices = @()
for ($i=0; $i -lt $files.Count; $i++) {
$fileChoices += [System.Management.Automation.Host.ChoiceDescription]("$($files[$i].Name) &$($i+1)")
}
$userChoice = $host.UI.PromptForChoice('Select File', 'Choose a file', $fileChoices, 0) + 1
# do something more useful here...
Write-Host "you chose $($files[$userChoice].FullName)"
小心怎樣的文件很多都是由Get-ChildItem
雅回來了,這是一種方式,如果他不想要一個基於GUI的事情。 :) –
非常感謝答案,它正在做我想做的事情。然而,這個腳本可能會用於WinRM,Out-GridView的工作嗎? – kyori
不,@kyori,你需要WinRM的命令行版本。 – TechSpud