powershell logic confused

I am writing a powershell script and having a small problem with my logic. What I am trying to do is to get something to work if the number is 7, 5, 2 or 1. Everything is running but need to specify each instance, thanks

 if (($totals -eq 7 ) -or ($totals -eq 5 ) -or ($totals -eq 2) -or ($totals -lt 1)) {
   Do something
  }
}
techdriveAsked:
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.

footechCommented:
That's pretty much the way you do it.
Another option would be to build an array of the options and then compare against that.
$options = 7,5,2,1
if ($options -contains $totals) {
   Do something
}

Open in new window

0
Steven CarnahanNetwork ManagerCommented:
You could leave out the inner paran's:

if ($totals - eq 7 -or $totals -eq 5 -or $totals -eq 2 or $totals -lt 1)
{
     Do something
}

Open in new window

0
Rainer JeschorCommented:
Hi,
an integer array and using contains operator should di the trick:
$allowedValues = 1,2,5,7
if ($allowedValues -contains $totals) {
do
}
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
... and of course you do not need to define the array as variable:
if (1, 2, 5, 7 -contains $totals)
{
  # ...
}

Open in new window

0
techdriveAuthor Commented:
I have this block of code but I am stomped. The script executes fine but I only got this portion because this is the one I am having issues with. I want to be able to say if a users password expires on these days only I want to be able to only send email on the following days. The following for some reason is only processing the first date and sometimes the last day. Please help me with my logic on this one.

if ($daystoexpiry -eq 40 -and $daystoexpiry -eq 45 -and $daystoexpiry -eq 13 -and $daystoexpiry -lt 2) {
    $emailTo = "$mail" 
    $subject = "Your Network password will expire in $daystoexpiry day(s) please change your password."     
    Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -Attachments $attachment -SmtpServer $smtpserver 
    Write-Host "Email was sent to $mail on $today" 
    Add-Content c:\temp\maillog$logdate.tx<wbr ></wbr>t  "Email was sent to $mail on $today" 
  } 
} 
Send-MailMessage -To "mailadmin@somedomain.com"<wbr ></wbr> -From "helpdesk@somedomain.com" -Subject "Password change log for $today" -Body "This is the log from $today" -Attachments "c:\temp\maillog$logdate.t<wbr ></wbr>xt" -SmtpServer $smtpserver 

Open in new window

0
Steven CarnahanNetwork ManagerCommented:
Are you intentionally using -and in this line?

if ($daystoexpiry -eq 40 -and $daystoexpiry -eq 45 -and $daystoexpiry -eq 13 -and $daystoexpiry -lt 2){

Try using -or
0
techdriveAuthor Commented:
I tried using -or also. Let me paste the entire script. I found this on the net and it works well with one date. If you have multiple dates then this is when the issue starts with the logic.


Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
Get-QADUser -SizeLimit 0 | Select-Object samAccountName,mail,PasswordStatus |  
Where-Object {$_.PasswordStatus -ne "Password never expires" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} |  
ForEach-Object {
  $today = Get-Date
  $logdate = Get-Date -format yyyyMMdd
  $samaccountname = $_.samAccountName
  $mail = $_.mail  
  $passwordstatus = $_.PasswordStatus
  $passwordexpiry = $passwordstatus.Replace("Expires at: ","")
  $passwordexpirydate = Get-Date $passwordexpiry
  $daystoexpiry = ($passwordexpirydate - $today).Days
  $smtpserver = "FQDNmailserver"
  $attachment = "pathofdoc"
  $emailFrom = "emailaddress"
  $body = "Please change your password to prevent loss of access to the SBGTV network* systems`n`n"
  $body += "If you are unable to change your password, please contact the help desk at ext #### or ###.###.####"

if ($daystoexpiry -eq 40 -or $daystoexpiry -eq 45 -or $daystoexpiry -eq 13 -or $daystoexpiry -lt 2) {
    $emailTo = "$mail"
    $subject = "Your Network password will expire in $daystoexpiry day(s) please change your password."    
    Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -Attachments $attachment -SmtpServer $smtpserver
    Write-Host "Email was sent to $mail on $today"
    Add-Content c:\temp\maillog$logdate.txt  "Email was sent to $mail on $today"
  }
}
Send-MailMessage -To "addressemail" -From "addressemail" -Subject "Password change log for $today" -Body "This is the log from $today" -Attachments "c:\temp\maillog$logdate.txt" -SmtpServer $smtpserver
0
footechCommented:
I don't see any issue, and testing with your if statement works just fine.  You definitely want "-or".  Are you certain that you should be getting matches?  Try adding a command like this right above the If statement and manually look for expiry dates of exactly 45, 40, 13, 1, or 0 in the console.
Write-Output "$samaccountname expires - ""$daystoexpiry"""

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Aside from it seeming not to work, I prefer this line:
if (13, 40, 45 -contains $daystoexpiry -or $daystoexpiry -lt 2) {

Open in new window

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
Steven CarnahanNetwork ManagerCommented:
if (13, 40, 45 -contains $daystoexpiry -or $daystoexpiry -lt 2) {

if ($daystoexpiry -eq 40 -or $daystoexpiry -eq 45 -or $daystoexpiry -eq 13 -or $daystoexpiry -lt 2)

Nice job as usual Qlemo.  Although I still don't see why it didn't work originally.  The only difference I see is that your is much easier to read/code.   (Aside from the fact I have trouble with it looking backwards)  :)
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Yes, that "backwards logic" is somewhat strange; an IN operator would be easier to read:
  if ($daystoexpiry -in 13, 40, 45 -or $daystoexpiry -lt 2)
but sadly there is no such operator.
0
footechCommented:
Actually, in PS 3.0 there is an -in operator, and it works just like that.
I also don't see why the original didn't work for the OP, as it works in testing the same as the -contains operator.
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.