我有一个需要在PowerShell上运行的命令,我可以通过提供一个变量直接运行它。示例:
command run 2017 Jan
但是我的要求是针对每个变量列表在循环内运行命令:
Jan Feb Mar .... Oct Nov Dec
$ACSautoPath = "C:\sap\rexon\bin\rexon.bat"
$AutomationPath = "C:\Sap\Automation"
$LogPath = "C:\Sap\Automation\Log"
$BackupPath = "C:\Sap\Automation\Backup"
#Date Variables
$DaysToKeep = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($DaysToKeep)
$TimeStamp = Get-Date -format "yyyyMMddHHmm"
$LogFileName = "Backup" + $TimeStamp + ".log"
#ACS Variables
$ACSdomain = "yourdomain"
$ACSurl = "XXXXXXXXXXXXXX"
$ACSuser = "yourusername"
$ACSpass = "c:\ Sap\Automation\password.epw"
#Snapshot Variables
$ACSExportName = "Snapshot"
$ACSExportDownloadName = $ACSExportName + ".zip"
$ACSExportRename = $ACSExportName + $TimeStamp + ".zip"
#Start Logging
Start-Transcript -path $LogPath\$LogFileName
#Log into ACS
Write-Host ([System.String]::Format("Login to source: {0}", [System.DateTime]::Now))
&$ACSautoPath "login" $ACSuser $ACSpass $ACSurl
#Run A rule
Write-Host ([System.String]::Format("Run A rule: {0}",[System.DateTime]::Now))
&$ACSautoPath Runrule cons india 2017 jan
#Log out of ACS
Write-Host ([System.String]::Format("Logout of source: {0}",[System.DateTime]::Now))
&$ACSautoPath "logout"
#Stop Logging
Stop-Transcript
在第27行,用户必须提供类似于Mar的变量,然后必须对列表中的所有时段执行第27行,如下所述,
我应该在中间提供任何句点,例如Mar
,然后循环必须运行以下命令:
apr may jun july aug sep oct nov dec jan feb
如何实现?
答案 0 :(得分:1)
您可以使用modulus operator从任何起点循环浏览几个月:
$months = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AbbreviatedMonthNames
$start = $months.IndexOf('Jul')
1 .. 12 | ForEach-Object { $months[++$start % 12] }
哪个给出这样的输出:
Aug
Sep
Oct
Nov
Dec
Jan
Feb
Mar
Apr
May
Jun
Jul
编辑:已更新,其中包含了来自更为谨慎的@LotPings的反馈
答案 1 :(得分:0)
因此$ACSautoPath
是您向其传递参数的批处理文件的路径,
但是初始值2017 jan
的来源是什么?
我假设您要从本月开始并重复下一个11:
$Start = Get-Date # or set a fixed date with # -year 2017 -month 1
0..11|ForEach-Object {
$Params = "Runrule cons india {0}" -f $Start.AddMonths($_).ToString('yyyy MMM')
&$ACSautoPath $Params
}
将$ACSautoPath
暂时设置为Echo
后,我得到以下输出:
Runrule cons india 2018 Nov
Runrule cons india 2018 Dez
Runrule cons india 2019 Jan
Runrule cons india 2019 Feb
Runrule cons india 2019 Mrz
Runrule cons india 2019 Apr
Runrule cons india 2019 Mai
Runrule cons india 2019 Jun
Runrule cons india 2019 Jul
Runrule cons india 2019 Aug
Runrule cons india 2019 Sep
Runrule cons india 2019 Okt