目前,我正在使用Powershell在SQL Server数据库上运行SQL查询,并将结果输出到Excel电子表格.
它目前输出通过
invoke-sqlcmd -inputfile "C:\scripts-and-reports\all_new-matters-since-last-run.sql" -serverinstance "PE-SERVER\PRACTICEEVOLVE" -database "PracticeEvolve_c1" |
Select-Object * -ExcludeProperty "RowError","RowState","Table","ItemArray","HasErrors" |
Export-XLSX -WorksheetName "15-05-2017 to $(get-date -f dd-MM-yyyy)" -Path "C:\scripts-and-reports\15-05-2017 to $(get-date -f yyyy-MM-dd) Taree PE new clients.xlsx" -Force -Verbose
Run Code Online (Sandbox Code Playgroud)
这是正常工作,因为我在SQL查询,WorksheetName和文件名中手动指定日期过滤器.
我的目标是使整个过程自动化,使其在每个月的第一天运行并报告整个上个月.
我现在已经修改了我的SQL查询以过滤上个月,但我想让输出文件的工作表和文件名只包含上个月的名称(例如,如果我今天运行它,则为"八月").
我试过了
invoke-sqlcmd -inputfile "C:\scripts-and-reports\all_new-matters-for-last-month.sql" -serverinstance "PE-SERVER\PRACTICEEVOLVE" -database "PracticeEvolve_c1" |
Select-Object * -ExcludeProperty "RowError","RowState","Table","ItemArray","HasErrors" |
Export-XLSX -WorksheetName "$(get-date AddMonths(-1) -f MMMM)" -Path "C:\scripts-and-reports\$(get-date AddMonths(-1) -f MMMM) Taree PE new clients.xlsx" -Force -Verbose
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我收到有关字符串不是有效DateTime的错误.
我哪里出错了,它应该是什么?
get-date AddMonths(-1) -f MMMM
Run Code Online (Sandbox Code Playgroud)
这一行是不同语法的混合,并不适合.
Get-Date is a cmdlet which takes a parameter -Format which can be shortened to -f
Get-Date returns a [datetime] object
.AddMonths() is a method on a [datetime] object, not a parameter to Get-Date
Run Code Online (Sandbox Code Playgroud)
改成:
(Get-Date).AddMonths(-1).ToString('MMMM')
Run Code Online (Sandbox Code Playgroud)
获取日期,然后在该返回值上调用AddMonths,然后使用datetime方式将其转换为文本,因为您不能在-Format此处使用该参数,因为cmdlet调用之前已完成.
| 归档时间: |
|
| 查看次数: |
2444 次 |
| 最近记录: |