Link to home
Start Free TrialLog in
Avatar of techdrive
techdriveFlag for United States of America

asked on

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
  }
}
Avatar of footech
footech
Flag of United States of America image

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

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

Hi,
an integer array and using contains operator should di the trick:
$allowedValues = 1,2,5,7
if ($allowedValues -contains $totals) {
do
}
... and of course you do not need to define the array as variable:
if (1, 2, 5, 7 -contains $totals)
{
  # ...
}

Open in new window

Avatar of techdrive

ASKER

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

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
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
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

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)  :)
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.
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.