Guest User

Untitled

a guest
Nov 27th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. # ------------------------------------------------------------------------------------------ #
  2. #
  3. # ユーザアカウントパスワード変更依頼通知スクリプト
  4. #
  5. # Copyright (c) 2017 tag.
  6. #
  7. # ------------------------------------------------------------------------------------------ #
  8.  
  9. # ------------------------------------------------------------------------------------------ #
  10. #
  11. # ## 概要
  12. # 指定したドメインのパスワード有効期限切れが迫っていることをユーザに通知する。
  13. #
  14. # ## 想定環境
  15. # 端末でログインするドメインと別ドメインのユーザアカウントを利用している環境下
  16. #
  17. # ## 利用想定
  18. # ログオンスクリプト (下記、スクリプトサンプル) やタスクスケジューラなどで仕込む
  19. # [NotifyPwdExpiration.bat] -------------------------------------------------------------- |
  20. # | @echo off |
  21. # | C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\NotifyPwdExpiration.ps1 |
  22. # | -------------------------------------------------------------------------------------- |
  23. #
  24. # ## 注意
  25. # 初回起動時に対象ユーザのユーザ名とパスワードを入力する必要があります。
  26. # パスワードが間違っている場合や対象サーバと通信できないにも、再度求められます。
  27. #
  28. # ------------------------------------------------------------------------------------------ #
  29.  
  30. # ------------------------------------------------------------------------------------------ #
  31. # 環境変数
  32.  
  33. # FQDN名 (ドメイン名)
  34. $LdapHost = "contoso.local"
  35. # LDAPアドレス (変更不要)
  36. $LdapPath = "LDAP://${LdapHost}"
  37. # 資格情報保存ファイル (XMLファイル)
  38. $CredData = "${env:APPDATA}\${LdapHost}.dat"
  39. # 通知タイミング (期限切れの x 日前)
  40. $GraceDays = 14
  41.  
  42. # ------------------------------------------------------------------------------------------ #
  43.  
  44. # ------------------------------------------------------------------------------------------ #
  45. # 各種関数
  46.  
  47. # 資格情報の読込
  48. function Load-Credential($CredData) {
  49. $Cred = $null
  50. if (Test-Path -Path $CredData) {
  51. $Cred = Import-Clixml $CredData
  52. }
  53. if (-not $Cred) {
  54. $Cred = Get-Credential -Message "${LdapHost} のユーザ名とパスワードを入力してください。"
  55. Save-Credential $Cred
  56. }
  57. return $Cred
  58. }
  59.  
  60. # 資格情報の書出
  61. function Save-Credential($Cred) {
  62. $Cred | Export-Clixml $CredData
  63. }
  64.  
  65. # パスワード有効日数取得
  66. function Get-MaxPwdAgeDays($LdapPath, $SAMAccountName, $Password) {
  67. $DsEntry = New-Object System.DirectoryServices.DirectoryEntry($LdapPath, $SAMAccountName, $Password)
  68. $DsSearch = New-Object System.DirectoryServices.DirectorySearcher($DsEntry)
  69. try {
  70. $Res = $DsSearch.FindOne()
  71. } catch {
  72. Remove-Item $CredData
  73. exit 1
  74. }
  75. return ([Int64][System.Math]::Abs($Res.properties.item("maxPwdAge")[0])) / 864000000000
  76. }
  77.  
  78. # 自ユーザ情報の取得
  79. function Get-UserEntry($LdapPath, $SAMAccountName, $Password) {
  80. $DsEntry = New-Object System.DirectoryServices.DirectoryEntry($LdapPath, $SAMAccountName, $Password)
  81. $DsSearch = New-Object System.DirectoryServices.DirectorySearcher($DsEntry)
  82. $DsSearch.Filter = "(&(objectClass=user)(sAMAccountName=${SAMAccountName}))"
  83. try {
  84. $Res = $DsSearch.FindOne()
  85. } catch {
  86. Remove-Item $CredData
  87. exit 1
  88. }
  89. #$Res.Properties | % { $_ }
  90. return $Res.Properties
  91. }
  92.  
  93. # パスワードの更新 (未完成)
  94. # LDAPS でないとそもそも変えられないらしい……ので諦めた
  95. # Ctrl-Alt-Del からできるやつは何なのよ……なんらかの Windows API なんだろうけど
  96. <#
  97. function Update-UserPassword($AdsPath, $SAMAccountName, $Password) {
  98. $Cred = Get-Credential -Message "${LdapHost} の新しいパスワードを入力してください。" -UserName $SAMAccountName
  99. Save-Credential $Cred
  100. $DsEntry = New-Object System.DirectoryServices.DirectoryEntry($AdsPath, $SAMAccountName, $Password)
  101. $DsEntry.Password = $Cred.GetNetworkCredential().Password
  102. $Res = $DsEntry.CommitChanges
  103. }
  104. #>
  105.  
  106. # ------------------------------------------------------------------------------------------ #
  107.  
  108. # ------------------------------------------------------------------------------------------ #
  109. # 処理
  110.  
  111. # ユーザ資格情報の取得
  112. $Cred = Load-Credential $CredData
  113. $SAMAccountName = $Cred.UserName
  114. $Password = $Cred.GetNetworkCredential().Password
  115.  
  116. # 対象ユーザ情報の取得
  117. $User = Get-UserEntry $LdapPath $SAMAccountName $Password
  118.  
  119. # パスワード有効期限警告チェック
  120. $MaxPwdAge = Get-MaxPwdAgeDays $LdapPath $SAMAccountName $Password
  121. $PwdLastSet = [DateTime]::FromFileTime([String]$User.pwdlastset).DateTime
  122. $RemainedDays = $(New-TimeSpan $(Get-Date) ([DateTime]$PwdLastSet).AddDays($MaxPwdAge)).Days
  123. $ExpiredDate = Get-Date ([DateTime]$PwdLastSet).AddDays($MaxPwdAge) -Format "yyyy/MM/dd HH:mm:ss"
  124. if ($RemainedDays -gt $GraceDays) {
  125. Write-Host "パスワード変更の必要はありません。 (有効期限: ${ExpiredDate})"
  126. } else {
  127. Write-Host "パスワード変更の必要があります。 (有効期限: ${ExpiredDate})"
  128. [System.Windows.Forms.MessageBox]::Show("${LdapHost} のパスワード有効期限切れが迫っています。`nパスワードを変更してください。 (有効期限: $ExpiredDate)", "パスワード有効期限切れまであと ${RemainedDays} 日!")
  129. }
  130.  
  131. # パスワード変更処理
  132. #Update-UserPassword $User.adspath $SAMAccountName $Password
  133.  
  134. # ------------------------------------------------------------------------------------------ #
Add Comment
Please, Sign In to add comment