But, errors:<\/strong><\/p>\n
Cannot find an overload for “op_Subtraction” and the argument count: “2”. Method invocation failed because [System.Management.Automation.PSMethod] does not contain a method named ‘addDays’. Exception calling “Send” with “1” argument(s): “Failure sending mail.” Cannot convert value to type System.String. Cannot convert value to type System.String. Please help me!!!<\/p>","upvoteCount":7,"answerCount":19,"datePublished":"2023-03-08T18:24:53.000Z","author":{"@type":"Person","name":"spiceuser-pdenz","url":"https://community.spiceworks.com/u/spiceuser-pdenz"},"suggestedAnswer":[{"@type":"Answer","text":" But, errors:<\/strong><\/p>\n Cannot find an overload for “op_Subtraction” and the argument count: “2”. Method invocation failed because [System.Management.Automation.PSMethod] does not contain a method named ‘addDays’. Exception calling “Send” with “1” argument(s): “Failure sending mail.” Cannot convert value to type System.String. Cannot convert value to type System.String. Please help me!!!<\/p>","upvoteCount":7,"datePublished":"2023-03-08T18:24:53.000Z","url":"https://community.spiceworks.com/t/active-directory-and-powershell-notify-change-passsword/947756/1","author":{"@type":"Person","name":"spiceuser-pdenz","url":"https://community.spiceworks.com/u/spiceuser-pdenz"}},{"@type":"Answer","text":" Welcome.
\nAt line:47 char:1<\/p>\n\n
<\/span>, MethodException<\/li>\n
\n
\nAt line:55 char:1<\/p>\n\n
<\/span>, RuntimeException<\/li>\n
\n
\nAt line:18 char:1<\/p>\n\n
<\/span>, MethodInvocationException<\/li>\n
\n
\nAt line:87 char:1<\/p>\n\n
<\/span>, RuntimeException<\/li>\n
\n
\nAt line:88 char:1<\/p>\n\n
<\/span>, RuntimeException<\/li>\n
\nFunction Get-UAC($uac)\n{\n$flags=@()\nif ((65536 -band $uac) -ne \"0\"){$flags+=\"DONT_EXPIRE_PASSWORD\"}\nreturn $flags\n}\nFunction enviar-mail($mail,$body)\n{\n$subject=\"Recordatorio cambio de contraseña\"\n$smtpServer = \"correo365.pichincha.fin.ec\"\n$emailFrom = \"[email protected]\"\n$smtp = new-object Net.Mail.SmtpClient($smtpServer,587)\n$mailAuthentication = new-object System.Net.NetworkCredential(\"unlockda\", \"Unl0ckD4\")\n$smtp.UseDefaultCredentials = $False\n$smtp.Credentials =$mailAuthentication\n$MailMessage = new-object Net.Mail.MailMessage($emailFrom, $mail, $subject, $body)\n$MailMessage.IsBodyHtml = $true\n$smtp.Send($MailMessage)\n}\n##### main #####\nout-file \"C:\\Bats\\mailing_caducidad_contraseñas_08032023.csv\" -input \"usuario;ultimocambio;fechacaducidad;dias;avisado\"\n$SECS_IN_A_DAY = new-object DirectoryServices.DirectorySearcher([adsi]\"\")\n$SECS_IN_A_DAY=86400\n$domain = [ADSI]\"WinNT://$env:userdomain\"\n$intMaxPwdAge =($domain.maxpasswordage.value)/$SECS_IN_A_DAY\n$ahora=get-date\n\n#lista los datos de todos los usuarios del dominio\n$searcher = New-Object DirectoryServices.DirectorySearcher([adsi]\"\")\n$searcher.filter = \"(&(objectclass=user)(objectcategory=person)(mail=*)(!userAccountControl:1.2.840.113556.1.4.803:=2))\"\n$users = $searcher.findall()\n\nForeach($user in $users)\n{\n$TheUser = new-object DirectoryServices.DirectorySearcher([adsi]\"\")\n$TheUser=$user.GetDirectoryEntry()\n\n$usuario=$theuser.samaccountname\n$nombre=$theuser.givenname\n$apellidos=$theuser.sn\n$mail=$theuser.mail\n$uac=$theUser.userAccountControl\n$flags=Get-Uac(\"$uac\")\nif ($flags -notcontains \"DONT_EXPIRE_PASSWORD\")\n{\n$PasswordLastChanged =$theuser.PasswordLastChanged\n$intTimeInterval = [int32]($ahora - $PasswordLastChanged).days\nIf ($intTimeInterval -ge $intMaxPwdAge)\n{\n$strExpiringDays=\"CADUCADA\"\n$avisar=0\n}\nelse\n{\n$ExpiringDate = $PasswordLastChanged.addDays($intMaxPwdAge)\n$fechacaducidad=$Expiringdate.toString(\"dd/MM/yyyy\")\n$ExpiringDays = [int32]($ExpiringDate - $ahora).days\n$strExpiringDays= \"$ExpiringDays dias\"\n#Avisar con 4 dias de antelacion\nif ($ExpiringDays -le 4)\n{\n$avisar=1\n$body= \"Estimad@ <br> <br>\n\n$nombre $apellidos, su usuario de red <b>$usuario</b>, va a caducar dentro de \" + $ExpiringDays + \" días (\" + $fechacaducidad + \").<br>\nPor favor, desde su computador cambiar la contraseña presionando <b> CTL + ALT + SUPR </b>, seleccionar la opción <b>Cambiar la Contraseña</b>, luego:<br><br>\n\nValidar <b>Nombre de usuario.</b><br>\nColocar la <b>contraseña actual.</b><br>\nColocar la <b>contraseña Nueva.</b><br>\nVolver a ingresar la <b>contraseña Nueva.</b><br>\nFinalmente, proceder a aceptar.<br> <br>\n\nPara desbloquear su usuario o cambiar contraseña lo puede hacer desde la Intranet Institucional en el botón <b>Desbloqueo de usuario de red.</b><br>\n\nAgradecemos su colaboración.<br> <br>\n\nAtentamente<br>\n<b>Service Desk.</b>\"\nenviar-mail $mail $body\n}\nelse\n{\n$avisar=0\n}#fin de if 4 dias\n}#fin de if caducada\nwrite-host \"$usuario - $PasswordLastChanged - $strExpiringDays ($avisar)\" -fore cyan\nout-file \"C:\\Bats\\mailing_caducidad_contraseñas_08032023.csv\" -input \"$usuario;$PasswordLastChanged;$fechacaducidad;$strExpiringDays;$avisar\" -append\n\n}#fin de flags uac validos\n\n}#fin foreach\n<\/code><\/pre>\n
\nAt line:47 char:1<\/p>\n\n
<\/span>, MethodException<\/li>\n
\n
\nAt line:55 char:1<\/p>\n\n
<\/span>, RuntimeException<\/li>\n
\n
\nAt line:18 char:1<\/p>\n\n
<\/span>, MethodInvocationException<\/li>\n
\n
\nAt line:87 char:1<\/p>\n\n
<\/span>, RuntimeException<\/li>\n
\n
\nAt line:88 char:1<\/p>\n\n
<\/span>, RuntimeException<\/li>\n
\n
\nWe’re happy to help but not a script writing service.
\nIf you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>