<0001>;第三周;每周一上午12点至凌晨2点 002;第三周;每周二晚上8点至10点 003;第三周;每周一上午12点至凌晨2点
#Get the number of lines in a CSV file
$Lines = (Import-Csv "C:\MM1.csv").count
#Import the CSV file
$a = @(Import-CSV "C:\MM1.csv")
$month = Get-Date -Format MMMM
#loop around the end of the file
for ($i=0; $i -le $lines; $i++) {
$Servername = $a[$i].ServerName
$week = $a[$i].Week
$dayweekString = [String]$a[$i].DayTime
# This will help in getting the Day of the WeekDay String
$dayweekString = ($dayweekString -split "\s+",(3))[(1)]
#This will find the time Ex 2am or 8pm, it can be any time
$DayNew = if ($Day -match "\d{1,2}Travelm") {$Matches[0]}
#Format for Maintenance mode which can be fed into the SCOM MM Script.
$MaintenanceTime = get-date "$DayNew $month.$($_.$dayweekString).$year"
write-host $Servername, $MaintenanceTime
#Store all my data while in a for each loop
New-Object -TypeName PSObject -Property @{
ServerNameNew = $Servername
TimeStamp = $MaintenanceTime
} | Select-Object ServerNameNew,TimeStamp
} | Export-Csv -Path "C:\MM3.csv" -Append
# Error as extra Pipe
我无法在带有标题的循环中管道输出。说额外管道并将额外的TimeStamp变量行写入该行。
答案 0 :(得分:2)
您正在尝试将for
语句的结果传递给另一个命令。这不受支持。如果要这样做,则需要在语句周围使用子表达式:
$( for(){} ) | ...
(原因是管道需要一个表达式作为它们的第一个元素。for
不是表达式,它是一个声明。)
但是,在你的情况下,我会用一个遍历数组的简单管道替换for
,如下所示:
Import-CSV "C:\MM1.csv" | ForEach-Object {
$Servername = $_.ServerName
$week = $_.Week
$dayweekString = [String]$_.DayTime
...
}
通常,很少有理由在PowerShell中使用显式循环结构。它导致代码最多尴尬(因为它类似于转换的C#或VBScript代码)并且在最坏的情况下可怕。只是不要这样做。