Use the name of last month in PowerShell output file -


at moment i'm using powershell run sql query on sql server database , output results excel spreadsheet.

it outputs via

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 $(get-date -f dd-mm-yyyy)" -path "c:\scripts-and-reports\15-05-2017 $(get-date -f yyyy-mm-dd) taree pe new clients.xlsx" -force -verbose 

which working fine because i'm manually specifying date filter in sql query, in worksheetname, , in file name.

i aim automate entire process runs on 1st of each month , reports on entire previous month.

i've since modified sql query filter previous month, i'd have output file's worksheet , filename include previous month's name (eg. "august" if running today).

i've tried

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 

but doesn't work. error string not being valid datetime.

where have gone wrong , should be?

get-date addmonths(-1) -f mmmm  

this line mix of different syntaxes, , doesn't fit together.

get-date cmdlet takes parameter -format can shortened -f get-date returns [datetime] object  .addmonths() method on [datetime] object, not parameter get-date 

change to:

(get-date).addmonths(-1).tostring('mmmm') 

which gets date, calls addmonths on return value, uses datetime way of converting text, because can't use -format parameter here, because cmdlet call finished earlier.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -