Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. # function to convert a secure string to a standard string
  2. function ConvertTo-String {
  3. param(
  4. [System.Security.SecureString] $secureString
  5. )
  6.  
  7. $marshal = [System.Runtime.InteropServices.Marshal]
  8.  
  9. try {
  10. $intPtr = $marshal::SecureStringToBSTR($secureString)
  11. $string = $marshal::PtrToStringAuto($intPtr)
  12. }
  13. finally {
  14. if($intPtr) {
  15. $marshal::ZeroFreeBSTR($intPtr)
  16. }
  17. }
  18. $string
  19. }
  20.  
  21. $clients = Get-Content -Path C:scriptsutilitieshostnames_online.txt
  22. $adminUser = "Administrator"
  23.  
  24. # prompt for password and confirm
  25. do {
  26. $ss1 = Read-Host "Enter new password" -AsSecureString
  27. $ss2 = Read-Host "Enter again to confirm" -AsSecureString
  28.  
  29. # compare strings - proceed if same - prompt again if different
  30. $ok = (ConvertTo-String $ss1) -ceq (ConvertTo-String $ss2)
  31. Write-Host "Passwords match"
  32.  
  33. if(-not $ok) {
  34. Write-Host "Passwords do not match"
  35. }
  36. }
  37. until($ok)
  38.  
  39. # set password variable to string value
  40. $adminPassword = ConvertTo-String $ss1
  41.  
  42. # setup job to reset password on each client
  43. foreach($client in $clients) {
  44. $status = "OFFLINE"
  45. $isOnline = "OFFLINE"
  46.  
  47. if((Test-Connection -ComputerName $client -Quiet -Count 1 -Delay 1) -eq $true) {
  48. $isOnline = "ONLINE"
  49. }
  50.  
  51. # change the password
  52. try {
  53. $localAdminAccount = [adsi]"WinNT://$client/$adminuser,user"
  54. $localAdminAccount.SetPassword($adminPassword)
  55. $localAdminAccount.SetInfo()
  56. Write-Verbose "Password change completed successfully"
  57. }
  58. catch {
  59. $status = "FAILED"
  60. Write-Verbose "Failed to change password"
  61. }
  62.  
  63. # create psobject with system info
  64. $obj = New-Object -TypeName PSObject -Property @{
  65. ComputerName = $client
  66. Online = $isOnline
  67. ChangeStatus = $status
  68. }
  69.  
  70. $obj | Select computerName, Online, changeStatus | Out-File -FilePath C:test.txt -Append
  71.  
  72. if($status -eq "FAILED" -or $isOnline -eq "OFFLINE") {
  73. $stream.writeline("$client -t $status")
  74. }
  75. }
  76.  
  77. $adminPassword = " "
  78.  
  79. Write-Verbose "Complete"
  80.  
  81. Invoke-Item C:test.txt
  82.  
  83. # function to convert a secure string to a standard string
  84. function ConvertTo-String {
  85. param(
  86. [System.Security.SecureString] $secureString
  87. )
  88.  
  89. $marshal = [System.Runtime.InteropServices.Marshal]
  90.  
  91. try {
  92. $intPtr = $marshal::SecureStringToBSTR($secureString)
  93. $string = $marshal::PtrToStringAuto($intPtr)
  94. }
  95. finally {
  96. if($intPtr) {
  97. $marshal::ZeroFreeBSTR($intPtr)
  98. }
  99. }
  100. $string
  101. }
  102.  
  103. $clients = Get-Content -Path C:scriptsutilitieshostnames_online.txt
  104. $adminUser = "Administrator"
  105.  
  106. # prompt for password and confirm
  107. do {
  108. $ss1 = Read-Host "Enter new password" -AsSecureString
  109. $ss2 = Read-Host "Enter again to confirm" -AsSecureString
  110.  
  111. # compare strings - proceed if same - prompt again if different
  112. $ok = (ConvertTo-String $ss1) -ceq (ConvertTo-String $ss2)
  113. Write-Host "Passwords match"
  114.  
  115. if(-not $ok) {
  116. Write-Host "Passwords do not match"
  117. }
  118. }
  119. until($ok)
  120.  
  121. # set password variable to string value
  122. $adminPassword = ConvertTo-String $ss1
  123.  
  124. # setup job to reset password on each client
  125. $scriptBlock = {
  126. foreach($client in $clients) {
  127. $status = "OFFLINE"
  128. $isOnline = "OFFLINE"
  129.  
  130. if((Test-Connection -ComputerName $client -Quiet -Count 1 -Delay 1) -eq $true) {
  131. $isOnline = "ONLINE"
  132. }
  133.  
  134. # change the password
  135. try {
  136. $localAdminAccount = [adsi]"WinNT://$client/$adminuser,user"
  137. $localAdminAccount.SetPassword($adminPassword)
  138. $localAdminAccount.SetInfo()
  139. Write-Verbose "Password change completed successfully"
  140. }
  141. catch {
  142. $status = "FAILED"
  143. Write-Verbose "Failed to change password"
  144. }
  145.  
  146. # create psobject with system info
  147. $obj = New-Object -TypeName PSObject -Property @{
  148. ComputerName = $client
  149. Online = $isOnline
  150. ChangeStatus = $status
  151. }
  152.  
  153. $obj | Select computerName, Online, changeStatus | Out-File -FilePath C:test.txt -Append
  154.  
  155. if($status -eq "FAILED" -or $isOnline -eq "OFFLINE") {
  156. $stream.writeline("$client -t $status")
  157. }
  158. }
  159. }
  160.  
  161. Get-Job | Remove-Job -Force
  162.  
  163. Start-Job $scriptBlock -ArgumentList $_ -Name AdminPWReset
  164.  
  165. Get-Job
  166.  
  167. While(Get-Job -State "Running") {
  168. Start-Sleep -m 10
  169. }
  170.  
  171. Receive-Job -name AdminPWReset | Out-File C:test2.txt
  172.  
  173. $adminPassword = " "
  174.  
  175. Write-Verbose "Complete"
  176.  
  177. Invoke-Item C:test.txt
  178. Invoke-Item C:test2.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement