这是我到目前为止所拥有的:
一个名为 C:\TEMP\AIST2330TP04\SampleEnrollees.csv 的 csv 文件,其中包含以下信息:
OrgDefinedId,Username,LastName,FirstName,Email,Section
1,tuser,User,Test,tuser@mymail.com,AIST2330
2,duser,User,Demo,duser@mymail.com,AIST2330
3,istudent,Student,Ima,istudent@mymail.com,AIST2330
4,ustudent,Student,Ura,ustudent@mymail.com,AIST2330
5,cfarquar,Farquar,Clyde,cfarquar@mymail.com,AIST2330
我的代码被参数化并获取某些信息并输出文本文件:
#this parameter is a requirement
param(
[string]$WorkDir = 'C:\Users\cryanderson\Documents'
)
#this is just so that the process can be run multiple times without creating multiple folders and files
Remove-Item 'DemoFiles' -Recurse -ErrorAction Ignore
mkdir 'DemoFiles'
$csv=Import-Csv C:\TEMP\AIST2330TP04\SampleEnrollees.csv
#I want to make sure I am using existing properties, so I display them here
echo $csv
$csv | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty
'Name'
#The loop that I am having issues with
ForEach($line in $csv)`
{
$username = $_.Username,`
$lastname = $_.LastName,`
$firstname = $_.FirstName,`
$text = "Generated for $firstname $lastname by cryanderson.",`
$text |Out-File
"C:\Users\cryanderson\Documents\DemoFiles\${username}Stuff.txt"
}
当我运行它时,会发生以下情况,但没有创建任何文件:
PS C:\Users\cryanderson\documents>
C:\Users\cryanderson\Documents\CreateDemoFiles.ps1
Directory: C:\Users\cryanderson\documents
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 3/11/2018 8:54 PM DemoFiles
OrgDefinedId : 1
Username : tuser
LastName : User
FirstName : Test
Email : tuser@mymail.com
Section : AIST2330
OrgDefinedId : 2
Username : duser
LastName : User
FirstName : Demo
Email : duser@mymail.com
Section : AIST2330
OrgDefinedId : 3
Username : istudent
LastName : Student
FirstName : Ima
Email : istudent@mymail.com
Section : AIST2330
OrgDefinedId : 4
Username : ustudent
LastName : Student
FirstName : Ura
Email : ustudent@mymail.com
Section : AIST2330
OrgDefinedId : 5
Username : cfarquar
LastName : Farquar
FirstName : Clyde
Email : cfarquar@mymail.com
Section : AIST2330
Email
FirstName
LastName
OrgDefinedId
Section
Username
#This error message is repeated for all 5 users listed
The property 'FirstName' cannot be found on this object. Verify that the
property exists and can be set.
At C:\Users\cryanderson\Documents\CreateDemoFiles.ps1:14 char:14
+ $firstname = $_.FirstName,`
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
我认为我的 for 循环存在问题,但我无法弄清楚。请帮忙?