JakeBentz

Change-Root-Pwd.ps1

Jan 31st, 2013
189
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # /=======================================================================
  2. # /=
  3. # /=  Change-Root-Pwd.ps1
  4. # /=
  5. # /=  AUTHOR: Jake Bentz
  6. # /=  DATE: 01/25/2013
  7. # /=
  8. # /=  REQUIREMENTS: N/A
  9. # /=
  10. # /=  DESCRIPTION: This script changes the root password on all ESX hosts in the esxservers.txt textfile
  11. # /=
  12. # /=  REVISION HISTORY
  13. # /=   VER  DATE        AUTHOR/EDITOR   COMMENT
  14. # /=   1.0  01/25/2013   Jake Bentz  Created script
  15. # /=
  16. # /=======================================================================#
  17. #
  18. # Create log folder if it doesn't exist
  19. $Logpath = "e:\logs\ESX"
  20. If ((Test-Path -Path $Logpath) -ne $true) { New-Item -ItemType directory -Path $Logpath}
  21. #
  22. # Create log file
  23. $Logfile = "e:\logs\ESX\ESX_Root"+ (get-date -format '_ddMMMyyyy_hhmm') +".txt"
  24. Write "================================================================" > $Logfile
  25. Write "Root Password Update Output File" >> $Logfile
  26. Write "================================================================" >> $Logfile
  27. #
  28. # Add VI-toolkit #
  29. Add-PSsnapin VMware.VimAutomation.Core
  30. Initialize-VIToolkitEnvironment.ps1# Get old root credential
  31. $oldrootPassword = Read-Host ìEnter old root passwordî -AsSecureString
  32. $oldrootCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist ìrootî,$oldrootPassword
  33.  
  34. # Get new root credential
  35. $newrootPassword = Read-Host ìEnter new root passwordî -AsSecureString
  36. $newrootCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist ìrootî,$newrootPassword
  37. $newrootPassword2 = Read-Host ìRetype new root passwordî -AsSecureString
  38. $newrootCredential2 = new-object -typename System.Management.Automation.PSCredential -argumentlist ìrootî,$newrootPassword2
  39.  
  40. # Compare passwords
  41. If ($newrootCredential.GetNetworkCredential().Password -ceq $newrootCredential2.GetNetworkCredential().Password) {
  42.  
  43.  # Create new root account object
  44.  $rootaccount = New-Object VMware.Vim.HostPosixAccountSpec
  45.  $rootaccount.id = ìrootî
  46.  $rootaccount.password = $newrootCredential.GetNetworkCredential().Password
  47.  $rootaccount.shellAccess = ì/bin/bashî
  48.  
  49.  # Get list of Host servers from textfile to change root password on
  50.  Get-Content esxservers.txt | %{
  51.   Connect-VIServer $_ -User root -Password $oldrootCredential.GetNetworkCredential().Password -ErrorAction SilentlyContinue -ErrorVariable ConnectError | Out-Null
  52.   If ($ConnectError -ne $Null) {
  53.    Write-Host ìERROR: Failed to connect to ESX server:î $_
  54.    $Output = "ERROR: Failed to connect to ESX server: "
  55.    $Output = $Output += $_
  56.    Write $Output >> $Logfile
  57.   }
  58.   Else {
  59.    $si = Get-View ServiceInstance
  60.    $acctMgr = Get-View -Id $si.content.accountManager
  61.    $acctMgr.UpdateUser($rootaccount)
  62.    Write-Host ìRoot password successfully changed onî $_
  63.    $Output = ìRoot password successfully changed on: î
  64.    $Output = $Output += $_
  65.    Write $Output >> $Logfile
  66.       Disconnect-VIServer * -Confirm:$False | Out-Null
  67.   }
  68.  }
  69. }
  70. Else {
  71. Write-Host ìERROR: New root passwords do not match. Exiting...î
  72. $Output = ìERROR: New root passwords do not match. Exiting...î
  73. Write $Output >> $Logfile
  74. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment