シスメモ

Linux(CentOS)、Windows(サーバー/クライアント)、Yamaha RTX、その他の技術メモ

Windows Server

【VBScript】ActiveDirectoryのパスワード期限が来た時にメールで通知

投稿日:

photo credit: akeg via photopin cc

photo credit: akeg via photopin cc

ADの特定ユーザーのパスワードが有効期限まで3日に迫った場合に、特定のアドレスにアラートメールを送信したい・・という要望があったので、スクリプトをVBSで適当に作ってみました。
ADメンバーサーバー上のタスクで、1日1回実行するよう設定しています。

【参考】

Option Explicit

Dim objUser
Dim intUserAccountControl
Dim dtmValue
Dim intTimeInterval
Dim objDomain
Dim objMaxPwdAge
Dim dblMaxPwdNano
Dim dblMaxPwdSecs
Dim dblMaxPwdDays
Dim objSysInfo
Dim oMsg
Set objSysInfo = CreateObject(“ADSystemInfo”)

Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
Const ONE_HUNDRED_NANOSECOND = .000000100
Const SECONDS_IN_DAY = 86400

Set objUser = GetObject(“LDAP://cn=hogehoge_user,cn=Users,dc=hogehoge,dc=local”)

‘##### パスワード有効期限有無の確認 #####
intUserAccountControl = objUser.Get(“userAccountControl”)
If intUserAccountControl And ADS_UF_DONT_EXPIRE_PASSWD Then
WScript.Quit
Else
dtmValue = objUser.PasswordLastChanged
intTimeInterval = Int(Now – dtmValue)

Set objDomain = GetObject(“LDAP://DC=hogehoge,DC=local”)
Set objMaxPwdAge = objDomain.Get(“maxPwdAge”)

‘##### パスワード有効期限確認 #####
If objMaxPwdAge.LowPart = 0 Then
WScript.Quit
Else
dblMaxPwdNano = _
Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart)
dblMaxPwdSecs = dblMaxPwdNano * ONE_HUNDRED_NANOSECOND
dblMaxPwdDays = Int(dblMaxPwdSecs / SECONDS_IN_DAY)

‘##### パスワード期限切れの確認 #####
If intTimeInterval >= dblMaxPwdDays Then
WScript.quit
ElseIf Int((dtmValue + dblMaxPwdDays) – Now) < 4 Then Set oMsg = CreateObject("CDO.Message") oMsg.From = "<送信元メールアドレス>”
oMsg.To = “<送信先メールアドレス>”
oMsg.Cc = “”
oMsg.Subject = “パスワード期限3日前です”
oMsg.TextBody = “ユーザーhogehoge_userのパスワード有効期限まであと ” & _
Int((dtmValue + dblMaxPwdDays) – Now) & ” 日です! 変更して下さい!”
oMsg.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
oMsg.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = _

oMsg.Configuration.Fields.Item _
(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25
oMsg.Configuration.Fields.Update
oMsg.Send
Else
WScript.quit
End If
End If
End If



-Windows Server
-, ,

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

リモートからリモートデスクトップを許可する

photo credit: EL JOKER THE PYRA via photopin (license)  ■リモートからリモートデスクトップを許可する 【環境】 サーバー:Window …

グループポリシーでのIE信頼済みサイトへの追加

photo credit: trackrecord via photopin cc ■グループポリシーでのIE信頼済みサイトへの追加 信頼済みサイトへの追加は、2通りある。 方法1. ユーザーの構成 …

ドメイン参加とコンピュータアカウントの移動を同時に行う

photo credit: Michael Kappel via photopin cc   ドメイン参加後にコンピュータアカウントを手動で移動するのが面倒なため、ドメイン参加&コンピュータ …

Windows Server 2012評価版から製品版への移行

photo credit: marsmet543 via photopin cc 1.コマンドプロンプト(管理者)を起動し、以下のコマンドを実行する。 コマンド:DISM /online /Get-C …

ログオンしたドメインコントローラの確認

photo credit: dawhitfield via photopin cc ドメインにログオンした時のドメインコントローラを確認したい場合、以前は「set logonserver」コマンドを実 …