Advertisement
Guest User

Untitled

a guest
Jan 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 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. #-----------------------------------------------------------------------------------------
  126. $scriptBlock = {
  127. #-----------------------------------------------------------------------------------------
  128. foreach($client in $clients) {
  129. $status = "OFFLINE"
  130. $isOnline = "OFFLINE"
  131.  
  132. if((Test-Connection -ComputerName $client -Quiet -Count 1 -Delay 1) -eq $true) {
  133. $isOnline = "ONLINE"
  134. }
  135.  
  136. # change the password
  137. try {
  138. $localAdminAccount = [adsi]"WinNT://$client/$adminuser,user"
  139. $localAdminAccount.SetPassword($adminPassword)
  140. $localAdminAccount.SetInfo()
  141. Write-Verbose "Password change completed successfully"
  142. }
  143. catch {
  144. $status = "FAILED"
  145. Write-Verbose "Failed to change password"
  146. }
  147.  
  148. # create psobject with system info
  149. $obj = New-Object -TypeName PSObject -Property @{
  150. ComputerName = $client
  151. Online = $isOnline
  152. ChangeStatus = $status
  153. }
  154.  
  155. $obj | Select computerName, Online, changeStatus | Out-File -FilePath C:test.txt -Append
  156.  
  157. if($status -eq "FAILED" -or $isOnline -eq "OFFLINE") {
  158. $stream.writeline("$client -t $status")
  159. }
  160. }
  161. }
  162. #-----------------------------------------------------------------------------------------
  163. Get-Job | Remove-Job -Force
  164.  
  165. Start-Job $scriptBlock -ArgumentList $_ -Name AdminPWReset
  166.  
  167. Get-Job
  168.  
  169. While(Get-Job -State "Running") {
  170. Start-Sleep -m 10
  171. }
  172.  
  173. Receive-Job -name AdminPWReset | Out-File C:test2.txt
  174. #-----------------------------------------------------------------------------------------
  175. $adminPassword = " "
  176.  
  177. Write-Verbose "Complete"
  178.  
  179. Invoke-Item C:test.txt
  180. Invoke-Item C:test2.txt
  181.  
  182. $scriptBlock = {
  183. foreach ($client in $clients) {
  184. ...
  185. }
  186. }
  187.  
  188. $scriptBlock = {
  189. foreach ($client in $using:clients) {
  190. ...
  191. }
  192. }
  193.  
  194. $scriptBlock = {
  195. foreach ($client in $args[0]) {
  196. ...
  197. }
  198. }
  199.  
  200. Start-Job -ScriptBlock $scriptBlock -ArgumentList $clients
  201.  
  202. Start-Job $scriptBlock -ArgumentList $_ -Name AdminPWReset
  203.  
  204. $scriptBlock = {
  205. Param($client)
  206.  
  207. $status = "OFFLINE"
  208. $isOnline = "OFFLINE"
  209.  
  210. if (Test-Connection -Computer $client -Quiet -Count 1 -Delay 1) {
  211. $isOnline = "ONLINE"
  212. }
  213. ...
  214. }
  215.  
  216. foreach ($client in $clients) {
  217. Start-Job -Name AdminPWReset -ScriptBlock $scriptBlock -ArgumentList $client
  218. }
  219.  
  220. while (Get-Job -State "Running") {
  221. Start-Sleep -Milliseconds 100
  222. }
  223.  
  224. Receive-Job -Name AdminPWReset | Out-File C:test2.txt
  225. Remove-Job -Name AdminPWReset
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement