Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ------------------------------------------------------------------------------------------ #
- #
- # ユーザアカウントパスワード変更依頼通知スクリプト
- #
- # Copyright (c) 2017 tag.
- #
- # ------------------------------------------------------------------------------------------ #
- # ------------------------------------------------------------------------------------------ #
- #
- # ## 概要
- # 指定したドメインのパスワード有効期限切れが迫っていることをユーザに通知する。
- #
- # ## 想定環境
- # 端末でログインするドメインと別ドメインのユーザアカウントを利用している環境下
- #
- # ## 利用想定
- # ログオンスクリプト (下記、スクリプトサンプル) やタスクスケジューラなどで仕込む
- # [NotifyPwdExpiration.bat] -------------------------------------------------------------- |
- # | @echo off |
- # | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\NotifyPwdExpiration.ps1 |
- # | -------------------------------------------------------------------------------------- |
- #
- # ## 注意
- # 初回起動時に対象ユーザのユーザ名とパスワードを入力する必要があります。
- # パスワードが間違っている場合や対象サーバと通信できないにも、再度求められます。
- #
- # ------------------------------------------------------------------------------------------ #
- # ------------------------------------------------------------------------------------------ #
- # 環境変数
- # FQDN名 (ドメイン名)
- $LdapHost = "contoso.local"
- # LDAPアドレス (変更不要)
- $LdapPath = "LDAP://${LdapHost}"
- # 資格情報保存ファイル (XMLファイル)
- $CredData = "${env:APPDATA}\${LdapHost}.dat"
- # 通知タイミング (期限切れの x 日前)
- $GraceDays = 14
- # ------------------------------------------------------------------------------------------ #
- # ------------------------------------------------------------------------------------------ #
- # 各種関数
- # 資格情報の読込
- function Load-Credential($CredData) {
- $Cred = $null
- if (Test-Path -Path $CredData) {
- $Cred = Import-Clixml $CredData
- }
- if (-not $Cred) {
- $Cred = Get-Credential -Message "${LdapHost} のユーザ名とパスワードを入力してください。"
- Save-Credential $Cred
- }
- return $Cred
- }
- # 資格情報の書出
- function Save-Credential($Cred) {
- $Cred | Export-Clixml $CredData
- }
- # パスワード有効日数取得
- function Get-MaxPwdAgeDays($LdapPath, $SAMAccountName, $Password) {
- $DsEntry = New-Object System.DirectoryServices.DirectoryEntry($LdapPath, $SAMAccountName, $Password)
- $DsSearch = New-Object System.DirectoryServices.DirectorySearcher($DsEntry)
- try {
- $Res = $DsSearch.FindOne()
- } catch {
- Remove-Item $CredData
- exit 1
- }
- return ([Int64][System.Math]::Abs($Res.properties.item("maxPwdAge")[0])) / 864000000000
- }
- # 自ユーザ情報の取得
- function Get-UserEntry($LdapPath, $SAMAccountName, $Password) {
- $DsEntry = New-Object System.DirectoryServices.DirectoryEntry($LdapPath, $SAMAccountName, $Password)
- $DsSearch = New-Object System.DirectoryServices.DirectorySearcher($DsEntry)
- $DsSearch.Filter = "(&(objectClass=user)(sAMAccountName=${SAMAccountName}))"
- try {
- $Res = $DsSearch.FindOne()
- } catch {
- Remove-Item $CredData
- exit 1
- }
- #$Res.Properties | % { $_ }
- return $Res.Properties
- }
- # パスワードの更新 (未完成)
- # LDAPS でないとそもそも変えられないらしい……ので諦めた
- # Ctrl-Alt-Del からできるやつは何なのよ……なんらかの Windows API なんだろうけど
- <#
- function Update-UserPassword($AdsPath, $SAMAccountName, $Password) {
- $Cred = Get-Credential -Message "${LdapHost} の新しいパスワードを入力してください。" -UserName $SAMAccountName
- Save-Credential $Cred
- $DsEntry = New-Object System.DirectoryServices.DirectoryEntry($AdsPath, $SAMAccountName, $Password)
- $DsEntry.Password = $Cred.GetNetworkCredential().Password
- $Res = $DsEntry.CommitChanges
- }
- #>
- # ------------------------------------------------------------------------------------------ #
- # ------------------------------------------------------------------------------------------ #
- # 処理
- # ユーザ資格情報の取得
- $Cred = Load-Credential $CredData
- $SAMAccountName = $Cred.UserName
- $Password = $Cred.GetNetworkCredential().Password
- # 対象ユーザ情報の取得
- $User = Get-UserEntry $LdapPath $SAMAccountName $Password
- # パスワード有効期限警告チェック
- $MaxPwdAge = Get-MaxPwdAgeDays $LdapPath $SAMAccountName $Password
- $PwdLastSet = [DateTime]::FromFileTime([String]$User.pwdlastset).DateTime
- $RemainedDays = $(New-TimeSpan $(Get-Date) ([DateTime]$PwdLastSet).AddDays($MaxPwdAge)).Days
- $ExpiredDate = Get-Date ([DateTime]$PwdLastSet).AddDays($MaxPwdAge) -Format "yyyy/MM/dd HH:mm:ss"
- if ($RemainedDays -gt $GraceDays) {
- Write-Host "パスワード変更の必要はありません。 (有効期限: ${ExpiredDate})"
- } else {
- Write-Host "パスワード変更の必要があります。 (有効期限: ${ExpiredDate})"
- [System.Windows.Forms.MessageBox]::Show("${LdapHost} のパスワード有効期限切れが迫っています。`nパスワードを変更してください。 (有効期限: $ExpiredDate)", "パスワード有効期限切れまであと ${RemainedDays} 日!")
- }
- # パスワード変更処理
- #Update-UserPassword $User.adspath $SAMAccountName $Password
- # ------------------------------------------------------------------------------------------ #
Add Comment
Please, Sign In to add comment