Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #How Integrates with Credentials
- # Override the default (en) if it exists in lang directory
- Import-LocalizedData -BaseDirectory ($ScriptPath + "\lang") -BindingVariable pLang -ErrorAction SilentlyContinue
- # Find the VI Server and port from the global settings file
- $VIServer = ($Server -Split ":")[0]
- if (($server -split ":")[1]) {
- $port = ($server -split ":")[1]
- }
- else
- {
- $port = 443
- }
- # Path to credentials file which is automatically created if needed
- $Credfile = $ScriptPath + "\Windowscreds.xml"
- # Adding PowerCLI core snapin, also check if powerCLI module is alsready added
- if (!(get-module -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {
- if (!(get-pssnapin -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {
- add-pssnapin VMware.VimAutomation.Core -erroraction silentlycontinue
- }
- }
- $OpenConnection = $global:DefaultVIServers | where { $_.Name -eq $VIServer }
- if($OpenConnection.IsConnected) {
- Write-CustomOut ( "{0}: {1}" -f $pLang.connReuse, $Server )
- $VIConnection = $OpenConnection
- } else {
- Write-CustomOut ( "{0}: {1}" -f $pLang.connOpen, $Server )
- $VIConnection = Connect-VIServer -Server $VIServer -Port $Port
- }
- if (-not $VIConnection.IsConnected) {
- Write-Error $pLang.connError
- }
- Write-CustomOut $pLang.custAttr
- ########################################
- # Part 2
- If (! $VCSA) {
- If (Test-Path $Credfile) {
- $LoadedCredentials = Import-Clixml $Credfile
- $creds = New-Object System.Management.Automation.PsCredential($LoadedCredentials.Username,($LoadedCredentials.Password | ConvertTo-SecureString))
- $Services = get-wmiobject -Credential $creds win32_service -ComputerName $VIServer -ErrorAction SilentlyContinue| Where {$_.DisplayName -like "VMware*" }
- } Else {
- $Services = get-wmiobject win32_service -ComputerName $VIServer -ErrorAction SilentlyContinue | Where {$_.DisplayName -like "VMware*" }
- if ($Error[0].Exception.Message -match "Access is denied.") {
- # Access Denied Error found so asking to store windows credentials in a file for future use
- 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"
- $Credential = Get-Credential
- $Pass = $credential.Password | ConvertFrom-SecureString
- $Username = $Credential.UserName
- $Store = "" | Select Username, Password
- $Store.Username = $Username
- $Store.Password = $Pass
- $Store | Export-Clixml $credfile
- $Services = get-wmiobject win32_service -ComputerName $VIserver -ErrorAction SilentlyContinue | Where {$_.DisplayName -like "VMware*" }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement