Powershell scripting

Dear Expert

I need help with reading and understanding the Powershell if statement, please read the statement below.

What I understand is:
If $months is 1 do this {
I dont understand the -gt? what is -gt?
$months +1
This I understand, variable months +1
0..$months | % {
This I dont understand 0..? Something and do follow.
$dateQuery = ( get-date (get-date $dateFilter).AddMonths( -1 ) )
This I understand partly, variable $dateQuery will get, get-date as command in PS, and () inside here I don't understand, and it addmonths -1 means it minus 1
if ( (get-date $dateQuery) -le (get-date) ) {
This I understand mostly but what is -le?
$sqlArr += f_sqlquery $dateQuery $SQLDBName
This I understand.
} else {
      $sqlArr += f_sqlquery $dateFilter $SQLDBName
This I understand.

if ($months -gt 1) {
      $months++
      0..$months | % {
            $dateQuery = ( get-date (get-date $dateFilter).AddMonths( -1 ) )
            if ( (get-date $dateQuery) -le (get-date) ) {
                  $sqlArr += f_sqlquery $dateQuery $SQLDBName
            }
      }
} else {
      $sqlArr += f_sqlquery $dateFilter $SQLDBName
LVL 1
WeTiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
-gt is the "Greater Than" comparison operator. Unlike other programming/scripting languages, where all kinds of strange character combinations are used to define all kinds of operators, PS went down the road to define them all with a beginning hyphen and a more or less descriptive short string.
See Get-Help about_Operators -ShowWindow or the web:
About Operators
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6

0..$months will result in an array from 0 to $months; so if $months is 2, it would be 0, 1, 2

% is an alias for the ForEach-Object cmdlet.

-lt is the "Less Than" comparison operator, see above.

(GetDate).AddMonths(-1) will get the current date, and immediately subtract one month from it.

The date query part is - well, somewhat unconventional.
From your former question (https://www.experts-exchange.com/questions/29084776/Oschestrator-Powershell-script-problem.html), it seems that $dateFilter is the string date representation required by SQL, and that you changed the code.

The conversion to the string representation of the date should be done only when passing the variable to the function, not when retrieving date objects to run comparisons with.
This should be the equivalent:
           $dateQuery = (Get-Date).AddMonths( -1 )
             if ($dateQuery -le (get-date)) {
                   $sqlArr += f_sqlquery $dateQuery.ToString($dateFilter) $SQLDBName
             }

Open in new window



           $dateQuery = ( get-date (get-date $dateFilter).AddMonths( -1 ) )
             if ( (get-date $dateQuery) -le (get-date) ) {
                   $sqlArr += f_sqlquery $dateQuery $SQLDBName
             }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
great explained thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.