我有带100个日期的testDates.csv
date 1/10/17 6/10/18 9/10/42 ...
我制作了一个脚本,可以根据以下日期创建示例密码:
Model
我想将创建的密码导出到新列中的原始.csv。因此,在“日期”旁边有一列“密码”,以查看我的脚本是否正常运行。示例:
date password 1/10/17 Winter17 6/10/18 Summer18 9/10/42 Fall42 ... ...
我知道它涉及$file = Import-Csv -Path U:\Desktop\testDates.csv
foreach ($line in $file) {
$fullDate = $line.date
$year = Get-Date $fullDate -Format "yy"
$currentDate = Get-Date $fullDate -Format "MM-dd"
# Determining season
if ($currentDate -ge (Get-Date 01-01) -and $currentDate -lt (Get-Date 03-01)) {
$season = "Winter"
} elseif ($currentDate -ge (Get-Date 03-01) -and $currentDate -le (Get-Date 05-31)) {
$season = "Spring"
} elseif ($currentDate -ge (Get-Date 06-01) -and $currentDate -le (Get-Date 08-31)) {
$season = "Summer"
} elseif ($currentDate -ge (Get-Date 09-01) -and $currentDate -le (Get-Date 11-30)) {
$season = "Fall"
} elseif ($currentDate -ge (Get-Date 12-01) -and $currentDate -le (Get-Date 12-31)) {
$season = "Winter"
} else {
$season = "ClearyAnIntern"
}
# Creating password
$SamplePassword = $season + $year
}
,但是我不知道如何特别是在每行中使用它。我一直在查看大量不同的线程,并在MS帮助中心中找到了如何使用诸如Export-Csv
之类的不同参数的方法,但是我所看到和尝试的一切都不完全是我要处理的内容。并且从这些示例中推断目前不在我的补救知识范围内。我开始相信这可能涉及嵌套的-Append
循环。
答案 0 :(得分:3)
Export-Csv
需要一个对象列表作为输入,因此这是您首先需要创建的。
foreach ($line in $file) {
...
[PSCustomObject]@{
'date' = $fullDate
'password' = $SamplePassword
}
}
接下来,foreach
循环无法写入管道,因此您要么需要在循环内附加到输出文件:
foreach ($line in $file) {
...
[PSCustomObject]@{
'date' = $fullDate
'password' = $SamplePassword
} | Export-Csv 'output.csv' -NoType -Append
}
或将输出收集到变量中,然后将其导出:
$list = foreach ($line in $file) {
...
[PSCustomObject]@{
'date' = $fullDate
'password' = $SamplePassword
}
}
$list | Export-Csv 'output.csv' -NoType
否则,您需要将foreach
循环替换为ForEach-Object
循环:
Import-Csv 'U:\Desktop\testDates.csv' | ForEach-Object {
$fullDate = $_.date
...
[PSCustomObject]@{
'date' = $fullDate
'password' = $SamplePassword
}
} | Export-Csv 'output.csv' -NoType
答案 1 :(得分:1)
您可以将Calculated properties与for循环结合使用。如下图所示-
#Importing csv dates file
$file = Import-Csv -Path U:\Desktop\testDates.csv
#Declaring the variable to store passwords
$SamplePassword = @()
#loop
foreach ($line in $file)
{
#Variable declarations
$fullDate = $line.date
$year = get-date $fullDate -Format "yy"
$currentDate = get-date $fullDate -Format "MM-dd"
#Determining season
if ($currentDate -ge (get-date 01-01) -and $currentDate -lt (get-date 03-01))
{$season = "Winter"}
elseif ($currentDate -ge (get-date 03-01) -and $currentDate -le (get-date 05-31))
{$season = "Spring"}
elseif ($currentDate -ge (get-date 06-01) -and $currentDate -le (get-date 08-31))
{$season = "Summer"}
elseif ($currentDate -ge (get-date 09-01) -and $currentDate -le (get-date 11-30))
{$season = "Fall"}
elseif ($currentDate -ge (get-date 12-01) -and $currentDate -le (get-date 12-31))
{$season = "Winter"}
else{
$season = "ClearyAnIntern"
}
#Creating password
$SamplePassword += $season + $year
}
完成上述步骤后,您的$SamplePassword
将包含所有密码。将计算的属性与for循环一起使用,如下所示-
#Declaring a new variable to store the results
$NewCsv = @()
$Originalcsv = Import-Csv -path U:\Desktop\testDates.csv
for($i = 0; $i -lt $Originalcsv.Length; $i++)
{
$NewCsv += $Originalcsv[$i] | Select-Object *, @{Name='Password';Expression={$SamplePassword[$i]}}
}
$NewCsv | Export-Csv U:\Desktop\NewtestDatesWithPasswords.csv -NoTypeInformation
答案 2 :(得分:1)
我只猜您的csv文件中的日期的格式为M/d/yy
?
从csv导入时,变量类型始终是字符串,因为我自己的语言环境有所不同,因此我必须定义不同的区域性进行转换。
使用我的function Get-Season
的其他问题的修改版本。
## Q:\Test\2018\06\21\SO_50976425.ps1
## define function first
Function Get-Season([datetime]$Date){
If (!$Date) {$Date = Get-Date} #If date was not provided, assume today.
# The dates are obviously not exactly accurate and are best guesses
$Spring = Get-Date -Day 20 -Month 03 -Year $Date.Year
$Summer = Get-Date -Day 21 -Month 06 -Year $Date.Year
$Autumn = Get-Date -Day 22 -Month 09 -Year $Date.Year
$Winter = Get-Date -Day 21 -Month 12 -Year $Date.Year
$Season = switch($Date) {
{($_ -lt $Spring)} {"Winter";Break}
{($_ -lt $Summer)} {"Spring";Break}
{($_ -lt $Autumn)} {"Summer";Break}
{($_ -lt $Winter)} {"Autumn";Break}
{($_ -ge $Winter)} {"Winter"}
}
"{0}{1}" -f $Season,$Date.ToString('yy')
}
#Importing csv dates file
$file = Import-Csv -Path '.\testDates.csv'
# get CultureInfo
$CIUS = New-Object System.Globalization.CultureInfo("en-US")
# process $file and create a new PSCustomObject
$DatePassw = foreach ($line in $file){
$fullDate = [datetime]::ParseExact($line.date,"M/d/yy",$CIUS)
[PsCustomObject]@{
date = $line.date
password = (Get-Season $fulldate)
}
}
$DatePassw | ft
$DatePassw | Export-Csv '.\testdatepassw.csv' -NoTypeInformation
示例输出:
> .\SO_50976425.ps1
date password
---- --------
1/10/17 Winter17
6/10/18 Spring18
9/10/42 Summer42
> gc .\testdatepassw.csv
"date","password"
"1/10/17","Winter17"
"6/10/18","Spring18"
"9/10/42","Summer42"
答案 3 :(得分:1)
Import-Csv C:\path\input.csv | ForEach-Object {
$seasons = [Char[]]'wwsssmmmfffw' # seasons map
}{ # walk around CSV lines
[PSCustomObject]@{
Date = $_.date # first and second columns of future CSV
Password = "$(switch ($seasons[([DateTime]$_.date).Month - 1]) {
'w' {'Winter'} 's' {'Spring'} 'm' {'Summer'} 'f' {'Fall'}
})$(Get-Date $_.date -Format yy)"
}
} | Export-Csv C:\path\out.csv # export result