<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Code - Virtually Everything: Your Guide to Virtualization</title>
    <link>https://virtualg.uk/category/vmware/code/</link>
    <atom:link href="https://virtualg.uk/category/vmware/code/feed/" rel="self" type="application/rss+xml" />
    <description>Scripts I've written for VMware admin work, mostly PowerShell and PowerCLI: automatic VCSA certificate renewal, the vROps REST API and faster scripts.</description>
    <language>en-GB</language>
    <lastBuildDate>Sun, 13 Aug 2023 17:06:40 +0000</lastBuildDate>
    <item>
      <title>Renewing your VCSA web certificate automatically</title>
      <link>https://virtualg.uk/renewing-your-vcsa-web-certificate-automatically/</link>
      <guid isPermaLink="true">https://virtualg.uk/renewing-your-vcsa-web-certificate-automatically/</guid>
      <pubDate>Sun, 13 Aug 2023 17:06:40 +0000</pubDate>
      <dc:creator><![CDATA[Graham Barker]]></dc:creator>
      <description><![CDATA[In this article I've provided a script which can be used to replace the default SSL certificate on your VCSA with a new signed certificate by your enterprise certificate…]]></description>
      <content:encoded><![CDATA[<p>In this article I've provided a script which can be used to replace the default SSL certificate on your VCSA with a new signed certificate by your enterprise certificate authority.</p>



<p>Further, you can also use this same script to replace your signed certificate, prior to it expiring.</p>



<h2 class="wp-block-heading" id="Background">Background <a class="hash" href="#Background" aria-label="Link to this section"></a></h2>



<p>The manual process to replace your VCSA SSL certificate is simplified in recent versions of the VCSA, however this is still a manual process which should be completed at least every year. If you're interested in my article on how to manually replace your VCSA Web SSL Certificate, check here: <a href="https://virtualg.uk/how-to-sign-your-vmware-vcenter-server-web-certificate/">How to manually sign your vCenter Server SSL Certificate</a></p>



<figure class="wp-block-image size-full"><a href="https://virtualg.uk/how-to-sign-your-vmware-vcenter-server-web-certificate/" target="_blank" rel="noreferrer noopener"><img src="https://virtualg.uk/wp-content/uploads/2023/08/image-2.png" alt="" class="wp-image-12026" loading="eager" decoding="async"></a></figure>



<p>If you have environments with many vCenter Servers it is not ideal to complete this process manually, this is especially true when you end up having to replace multiple certificates at the same time, before a deadline.</p>



<p>Manual installation of signed SSL certificates is also a top reason why often those responsible for the organization's VCSAs neglect to install a signed SSL certificate in the first instance - It is just too much work to manage on top of already increasing workloads.</p>







<h2 class="wp-block-heading" id="The_script_to_replace_your_vCenter_Server_Web_SSL_Certificate">The script to replace your vCenter Server Web SSL Certificate <a class="hash" href="#The_script_to_replace_your_vCenter_Server_Web_SSL_Certificate" aria-label="Link to this section"></a></h2>



<p>Using PowerShell, the vCenter REST APIs and Microsoft's <em>certreq </em>tool, we can manage the complete lifecycle of VCSA Web SSL certificates!</p>



<h3 class="wp-block-heading" id="The_script_performs_the_following_steps">The script performs the following steps: <a class="hash" href="#The_script_performs_the_following_steps" aria-label="Link to this section"></a></h3>



<ul>
<li>Creates a Certificate Signing Request (CSR) on the VCSA (Leaving the private key on the system for best security practices)</li>



<li>Downloads the CSR</li>



<li>Sends the CSR to your Microsoft Certificate Authority for signing, with a given CA Template</li>



<li>Downloads the signed certificate</li>



<li>Uploads the certificate and certificate chain to the VCSA</li>



<li>Restarts the VCSA services to make the new certificate take effect</li>
</ul>



<h3 class="wp-block-heading" id="What_do_you_need_to_use_the_script">What do you need to use the script? <a class="hash" href="#What_do_you_need_to_use_the_script" aria-label="Link to this section"></a></h3>



<p><strong>Permissions</strong></p>



<ul>
<li>You will need to run the PowerShell script with an account that is able to sign certificates with your Microsoft Certificate Authority.</li>
</ul>



<p><strong>CA Template</strong></p>



<ul>
<li>The Microsoft Certificate Authority comes with a default "WebServer" Signing Template. You should clone, enable and adjust the configuration based on your preferences.</li>



<li> Ensure the user you run the script as, has permission to sign against this template. <a rel="noreferrer noopener" href="https://learn.microsoft.com/en-us/windows-server/networking/core-network-guide/cncg/server-certs/configure-the-server-certificate-template" target="_blank">More info</a>.</li>
</ul>



<p><strong>Certificate Chain</strong></p>



<ul>
<li>The script does not currently create the certificate chain by itself. You'll need to obtain this yourself from your CA.</li>
</ul>



<p><strong>Certreq </strong></p>



<ul>
<li>The script uses the certreq executable, this is available on most Windows operating systems, so be sure to run your PowerShell script on one with certreq available and in the PATH environment variable. To validate this, open a PowerShell window and type <code>certreq -?</code> to see if the application responds with the help information.</li>
</ul>



<p><strong>Other</strong></p>



<ul>
<li>Naturally you will also need credentials for your VCSA with permission to mange certificates and a few other details for the CSR. You can find all the configurable variables in the example.</li>
</ul>



<h3 class="wp-block-heading" id="How_do_I_use_the_script">How do I use the script? <a class="hash" href="#How_do_I_use_the_script" aria-label="Link to this section"></a></h3>



<p>At the bottom of the VCSA.ps1 script there are a number of variables to populate. Set these variables then execute the script.</p>



<figure class="wp-block-image size-full"><img src="https://virtualg.uk/wp-content/uploads/2023/08/image.png" alt="" class="wp-image-12022" loading="lazy" decoding="async"></figure>



<h2 class="wp-block-heading" id="Where_do_I_download_the_script">Where do I download the script? <a class="hash" href="#Where_do_I_download_the_script" aria-label="Link to this section"></a></h2>



<p>The script is located over on GitHub: <a href="https://github.com/VirtualgUK/Certificate-Management" target="_blank" rel="noreferrer noopener">VirtualgUK/Certificate-Management (github.com)</a></p>



<p>To download a copy of the script select <strong>&lt;&gt; Code</strong> then <strong>Download Zip</strong></p>



<figure class="wp-block-image size-full"><img src="https://virtualg.uk/wp-content/uploads/2023/08/image-1.png" alt="" class="wp-image-12025" loading="lazy" decoding="async"></figure>



<h2 class="wp-block-heading" id="Feedback">Feedback <a class="hash" href="#Feedback" aria-label="Link to this section"></a></h2>



<p>This script is still under development and as such may not work as required, <em>I cannot accept any liability for this</em>. Please do your own due-diligence.</p>



<p>If you find any bugs, have any feature/integration requests or other feedback, please let me know via <a rel="noreferrer noopener" href="https://twitter.com/VirtualG_UK" data-type="link" data-id="https://twitter.com/VirtualG_UK" target="_blank">Twitter</a> /  E-mail or via <a rel="noreferrer noopener" href="https://github.com/VirtualgUK/Certificate-Management/issues" data-type="link" data-id="https://github.com/VirtualgUK/Certificate-Management/issues" target="_blank">GitHub</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Speed up your PowerShell scripts</title>
      <link>https://virtualg.uk/speed-up-your-powershell-scripts/</link>
      <guid isPermaLink="true">https://virtualg.uk/speed-up-your-powershell-scripts/</guid>
      <pubDate>Sat, 11 Jun 2022 19:37:33 +0000</pubDate>
      <dc:creator><![CDATA[Graham Barker]]></dc:creator>
      <description><![CDATA[PowerShell is a necessary part of your toolkit for anyone working on IT infrastructure. Over time your IT environment increases in size, scripts get more dependent upon and…]]></description>
      <content:encoded><![CDATA[<p>PowerShell is a necessary part of your toolkit for anyone working on IT infrastructure. Over time your IT environment increases in size, scripts get more dependent upon and consequently need to run much faster.</p>



<p>How can I &nbsp;speed up my PowerShell Scripts? This is a common question, fortunately, with some simple changes to your scripts, your PowerShell execution time can be significantly improved.</p>



<h2 class="wp-block-heading" id="Use_Measure-Command">Use Measure-Command <a class="hash" href="#Use_Measure-Command" aria-label="Link to this section"></a></h2>



<p>If you wrap a section of your code (or all of it) with Measure-Command, PowerShell will output the time the code block took to execute. You can also save the response to a variable and access all the timing data like so:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$Timer </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Measure-Command</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> { </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-Process</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$Timer</span></span></code></pre></div>







<p>This will return something similar to:</p>



<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>Days : 0</span></span>
<span class="line"><span>Hours : 0</span></span>
<span class="line"><span>Minutes : 0</span></span>
<span class="line"><span>Seconds : 0</span></span>
<span class="line"><span>Milliseconds : 72</span></span>
<span class="line"><span>Ticks : 724272</span></span>
<span class="line"><span>TotalDays : 8.38277777777778E-07</span></span>
<span class="line"><span>TotalHours : 2.01186666666667E-05</span></span>
<span class="line"><span>TotalMinutes : 0.00120712</span></span>
<span class="line"><span>TotalSeconds : 0.0724272</span></span>
<span class="line"><span>TotalMilliseconds : 72.4272</span></span></code></pre></div>



<p>Or you could simple do:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$Timer.TotalSeconds</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">0.0724272</span></span></code></pre></div>



<p>Don't get confused between $Timer.Seconds and $Timer.TotalSeconds though, the first is just the seconds element from the time. If the total time was 5 minutes and 1 second, then $Timer. Seconds would return 1. You will most likely need to use the TotalXXX options.</p>



<p>If you want to find out which section of your code is running slow, simply wrap those few lines with Measure-Command {} and you can see the execution time of those lines easily.</p>



<h2 class="wp-block-heading" id="Inspect_your_loops">Inspect your loops <a class="hash" href="#Inspect_your_loops" aria-label="Link to this section"></a></h2>



<p>Once we have identified slow sections of code, what can we do about them?</p>



<p>Obviously when you loop through data, every time the loop executes, the code within it will run. If you have commands that take a long time to return, see if it's possible to move them outside of the loop.</p>



<p>For example, if you are running something like the below within every iteration of a loop, it will add significant processing time, even if the loop is only running a few times.</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Connect-VIServer</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Server vcsa01.lab.local </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Credential $MyCredential</span></span></code></pre></div>



<p>In this example, you could connect to the vCenter server before the loop and disconnect after the loop</p>



<p>It may seem an obvious one, but it's always worth inspecting your loops for time-consuming code before anything else. This is because that code is running, usually, a large number of times and causes the most delay to your script.</p>



<h2 class="wp-block-heading" id="Avoid_using_Sleep_Wait_and_Pause">Avoid using Sleep Wait and Pause <a class="hash" href="#Avoid_using_Sleep_Wait_and_Pause" aria-label="Link to this section"></a></h2>



<p>Sometimes there is a reason you need to suspend code execution for exactly a given time period, but more often than not, it is unnecessary.</p>



<p>Take for example a scenario where you start a service and want to wait for it to have started before continuing your code.</p>



<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>Start-Service VSS</span></span></code></pre></div>



<p>In this or other similar examples, you may be tempted to wait a few seconds before continuing in case the code takes a while to finish and the corresponding action to complete.</p>



<p>I realise that Start-Service might be a bad example here but you get the idea!</p>



<p>So a good thing to do, is instead of using something like:</p>



<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>Start-Sleep 30</span></span></code></pre></div>



<p>which will always slow your script down by 30 seconds, it might be better to run a while loop and check if the service is started like below:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Start-Service</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> VSS</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">while</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> ((</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">get-service</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> VSS).Status </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-ne</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "Running"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">){}</span></span></code></pre></div>



<p>This means that the script is not sleeping for any more time than required.</p>



<h2 class="wp-block-heading" id="VMware_PowerCLI_Get-_commands_are_slow">VMware PowerCLI Get-* commands are slow <a class="hash" href="#VMware_PowerCLI_Get-_commands_are_slow" aria-label="Link to this section"></a></h2>



<p>When using 3rd party modules, if you've identified that those are running slow, you may need to deep-dive into why this is happening.</p>



<p>In my experience, the VMware PowerCLI Get-VM &amp; Get-Host commands are actually quite slow to run, especially if your environment is large or you are running the commands many times (Within a loop for example)</p>



<p>When we look specifically at Get-VM, I find that it usually takes an order of magnitude or longer more time to execute than a corresponding Get-View command. This is because Get-VM is really a wrapper for:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-View</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> –Viewtype VirtualMachine</span></span></code></pre></div>



<p>Now I should say here that using the Get-View option is much faster because it's not doing as much as Get-VM is under the hood. Get-VM will gather some information, and make it "look nice" than Get-View but if really need to speed things up, using Get-View is usually the way to go. Get-View can be made even faster by using a filter for only a particular VM or other VM attribute.</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-View</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Viewtype VirtualMachine –Filter </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">@</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">{Name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"VM-001"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span></code></pre></div>



<p>Be aware though that using Get-View can get complex as it's more aligned with the vSphere Web Services API than PowerCLI. For instance, determining the cluster a VM belongs to, requires several Web Services type calls rather than a simpler Get-Cluster command. But if speed is key, this extra work is what's required.</p>



<p>Get-View doesn't return your traditional PowerCLI objects, so some reading may be necessary to use it to its full extent: <a href="https://developer.vmware.com/apis/1192/vsphere" target="_blank" rel="noopener">https://developer.vmware.com/apis/1192/vsphere</a></p>



<p>If you're following along and have just run Get-VM on your environment, you may notice that in later PowerCLI releases, the command returns quite quickly.</p>



<p>But Wait!</p>



<p>Try actually selecting some data from Get-VM:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-VM</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> |</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Select-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> PowerState</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> NumCPU</span></span></code></pre></div>



<p>Not so fast now right?</p>



<p>This is because Get-VM has to run first, and then the Select-Object command needs to run after.<br>Consider the same thing but using Get-View:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-View</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Viewtype VirtualMachine </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">property Name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Summary.Runtime.PowerState</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Config.NumCPU</span></span></code></pre></div>



<p><em>Note: You can find all the properties for the VirtualMachine object under the vSphere Web Services API guide <a href="https://vdc-download.vmware.com/vmwb-repository/dcr-public/bf660c0a-f060-46e8-a94d-4b5e6ffc77ad/208bc706-e281-49b6-a0ce-b402ec19ef82/SDK/vsphere-ws/docs/ReferenceGuide/vim.VirtualMachine.html" target="_blank" rel="noopener">here</a></em></p>



<p>Now run this, wrapped in Measure-Command { #CodeHere } and compare its execution time to your Get-VM code sample: Get-VM | Select-Object Name, PowerState,&nbsp; NumCPU</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$TimerGetView </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Measure-Command</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-View</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Viewtype VirtualMachine </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">property Name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Summary.Runtime.PowerState</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Config.NumCPU}</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$TimerGetVM </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Measure-Command</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-VM</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> |</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Select-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> PowerState</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">,</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> NumCPU}</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$TimerGetView.TotalSeconds</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$TimerGetVM.TotalSeconds</span></span></code></pre></div>



<p>In summary, Get-View will likely be much faster to execute because it has less behind-the-scenes stuff going on than the Get-VM "Wrapper" as I like to call it. Ths same applies to most of the Get- commands including Get-Host and Get-Datastore. You can replace these with the below:</p>



<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-View</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Viewtype Datastore</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">Get-View</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Viewtype HostSystem</span></span></code></pre></div>



<h2 class="wp-block-heading" id="Part_2">Part 2 <a class="hash" href="#Part_2" aria-label="Link to this section"></a></h2>



<p>Subscribe to the <a href="https://virtualg.uk/subscribe" target="_blank" rel="noopener">mailing list here</a> or follow me on <a href="https://twitter.com/virtualg_uk" target="_blank" rel="noopener">Twitter</a> for updates on Part 2 where we will dive into more detail and analyse more complex scenarios.</p>]]></content:encoded>
    </item>
    <item>
      <title>Getting started with the vROps REST API &amp; PowerShell</title>
      <link>https://virtualg.uk/getting-started-with-the-vrops-rest-api/</link>
      <guid isPermaLink="true">https://virtualg.uk/getting-started-with-the-vrops-rest-api/</guid>
      <pubDate>Sun, 19 Dec 2021 02:24:43 +0000</pubDate>
      <dc:creator><![CDATA[Graham Barker]]></dc:creator>
      <description><![CDATA[A few years back I started using the vRealize Operations REST API. At the time I had a number of clients using the solution. I needed a way to automate adding new tenants to the…]]></description>
      <content:encoded><![CDATA[<p>A few years back I started using the vRealize Operations REST API. At the time I had a number of clients using the solution. I needed a way to automate adding new tenants to the solution and ensuring consistent configurations.</p>
<p>Recently I've been working again with the API in my lab and thought to share how you can quickly get started with reading vROps data directly from the REST API within PowerShell:</p>
<h2 id="Authentication">Authentication <a class="hash" href="#Authentication" aria-label="Link to this section"></a></h2>
<p>The most challenging part of working out how to use the API was how the authentication works. Essentially, you need to form the authentication URL, create a JSON formatted credential then obtain a session ID for the subsequent requests.</p>
<p>I have put all of this together in the below sample PowerShell code.</p>
<p><em>You'll probably not want to store credentials in the script, so be sure to find an alternative way to obtain the password (via Read-Host or a call to your password database API</em></p>
<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D">## Global Variables</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$vrops_server </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> ""</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$vrops_username </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> ""</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$vrops_password </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> ""</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D">## Ignore SSL Certificates</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">add-type</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> @"</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    using System.Net;</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    using System.Security.Cryptography.X509Certificates;</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    public class TrustAllCertsPolicy : ICertificatePolicy {</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">        public bool CheckValidationResult(</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">            ServicePoint srvPoint, X509Certificate certificate,</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">            WebRequest request, int certificateProblem) {</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">            return true;</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">        }</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    }</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"@</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">[</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">System.Net.ServicePointManager</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">]::CertificatePolicy </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> New-Object</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> TrustAllCertsPolicy</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D">## vROps API Strings</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_url_base </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "https://"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> +</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $vrops_server </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "/suite-api/api/"</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_url_authenticate </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "https://"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> +</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $vrops_server </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "/suite-api/api/auth/token/acquire"</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D">## Create JSON for Authentication</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$json_authentication </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"{</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">  ""</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">username</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">""</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">""</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$vrops_username</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">""</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">,</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">  ""</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">password</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">""</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">""</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$vrops_password</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">""</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">}"</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D">## Authenticate with vROPs REST API</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">Try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">    $rest_authenticate </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Invoke-RestMethod</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Method POST </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Uri $rest_url_authenticate </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Body $json_authentication </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">ContentType </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"application/json"</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">Catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">    $_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">.Exception.ToString()</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">    $error</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">] </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">|</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Format-List</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Force</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">    exit</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D">## Get Session ID from response</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_authentication_session_xml </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> @</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">{</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"Authorization"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"vRealizeOpsToken "</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_authenticate.</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">'auth-token'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">.token </span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"Accept"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"application/xml"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span></code></pre></div>
<h2 id="Get_Email_Plugins">Get Email Plugins <a class="hash" href="#Get_Email_Plugins" aria-label="Link to this section"></a></h2>
<p>Once you have authenticated to the API, you can use the $rest_authentication_session_xml variable, containing the Session ID for subsequent requests.</p>
<p>The next code snippet will output all configured vROps email plugins</p>
<div>
<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_url_email_plugins </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $rest_url_base</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"alertplugins"</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">Try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; $ResourcesXML </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Invoke-RestMethod</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Method GET </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Uri $rest_url_email_plugins </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Headers $rest_authentication_session_xml</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; $plugins </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $ResourcesXML.</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">'notification-plugins'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">.</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">'notification-plugin'</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">&nbsp; Write-Host</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "Available Email Plugins:"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">ForegroundColor Blue</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; $plugin_id_array</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=@</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">()</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">&nbsp; foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> ($plugin </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $plugins ) {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; &nbsp;</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">&nbsp; &nbsp; if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">($plugin.pluginTypeId </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-ne</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "StandardEmailPlugin"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">){</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">&nbsp; &nbsp; &nbsp; continue</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; &nbsp; }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; &nbsp;</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; &nbsp; $plugin_id_array </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $plugin.pluginId</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">&nbsp; &nbsp; Write-Host</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "Plugin Name:"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $plugin.name </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">" | Plugin ID"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $plugin.pluginId &nbsp;</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">ForegroundColor White</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">&nbsp; }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">Catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">&nbsp; $_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">.Exception.ToString()</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">&nbsp; Write-Host</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "Error getting email plugins. Exiting"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">ForegroundColor Red</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">&nbsp; exit</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span></code></pre></div>
<h2 id="Get_Remote_Collectors">Get Remote Collectors <a class="hash" href="#Get_Remote_Collectors" aria-label="Link to this section"></a></h2>
Next, you can amend the above code snippet to get all configured remote collectors
<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_url_remote_collectors </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $rest_url_base</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"collectors</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">Try {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">  $ResourcesXML</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> = Invoke-RestMethod -Method GET -Uri </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_url_remote_collectors</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> -Headers </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_authentication_session_xml</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">  $collectors</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> = </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$ResourcesXML</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">.'collectors'.'collector'</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">  Write-Host "</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Available Remote Collectors:</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">" -ForegroundColor Blue</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">  $collector_uuid_array</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">=@()</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">  foreach (</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$collector</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> in </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$collectors</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">) {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">    $collector_uuid_array</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> += </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$collector</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">.uuId</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    Write-Host  "</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Remote Collector Name:</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">" </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$collector</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">.name "</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> |</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> Collector ID</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">" </span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$collector</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">.uuId</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">  }</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">}</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">Catch {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">    $_</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">.Exception.ToString()</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    Write-Host "</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Error getting remote collectors. Exiting</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">" -ForegroundColor Red</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">    exit</span></span>
<span class="line"><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">  }</span></span></code></pre></div>
</div>
<h2 id="Get_Notification_Rules">Get Notification Rules <a class="hash" href="#Get_Notification_Rules" aria-label="Link to this section"></a></h2>
<p>As with the other code snippets, you can use the API to obtain all the notification rules:</p>
<div>
<div class="code"><div class="code-bar"><span>PowerShell</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">$rest_url_notification_rules </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $rest_url_base</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">+</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">"notifications/rules"</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">Try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">  $NotificationXML </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> Invoke-RestMethod</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Method GET </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Uri $rest_url_notification_rules </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">Headers $rest_authentication_session_xml</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">  $notification_rules </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $NotificationXML.</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">'notification-rules'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">.</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF">'notification-rule'</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">  Write-Host</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "Available Notification Rules:"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> &nbsp;-</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">ForegroundColor Blue</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">  foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> ($notification_rule </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> $notification_rules) {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">  &nbsp; Write-Host</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> &nbsp;$notification_rule.name</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">  }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">Catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8"> {</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">  &nbsp; $_</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">.Exception.ToString()</span></span>
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF">  &nbsp; Write-Host</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> "Error getting notification rules. Exiting"</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583"> -</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">ForegroundColor Red</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583">  &nbsp; exit</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8">}</span></span></code></pre></div>
</div>
<h2 id="Official_Documentation">Official Documentation <a class="hash" href="#Official_Documentation" aria-label="Link to this section"></a></h2>
<p>Once you have tried the above examples, check out the official documentation by appending /suite-api/doc/swagger-ui.html to the end of your vROps install FQDN.</p>
<p>This will open the vRealize Operations Manager API site and give you all the available API commands which you can substitute in the above code examples.</p>
<figure><img class="alignnone size-full wp-image-3738" src="https://virtualg.uk/wp-content/uploads/2021/12/vROps-API-scaled.jpg" alt="" width="1024" height="470" data-full="/wp-content/uploads/2021/12/vROps-API-scaled.jpg" loading="eager" decoding="async"></figure>]]></content:encoded>
    </item>
    <item>
      <title>How to backup VCSA to Photon OS</title>
      <link>https://virtualg.uk/how-to-backup-vcsa-to-photon-os/</link>
      <guid isPermaLink="true">https://virtualg.uk/how-to-backup-vcsa-to-photon-os/</guid>
      <pubDate>Fri, 20 Dec 2019 21:12:59 +0000</pubDate>
      <dc:creator><![CDATA[Graham Barker]]></dc:creator>
      <description><![CDATA[Backing up your vCenter Server (VCSA) is key to being able to recover in the event of a failure. VMware provides a built-in backup scheduler in version 6.7 & 7.0 of the VCSA which…]]></description>
      <content:encoded><![CDATA[<p>Backing up your vCenter Server (VCSA) is key to being able to recover in the event of a failure. VMware provides a built-in backup scheduler in version 6.7 &amp; 7.0 of the VCSA which is a file-level backup of the VCSA's data. Restoring the VCSA is a simple as deploying a new appliance while specifying the backup option along with the backup file location. (NOTE: This does not backup your Virtual Machines)</p>
<h1>Setting up the Infrastructure</h1>
<p>This article covers how to backup the VCSA to a Linux VM, or in this case, Photon OS. Windows licenses are expensive and so with Photon OS being freely available, it's a good solution to store your VCSA (and other appliance) backups such as switches and routers. IF you already have a secure backup service in place, then feel free to use this instead.</p>
<p>We won't cover the Photon OS deployment here. There is plenty of information on the Photon OS website here: <a href="https://vmware.github.io/photon/assets/files/html/3.0/index.html" target="_blank" rel="noopener noreferrer">https://vmware.github.io/photon/assets/files/html/3.0/index.html</a></p>
<h2 id="Photon_OS_Configuration">Photon OS Configuration <a class="hash" href="#Photon_OS_Configuration" aria-label="Link to this section"></a></h2>
<p>Login to your Photon OS VM</p>
<p><strong>Create a new group</strong></p>
<p>Run the following command to create a new group, I've named mine backup-grp</p>
<p>I've used groups so that you can create new users in the future and choose to add them to the group in order to decide if the user gets access to the backups or not.</p>
<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>groupadd backup-grp</span></span></code></pre></div>
<p><strong>Create a new folder</strong></p>
<p>Now navigate to a suitable folder then create a new folder to store the backups. I'm just creating the backups folder in the root directory here</p>
<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>cd /</span></span>
<span class="line"><span></span></span>
<span class="line"><span>mkdir backups</span></span></code></pre></div>
<p><strong>Create a new user&nbsp;</strong></p>
<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>useradd backup-usr</span></span></code></pre></div>
<p><strong>Set a password for the new backup user</strong></p>
<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>passwd backup-usr</span></span></code></pre></div>
<p>(specify the password when prompted)</p>
<p><strong>Make the new user a member of the new group</strong></p>
<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>usermod -g backup-grp backup-usr</span></span></code></pre></div>
<p><strong>Make the new group (and it's members) the group owner of the backups folder</strong></p>
<div class="code"><div class="code-bar"><span>Text</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span>chown root:backup-grp /backups</span></span></code></pre></div>
<p><strong>Now give the owner (root) and the group (backup-grp) read, write and execute permissions to the folder, but no one else</strong></p>
<div class="code"><div class="code-bar"><span>Bash</span><button type="button" class="copy">Copy</button></div><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e" tabindex="0"><code><span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0">chmod</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF"> 770</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF"> /backups</span></span></code></pre></div>
<h2 id="VCSA_Configuration">VCSA Configuration <a class="hash" href="#VCSA_Configuration" aria-label="Link to this section"></a></h2>
<p>Now that we have everything setup on the Photon OS side, we can configure the backup schedule in the VCSA</p>
<p><strong>First, log in to the VCSA on port 5480</strong></p>
<p>https://vcsa-fqdn-or-ip:5480</p>
<figure><img class="alignnone wp-image-2233 size-full" src="https://virtualg.uk/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0000.png" alt="" width="448" height="373" data-full="/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0000.png" loading="eager" decoding="async"></figure>
<p>Navigate to <strong>Backup</strong> on the left pane</p>
<p>On the top right select <strong>Configure</strong></p>
<figure><img class="alignnone wp-image-2236 size-full" src="https://virtualg.uk/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0002.png" alt="" width="856" height="772" data-full="/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0002.png" loading="lazy" decoding="async"></figure>
<ul>
 	<li>Backup location: Enter the Protocol, FQDN or IP address of the Photon OS VM &amp; the SCP port &amp; the backup directory
<ul>
 	<li>So in my example, we are using SCP<strong>://</strong>&nbsp; then <strong>192.168.100.143</strong> then <strong>:22</strong> and <strong>/backups</strong></li>
 	<li>Put it all together and you get: SCP<strong>://192.168.100.143:22/backups</strong></li>
 	<li>The VCSA is very particular about this format so be sure to double-check it</li>
</ul>
</li><br>
 	<li>User name &amp; Password: The credentials of the backup user we created earlier</li><br>
 	<li>Schedule: When you would like to backup the VCSA</li><br>
 	<li>Encrypt backup: This is optional but can encrypt the backup files in case you need to for compliance purposes</li><br>
 	<li>Number of backups to retain: This is going to be as per your corporate policy, I would advise setting "Retain last x backups "to a number to ensure your Photon OS VM does not run out of space. When set, the VCSA will automatically delete old backups which are outside this retention policy.</li><br>
 	<li>Data: Choose whether or not to backup VCSA stats, events and tasks. I have not done in my lab to speed up the backup process and to save some space.</li><br>
</ul><br>
This window will also show you an estimated size of how large the backups will be (at the current time)
<p>Now select <strong>Create</strong> and as long as you don't have any errors, you are good.</p>
<p>If you need to get access to the files to use them for a restore, then simply use an SCP tool on your machine. If you have a Windows desktop, you can use WinSCP to login and download the backups for recovery or testing.</p>
<h2 id="Testing">Testing <a class="hash" href="#Testing" aria-label="Link to this section"></a></h2>
<p>You can run a test straight away after setting up the schedule by selecting <strong>Backup Now</strong> and checking the option for: <strong>Use backup location and user name from backup schedule</strong></p>
<figure><img class="alignnone wp-image-2238 size-full" src="https://virtualg.uk/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0003.png" alt="" width="836" height="640" data-full="/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0003.png" loading="lazy" decoding="async"></figure>
<p>You can follow the backup progress on the Backup page of the VCSA:</p>
<figure><img class="alignnone wp-image-2237 size-full" src="https://virtualg.uk/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0004.png" alt="" width="1128" height="413" data-full="/wp-content/uploads/2019/12/VCSA-Backup-to-Photon-OS-VirtualG.uk-0004.png" loading="lazy" decoding="async"></figure>

<p>That's it!</p>]]></content:encoded>
    </item>
    <item>
      <title>Setting up and using the Workstation 15 Pro REST API</title>
      <link>https://virtualg.uk/setting-up-and-using-the-workstation-15-pro-rest-api/</link>
      <guid isPermaLink="true">https://virtualg.uk/setting-up-and-using-the-workstation-15-pro-rest-api/</guid>
      <pubDate>Mon, 15 Oct 2018 23:09:00 +0000</pubDate>
      <dc:creator><![CDATA[Graham Barker]]></dc:creator>
      <description><![CDATA[I'm a long-time user of Workstation Pro. My main purpose is for labs as I prefer to host them within software than expensive hardware. Hardware is great but there are too many…]]></description>
      <content:encoded><![CDATA[<p>I'm a long-time user of Workstation Pro. My main purpose is for labs as I prefer to host them within software than expensive hardware.</p>
<p>Hardware is great but there are too many limitations (Initial cost, power, cooling, maintenance, space, patching) and for me, Workstation Pro works perfectly provided that my machine has enough memory.</p>
<p>As I use Workstation&nbsp;all the time, I frequently find myself wanting to automate VM provisioning and management, however, the only way I could do this in the past was by installing my VMs inside an ESXi host which was running inside Workstation. While this does work for me I have to have a few layers of virtualisation and this can cause&nbsp;headaches and unnecessary&nbsp;compute and storage utilization.</p>
<p>The other week I heard that Workstation 15 Pro was being shipped with a REST API, this was something I had to look into further.</p>
<h2 id="Getting_started"><strong>Getting started</strong> <a class="hash" href="#Getting_started" aria-label="Link to this section"></a></h2>
<p>Watch my video or follow the instructions below to get started:</p>
<p>https://www.youtube.com/watch?v=OHLUhaQ786I</p>
<h2 id="Setting_up_the_API"><strong>Setting up the API</strong> <a class="hash" href="#Setting_up_the_API" aria-label="Link to this section"></a></h2>
<p>First, you need to be running Workstation 15 Pro. I'm running&nbsp;it on Windows but the API also works in Linux.</p>
<ul>
 	<li>Once you have installed or upgraded Workstation Pro, open a command prompt</li>
 	<li>CD to your installation folder:
<code>cd "C:\Program Files (x86)\VMware\VMware Workstation"</code></li>
 	<li>Run the vmrest configuration
<code>vmrest.exe -C</code></li>
 	<li>Specify a new username and password</li>
 	<li>Once complete, you can open&nbsp;<em>%USERPROFILE%\vmrest.</em>cfg to view the configuration that you just created</li>
 	<li>Now run vmrest to launch the API service
<code>vmrest</code></li>
</ul>
<h2 id="Using_the_API"><strong>Using the API</strong> <a class="hash" href="#Using_the_API" aria-label="Link to this section"></a></h2>
<ul>
 	<li>Launch the API Browser in a web browser <code>http://127.0.0.1:8697</code></li>
 	<li>Authenticate to the system with the "Authorization" button</li>
 	<li>Enter the previously created credentials</li>
 	<li>Now you can browse the API Explorer</li>
 	<li>Locate an API call that you are interested in. For example, expand the GET /vms&nbsp;API call</li>
 	<li>Scroll down and select "Try It Out"</li>
 	<li>This will run the call against your live Workstation instance and return the result which will be a list of all your VMs running in Workstation Pro with their IDs and VMX paths</li>
 	<li>Copy the Curl command and paste it into Command Prompt to get a feel for the API outside of the API explorer (In Windows you may need to replace the single quotes with double quotes
<code>curl "http://127.0.0.1:8697/api/vms" -X GET --header "Accept: application/vnd.vmware.vmw.rest-v1+json" --header "Authorization: Basic cmVzdF91c2VyOlZNd2FyZTEh"</code></li>
</ul>
<p>And that's it. You can use the API calls from a scripting or programming language of your choice.</p>]]></content:encoded>
    </item>
  </channel>
</rss>
