Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #How Integrates with Credentials
  2.  
  3. # Override the default (en) if it exists in lang directory
  4. Import-LocalizedData -BaseDirectory ($ScriptPath + "\lang") -BindingVariable pLang -ErrorAction SilentlyContinue
  5.  
  6. # Find the VI Server and port from the global settings file
  7. $VIServer = ($Server -Split ":")[0]
  8. if (($server -split ":")[1]) {
  9.    $port = ($server -split ":")[1]
  10. }
  11. else
  12. {
  13.    $port = 443
  14. }
  15.  
  16. # Path to credentials file which is automatically created if needed
  17. $Credfile = $ScriptPath + "\Windowscreds.xml"
  18.  
  19. # Adding PowerCLI core snapin, also check if powerCLI module is alsready added
  20. if (!(get-module -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {
  21.     if (!(get-pssnapin -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {
  22.         add-pssnapin VMware.VimAutomation.Core -erroraction silentlycontinue
  23.     }
  24. }
  25.  
  26. $OpenConnection = $global:DefaultVIServers | where { $_.Name -eq $VIServer }
  27. if($OpenConnection.IsConnected) {
  28.     Write-CustomOut ( "{0}: {1}" -f $pLang.connReuse, $Server )
  29.     $VIConnection = $OpenConnection
  30. } else {
  31.     Write-CustomOut ( "{0}: {1}" -f $pLang.connOpen, $Server )
  32.     $VIConnection = Connect-VIServer -Server $VIServer -Port $Port
  33. }
  34.  
  35. if (-not $VIConnection.IsConnected) {
  36.     Write-Error $pLang.connError
  37. }
  38.  
  39. Write-CustomOut $pLang.custAttr
  40.  
  41. ########################################
  42.  
  43. # Part 2
  44.  
  45. If (! $VCSA) {
  46.     If (Test-Path $Credfile) {
  47.         $LoadedCredentials = Import-Clixml $Credfile
  48.         $creds = New-Object System.Management.Automation.PsCredential($LoadedCredentials.Username,($LoadedCredentials.Password | ConvertTo-SecureString))
  49.         $Services = get-wmiobject -Credential $creds win32_service -ComputerName $VIServer -ErrorAction SilentlyContinue| Where {$_.DisplayName -like "VMware*" }
  50.     } Else {
  51.         $Services = get-wmiobject win32_service -ComputerName $VIServer -ErrorAction SilentlyContinue | Where {$_.DisplayName -like "VMware*" }
  52.         if ($Error[0].Exception.Message -match "Access is denied.") {
  53.             # Access Denied Error found so asking to store windows credentials in a file for future use
  54.             Write-Host "Current windows credentials do not allow for access to WMI on the host $VIServer, please enter Administrator credentials for this check to work, these will be stored in an encrypted file: $credfile"
  55.             $Credential = Get-Credential
  56.             $Pass = $credential.Password | ConvertFrom-SecureString
  57.             $Username = $Credential.UserName
  58.             $Store = "" | Select Username, Password
  59.             $Store.Username = $Username
  60.             $Store.Password = $Pass
  61.             $Store | Export-Clixml $credfile
  62.         $Services = get-wmiobject win32_service -ComputerName $VIserver -ErrorAction SilentlyContinue | Where {$_.DisplayName -like "VMware*" }
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement