问题描述
我正在尝试做一个简单的读取主机并将用户输入导出到一个 csv 文件。
$List = Read-Host -Prompt 'Enter List of files (exclude file extention)'|
Export-Csv "c:\user.csv" -NoType@R_95_4045@ion
我在我的文件中得到的只是:
"length"
"42"
用户输入:
filename1
filename2
export-csv C:\list.csv
应该有:
filename1
filename2
谢谢!
解决方法
您提到使用 PSCustomObject
,那么为什么不使用它呢?
$R_Input = Read-Host -Prompt "Exclude extension! `nEnter File Name"
$R_Input = $R_Input -split ","
[PSCustomObject]@{
"First File Name" = $R_Input[0]
"Second File Name" = $R_Input[1]
} | Export-Csv -Path "c:\user.csv" -NoTypeInformation -Force -Append
<#Input
Exclude extension!
Enter File Name: Path Number One,Path Number two
#>
<#OutPut
PS C:\WINDOWS\system32> Import-Csv c:\user.csv
First File Name Second File Name
--------------- ----------------
Name Number One Name Number two
#>
编辑:
如果您不关心文件名是否在同一列中,您可以使用 for
循环遍历所有输入(用逗号分隔),并传递到PSCustomObject
。现在他们输入多少名字不重要了。
$R_Input = Read-Host -Prompt "Exclude extension! `nEnter File Name"
$R_Input = $R_Input.Trim() -split ","
for ($i=0; $i -lt $R_Input.Count; $i++) {
[PSCustomObject]@{
"File Name" = $R_Input[$i]
} | Export-Csv -Path "c:\user.csv" -NoTypeInformation -Force -Append
}