Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. If (! $VCSA) {
  2.     If (Test-Path $Credfile) {
  3.         $LoadedCredentials = Import-Clixml $Credfile
  4.         $creds = New-Object System.Management.Automation.PsCredential($LoadedCredentials.Username,($LoadedCredentials.Password | ConvertTo-SecureString))
  5.         $Services = get-wmiobject -Credential $creds win32_service -ComputerName $VIServer -ErrorAction SilentlyContinue| Where {$_.DisplayName -like "VMware*" }
  6.     } Else {
  7.         $Services = get-wmiobject win32_service -ComputerName $VIServer -ErrorAction SilentlyContinue | Where {$_.DisplayName -like "VMware*" }
  8.         if ($Error[0].Exception.Message -match "Access is denied.") {
  9.             # Access Denied Error found so asking to store windows credentials in a file for future use
  10.             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"
  11.             $Credential = Get-Credential
  12.             $Pass = $credential.Password | ConvertFrom-SecureString
  13.             $Username = $Credential.UserName
  14.             $Store = "" | Select Username, Password
  15.             $Store.Username = $Username
  16.             $Store.Password = $Pass
  17.             $Store | Export-Clixml $credfile
  18.         $Services = get-wmiobject win32_service -ComputerName $VIserver -ErrorAction SilentlyContinue | Where {$_.DisplayName -like "VMware*" }
  19.         }
  20.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement