Created
December 12, 2016 17:17
-
-
Save lamw/ecef7afa7d260a962706e60fa910b9d4 to your computer and use it in GitHub Desktop.
Example of calling QuerySyncingVsanObjects() (http://pubs.vmware.com/vsphere-60/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.host.VsanInternalSystem.html) w/PowerCLI + vSphere MOB ###
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Author: William Lam | |
| # Blog: www.virtuallyghetto.com | |
| # Reference: http://www.virtuallyghetto.com/2016/07/how-to-automate-vsphere-mob-operations-using-powershell.html | |
| # Example of calling QuerySyncingVsanObjects() (http://pubs.vmware.com/vsphere-60/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.host.VsanInternalSystem.html) w/PowerCLI + vSphere MOB ### | |
| Function Get-VsanObjectStatus { | |
| param( | |
| [Parameter( | |
| Position=0, | |
| Mandatory=$true, | |
| ValueFromPipeline=$true, | |
| ValueFromPipelineByPropertyName=$true) | |
| ] | |
| [string]$esx_server, | |
| [String]$esx_username, | |
| [String]$esx_password | |
| ) | |
| $secpasswd = ConvertTo-SecureString $esx_password -AsPlainText -Force | |
| $credential = New-Object System.Management.Automation.PSCredential($esx_username, $secpasswd) | |
| # vSphere MOB URL to private enableMethods | |
| $mob_url = "https://$esx_server/mob/?moid=ha-vsan-internal-system&method=querySyncingVsanObjects" | |
| # Ingore SSL Warnings | |
| add-type -TypeDefinition @" | |
| using System.Net; | |
| using System.Security.Cryptography.X509Certificates; | |
| public class TrustAllCertsPolicy : ICertificatePolicy { | |
| public bool CheckValidationResult( | |
| ServicePoint srvPoint, X509Certificate certificate, | |
| WebRequest request, int certificateProblem) { | |
| return true; | |
| } | |
| } | |
| "@ | |
| [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
| # Initial login to vSphere MOB using GET and store session using $vmware variable | |
| $results = Invoke-WebRequest -Uri $mob_url -SessionVariable vmware -Credential $credential -Method GET | |
| # Extract hidden vmware-session-nonce which must be included in future requests to prevent CSRF error | |
| # Credit to https://blog.netnerds.net/2013/07/use-powershell-to-keep-a-cookiejar-and-post-to-a-web-form/ for parsing vmware-session-nonce via Powershell | |
| if($results.StatusCode -eq 200) { | |
| $null = $results -match 'name="vmware-session-nonce" type="hidden" value="?([^\s^"]+)"' | |
| $sessionnonce = $matches[1] | |
| } else { | |
| Write-host "Failed to login to vSphere MOB" | |
| exit 1 | |
| } | |
| # The POST data payload must include the vmware-session-nonce variable + URL-encoded | |
| $body = @" | |
| vmware-session-nonce=$sessionnonce&entity=%3Centity+type%3D%22ManagedEntity%22+xsi%3Atype%3D%22ManagedObjectReference%22%3E$vmmoref%3C%2Fentity%3E%0D%0A&method=%3Cmethod%3E$enable_method%3C%2Fmethod%3E | |
| "@ | |
| # Second request using a POST and specifying our session from initial login + body request | |
| $results = Invoke-WebRequest -Uri $mob_url -WebSession $vmware -Method POST -Body $body | |
| $cleanedUpResults = $results.ParsedHtml.body.innertext.split("`n").replace("`"","") | ? {$_.trim() -ne ""} | |
| # Loop through results looking for valtring which contains the data we want | |
| foreach ($parsedResults in $cleanedUpResults) { | |
| if($parsedResults -like "valstring*") { | |
| # JSON response | |
| $parsedResults.replace("valstring","") | |
| } | |
| } | |
| } | |
| ### Example of calling QuerySyncingVsanObjects() (http://pubs.vmware.com/vsphere-60/index.jsp?topic=%2Fcom.vmware.wssdk.apiref.doc%2Fvim.host.VsanInternalSystem.html) w/PowerCLI + vSphere MOB ### | |
| $esx_server = "192.168.1.50" | |
| $esx_username = "root" | |
| $esx_password = "vmware123" | |
| Get-VsanObjectStatus -esx_server $esx_server -esx_username $esx_username -esx_password $esx_password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment