<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scripts Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/scripts/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/scripts/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Mon, 09 Dec 2019 19:17:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>scripts Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/scripts/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Cloud-init &#8211; Part 4 &#8211; Running Scripts</title>
		<link>https://www.lucd.info/2019/12/09/cloud-init-part-4-running-scripts/</link>
					<comments>https://www.lucd.info/2019/12/09/cloud-init-part-4-running-scripts/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Mon, 09 Dec 2019 14:08:44 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Photon]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[cloud-init]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[VMware PowerCLI]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=6697</guid>

					<description><![CDATA[For now, the second to last part in this series. And although technically [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>For now, the second to last part in this series. And although technically not a &#8216;<em>real</em>&#8216; <a href="https://cloudinit.readthedocs.io/en/latest/" target="_blank" rel="noopener noreferrer">cloud-init</a> post, I consider it related to <a href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank" rel="noopener noreferrer">Part 1</a>, <a href="https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/" target="_blank" rel="noopener noreferrer">Part 2</a> and <a href="https://www.lucd.info/2019/12/08/cloud-init-part-3-photon-os/" target="_blank" rel="noopener noreferrer">Part3</a> in this series. In this post I&#8217;ll show how you can run <strong>scripts</strong> on these &#8216;<em>cattle</em>&#8216; stations we just deployed.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="721" height="427" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-4-2.png" alt="" class="wp-image-6907" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-4-2.png 721w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-4-2-300x178.png 300w" sizes="(max-width: 721px) 100vw, 721px" /></figure>



<span id="more-6697"></span>



<h2 class="wp-block-heading">Introduction</h2>



<p>The purpose of this post is quite straightforward. How can we use the &#8216;<em>cattle</em>&#8216; stations we created to run our PowerShell/PowerCLI scripts?</p>



<p>Since all the stations we deployed with cloud-init come with the <a rel="noreferrer noopener" aria-label="VMware Tools (opens in a new tab)" href="https://docs.vmware.com/en/VMware-Tools/index.html" target="_blank">VMware Tools</a> installed, we can use the <a rel="noreferrer noopener" aria-label="GuestOperations (opens in a new tab)" href="https://vdc-download.vmware.com/vmwb-repository/dcr-public/790263bc-bd30-48f1-af12-ed36055d718b/e5f17bfc-ecba-40bf-a04f-376bbb11e811/vim.vm.guest.GuestOperationsManager.html" target="_blank">GuestOperations</a>, and all cmdlets and functions based on these <strong>API</strong>, to communicate with the Guest OS.</p>



<p>In practice, this means that the <a rel="noreferrer noopener" aria-label="Invoke-VMScript (opens in a new tab)" href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6fb85470-f6ca-4341-858d-12ffd94d975e/4bee17f3-579b-474e-b51c-898e38cc0abb/doc/Invoke-VMScript.html" target="_blank">Invoke-VMScript</a> cmdlet should be used. But unfortunately, the current implementation of that cmdlet has some issues with multi-line bash scripts (and a few other problems).</p>



<p>To avoid those issues I have written my <a rel="noreferrer noopener" aria-label="Invoke-VMScriptPlus (opens in a new tab)" href="https://www.lucd.info/2019/11/17/invoke-vmscriptplus-v3/" target="_blank">Invoke-VMScriptPlus</a> function. Besides fixing the above-mentioned issues, it also adds some extra features that come in handy, as you will notice later on in this post.</p>



<h2 class="wp-block-heading">Local PowerShell Script</h2>



<h3 class="wp-block-heading">Ubuntu</h3>



<p>We start simple. In this post, we will show how you can run your PowerShell scripts locally inside the guest OS of the stations we deployed with cloud-init.</p>



<p>Note that in the examples I use a <a rel="noreferrer noopener" aria-label="VICredentialStoreItem (opens in a new tab)" href="https://vdc-repo.vmware.com/vmwb-repository/dcr-public/6fb85470-f6ca-4341-858d-12ffd94d975e/4bee17f3-579b-474e-b51c-898e38cc0abb/doc/VICredentialStoreItem.html" target="_blank">VICredentialStoreItem</a> to fetch the credentials for the target guest OS. As you might know by now, the VICredentialStoreItem cmdlets are only supported on a <strong>Windows platform</strong> (since they use the <a href="https://en.wikipedia.org/wiki/Data_Protection_API" target="_blank" rel="noreferrer noopener" aria-label="DPAPI (opens in a new tab)">DPAPI</a> methods).</p>



<p>Any which way you get those credentials will of course work. But I consider that not to be part of the cloud-init series of posts.</p>



<p>Back to the task at hand, let&#8217;s use a simple example to start.</p>



<pre class="lang:ps decode:true ">$vmName = 'ubuntubionicps'
$code = @'
$PSVersionTable
'@

$credItem = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credItem.User, (ConvertTo-SecureString $credItem.Password -AsPlainText -Force))

$sInvoke = @{
    VM = $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = $code
}
Invoke-VMScriptPlus @sInvoke
</pre>



<p>Nothing too complicated at first sight. We sent a PowerShell script to an Ubuntu box. But we do seem to get an error.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="111" src="https://www.lucd.info/wp-content/uploads/2019/12/error1-1024x111.png" alt="" class="wp-image-6858" srcset="https://www.lucd.info/wp-content/uploads/2019/12/error1-1024x111.png 1024w, https://www.lucd.info/wp-content/uploads/2019/12/error1-300x33.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/error1-768x83.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/error1-720x78.png 720w, https://www.lucd.info/wp-content/uploads/2019/12/error1.png 1215w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>That doesn&#8217;t really give us a lot of useful information on what is going on. My first step in analysing such non-obvious errors is to add some <strong>verbosity</strong>.</p>



<pre class="lang:ps decode:true ">$vmName = 'ubuntubionicps'
$code = @'
$PSVersionTable
'@

$credItem = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credItem.User, (ConvertTo-SecureString $credItem.Password -AsPlainText -Force))

$sInvoke = @{
    VM = $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = $code
    Verbose = $true
}
Invoke-VMScriptPlus @sInvoke
</pre>



<p>And now we get some more clues. Btw, I only listed part of the verbose output that helps in analysing the issue.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="220" src="https://www.lucd.info/wp-content/uploads/2019/12/error2-1024x220.png" alt="" class="wp-image-6863" srcset="https://www.lucd.info/wp-content/uploads/2019/12/error2-1024x220.png 1024w, https://www.lucd.info/wp-content/uploads/2019/12/error2-300x64.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/error2-768x165.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/error2-720x154.png 720w, https://www.lucd.info/wp-content/uploads/2019/12/error2.png 1203w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>The error comes immediately after the PUT where we copy the script text to a file in the guest OS. Since SSL/TLS secure channel is mentioned, let&#8217;s have a look at the certificate that is used on that ESXi node.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="406" height="550" src="https://www.lucd.info/wp-content/uploads/2019/12/cert.png" alt="" class="wp-image-6866" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cert.png 406w, https://www.lucd.info/wp-content/uploads/2019/12/cert-221x300.png 221w" sizes="auto, (max-width: 406px) 100vw, 406px" /></figure>



<p>And there is our answer. The certificate used on the ESXi node in question only contains a SAN for the FQDN, not the IP address.</p>



<p>Since this is something I watched happen at several sites, I added the <strong>NoIPinCert</strong> switch to the Invoke-VMScriptPlus function. When that switch is set, the function will take the IP address and replace it by the FQDN. This, of course, assumes that DNS resolution is working!</p>



<pre class="lang:ps decode:true ">$vmName = 'ubuntubionicps'
$code = @'
$PSVersionTable
'@

$credItem = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credItem.User, (ConvertTo-SecureString $credItem.Password -AsPlainText -Force))

$sInvoke = @{
    VM = $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = $code
    NoIPinCert = $true
}
Invoke-VMScriptPlus @sInvoke
</pre>



<p>And now we get the result we were expecting.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="910" height="553" src="https://www.lucd.info/wp-content/uploads/2019/12/out1.png" alt="" class="wp-image-6867" srcset="https://www.lucd.info/wp-content/uploads/2019/12/out1.png 910w, https://www.lucd.info/wp-content/uploads/2019/12/out1-300x182.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/out1-768x467.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/out1-720x438.png 720w" sizes="auto, (max-width: 910px) 100vw, 910px" /></figure>



<p>Don&#8217;t mind the properties beside ScriptOutput and the funny characters in ScriptOutput for now. We&#8217;ll get to that later.</p>



<h3 class="wp-block-heading">Photon</h3>



<p>And for good measure, let&#8217;s run the same on a <strong>Photon</strong> station.</p>



<pre class="lang:ps decode:true  ">$vmName = 'photonps'
$code = @'
$PSVersionTable
'@

$credItem = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credItem.User, (ConvertTo-SecureString $credItem.Password -AsPlainText -Force))

$sInvoke = @{
    VM = $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = $code
    NoIPinCert = $true
}
Invoke-VMScriptPlus @sInvoke
</pre>



<p>Confirmed, also a success with a Photon guest OS.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="928" height="566" src="https://www.lucd.info/wp-content/uploads/2019/12/out2.png" alt="" class="wp-image-6876" srcset="https://www.lucd.info/wp-content/uploads/2019/12/out2.png 928w, https://www.lucd.info/wp-content/uploads/2019/12/out2-300x183.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/out2-768x468.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/out2-720x439.png 720w" sizes="auto, (max-width: 928px) 100vw, 928px" /></figure>



<h2 class="wp-block-heading">Local PowerCLI Script</h2>



<p>So we can run PowerShell script, but what about <a href="https://www.vmware.com/support/developer/PowerCLI/" target="_blank" rel="noreferrer noopener" aria-label="VMware PowerCLI (opens in a new tab)">VMware PowerCLI</a>? I want to be able to run my PowerCLI script on such a station.</p>



<p>Since we need to have the modules installed, before we can actually use any of the PowerCLI cmdlets, let&#8217;s include that in our test script.</p>



<pre class="lang:ps decode:true  ">$vmName = 'ubuntubionicps'

$code = @'
<code>$ProgressPreference = "SilentlyContinue"
if(-not (Get-Module -Name 'VMware.PowerCLI' -ListAvailable)){
    Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber -Force | Out-Null
}
</code>$WarningPreference = 'SilentlyContinue'
<code>$sConf = @{
    InvalidCertificateAction = 'Ignore'
    ParticipateInCeip = </code>$false
    DisplayDeprecationWarnings = <code>$false
    Scope = 'User'
    Confirm = </code>$false
}
Set-PowerCLIConfiguration @sConf | Out-Null
Connect-VIServer -Server $vcsaName -User $user -Password $pswd | Out-Null
Get-VMHost | Select Name,Version,PowerState,NumCpu,MemoryTotalGB | ConvertTo-Csv -NotypeInformation -UseCulture
Disconnect-VIServer -Server $vcsaName -Confirm:<code>$false
'@

$vcsaName = 'vcsa.local.lab'
$credVCSA = Get-VICredentialStoreItem -Host $vcsaName
$user = $credVCSA.User
$pswd = $credVCSA.Password

$credVM = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credVM.User, (ConvertTo-SecureString $credVM.Password -AsPlainText -Force))

$sInvoke = @{
    VM = Get-VM -Name $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)
    NoIPinCert = $true
}
$result = Invoke-VMScriptPlus @sInvoke
$result.ScriptOutput | ConvertFrom-Csv -UseCulture | Format-Table -AutoSize
</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 3-20</strong>: The script we are executing inside the target guest OS.</p>



<p><strong>Line 4</strong>: Since we are using variable substitution on the script text later on (see <strong>line 34</strong>), we need to escape the dollar signs in the variables we do NOT want to be substituted. This is done through a backtick before the dollar sign.</p>



<p><strong>Line 4</strong>: We mute the progress information. This to avoid unnecessary output during the installation of the PowerCLI modules.</p>



<p><strong>Line 5-7</strong>: A simple test to check if the PowerCLI modules are installed or not. If they aren't, the script will install them. Note that since we are running our scripts locally in the guest OS, once the modules are installed they will stay installed for any future script runs. If you want to start fresh for each script, you will have to deploy a new instance. The 'cattle' principle!</p>



<p><strong>Line 9-16</strong>: We want our script to be as 'silent' as possible, that is why we change some PowerCLI configuration settings. Note that disabling the <strong>CEIP</strong> participation in this example script is a personal choice and you can leave it on without any impact on your script(s).</p>



<p><strong>Line 18</strong>: This sample code will output results in CSV format (<a rel="noreferrer noopener" aria-label="ConvertTo-Csv (opens in a new tab)" href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-csv?view=powershell-6" target="_blank">ConvertTo-Csv</a>) to the console. This will make it easy for the calling script to handle the returned data.</p>



<p><strong>Line 34</strong>: Instead of hard-coding specific values in the script we send to the target station, we use variable substitution to fill in the values at the moment we send the script to the target station. This is done through the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.commandinvocationintrinsics.expandstring?view=pscore-6.2.0" target="_blank" rel="noreferrer noopener" aria-label="ExpandString (opens in a new tab)">ExpandString</a> method.</p>



<p><strong>Line 38</strong>: This show how easy it is to receive the result when we have the data converted into a well-known format (CSV in this case).</p>



<p>The script results in the following. Instead of <strong>Format-Table</strong> we could have used any other cmdlet that sends the result to a file, or as the body of an email...</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="671" height="154" src="https://www.lucd.info/wp-content/uploads/2019/12/out3.png" alt="" class="wp-image-6890" srcset="https://www.lucd.info/wp-content/uploads/2019/12/out3.png 671w, https://www.lucd.info/wp-content/uploads/2019/12/out3-300x69.png 300w" sizes="auto, (max-width: 671px) 100vw, 671px" /></figure>



<p>The same script runs in the same way on Ubuntu and Photon. And in both cases produces the same result.</p>



<h2 class="wp-block-heading">Your Existing Scripts</h2>



<p>A question I heard several times when I presented the concept of 'cattle' stations at conferences, was "<em>What will happen with my existing scripts?</em>".</p>



<p>As it turns out, you will not have to do a lot. In most cases, you will able to just take your script and with a bit of PowerShell magic, you can use it in this setup.</p>



<p>Let's take this simple script as an example. And yes, this is exactly the same code as ran before. But we're demonstrating the concept.</p>



<p>We save the following lines as <strong>Sample-PCLI-Script.ps1</strong>, and it is representative for all your existing scripts.</p>



<pre class="lang:ps decode:true ">Get-VMHost | Select Name, Version, PowerState, NumCpu, MemoryTotalGB |<br>ConvertTo-Csv -NotypeInformation -UseCulture
</pre>



<p>We now need to use a slightly different script to submit this to our target station.</p>



<pre class="lang:ps decode:true ">$vmName = 'ubuntubionicps'

$prefix = @'
</code>$ProgressPreference = "SilentlyContinue"
if(-not (Get-Module -Name 'VMware.PowerCLI' -ListAvailable)){
    Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber -Force | Out-Null
}
<code>$WarningPreference = 'SilentlyContinue'
</code>$sConf = @{
    InvalidCertificateAction = 'Ignore'
    ParticipateInCeip = <code>$false
    DisplayDeprecationWarnings = </code>$false
    Scope = 'User'
    Confirm = <code>$false
}
Set-PowerCLIConfiguration @sConf | Out-Null
Connect-VIServer -Server $vcsaName -User $user -Password $pswd | Out-Null
'@

$code = Get-Content -Path .\Sample-PCLI-Script.ps1

$suffix = @'
Disconnect-VIServer -Server $vcsaName -Confirm:</code>$false
'@

$vcsaName = 'vcsa.local.lab'
$credVCSA = Get-VICredentialStoreItem -Host $vcsaName
$user = $credVCSA.User
$pswd = $credVCSA.Password

$credVM = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credVM.User, (ConvertTo-SecureString $credVM.Password -AsPlainText -Force))

$sInvoke = @{
    VM = Get-VM -Name $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = ($ExecutionContext.InvokeCommand.ExpandString($prefix) + "<code>n$code</code>n" + $ExecutionContext.InvokeCommand.ExpandString($suffix))
    NoIPinCert = $true
}
$result = Invoke-VMScriptPlus @sInvoke
$result.ScriptOutput
$result.ScriptOutput | ConvertFrom-Csv -UseCulture | Format-Table -AutoSize
</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 3-18</strong>: Under the $prefix variable we store the 'fixed' code that prepares the environment. Just as the same part in the previous example.</p>



<p><strong>Line 20</strong>: This is where we read our existing script into a variable.</p>



<p><strong>Line 22-24</strong>: The $suffix variable contains the 'fixed' closing part of the code.</p>



<p><strong>Line 38</strong>: We compose the ScriptText from the three variables $preifx, $code and $suffix. Note that for the $code variable we don't need to do any variable substitution.</p>



<p>Will this work for all your existing scripts?</p>



<p>Probably not, you will need to verify if there are for example any Connect- and/or DisConnect-VIServer lines in your script.</p>



<h2 class="wp-block-heading">Infile and Outfile </h2>



<p>In the previous examples, we always send the result back over the standard output. This is not a feasible solution in all cases. What for example when your script produces multiples outputs? And what when you need to read data as input for your script?</p>



<p>For all those reasons I added the <strong>InFile</strong> and <strong>OutFile</strong> parameters to Invoke-VMScriptPlus. For more details see my <a rel="noreferrer noopener" aria-label="Invoke-VMScriptPLus V3 (opens in a new tab)" href="https://www.lucd.info/2019/11/17/invoke-vmscriptplus-v3/" target="_blank">Invoke-VMScriptPLus V3</a> post.</p>



<p>The following demonstrates, in a simple way, how you can use those InFile and OutFile parameters to run your scripts on a 'cattle' station.</p>



<p>This is our original script, saved as <strong>Sample-PCLI-Script4.ps1</strong>.</p>



<pre class="lang:ps decode:true">$esxName = Import-Csv -Path .\esxnames.csv | Select -ExpandProperty Name
Get-VMHost -Name $esxName | Select Name, NumCpu, Version |
Export-Csv -Path .\report.csv -UseCulture -NoTypeInformation

Write-Host "Script completed"
</pre>



<p>The <strong>esxnames.csv</strong> file contains the following.</p>



<table style="border-collapse: collapse; width: 100%;">
<tbody>
<tr>
<td style="width: 100%;">
<div>Name</div>
</td>
</tr>
<tr>
<td style="width: 100%;">esx1.local.lab</td>
</tr>
<tr>
<td style="width: 100%;">esx3.local.lab</td>
</tr>
</tbody>
</table>



<p>Our calling script. Notice the use of the <strong>InFile</strong> and <strong>OutFile</strong> parameters on <strong>lines 40 and 41</strong>.</p>



<pre class="lang:ps decode:true ">$vmName = 'ubuntubionicps'

$prefix = @'
<code>$ProgressPreference = "SilentlyContinue"
if(-not (Get-Module -Name 'VMware.PowerCLI' -ListAvailable)){
    Install-Module -Name VMware.PowerCLI -Scope CurrentUser -AllowClobber -Force | Out-Null
}
</code>$WarningPreference = 'SilentlyContinue'
<code>$sConf = @{
    InvalidCertificateAction = 'Ignore'
    ParticipateInCeip = </code>$false
    DisplayDeprecationWarnings = <code>$false
    Scope = 'User'
    Confirm = </code>$false
}
Set-PowerCLIConfiguration @sConf | Out-Null
Connect-VIServer -Server $vcsaName -User $user -Password $pswd | Out-Null
'@

$code = Get-Content -Path .\Sample-PCLI-Script4.ps1

$suffix = @'
Disconnect-VIServer -Server $vcsaName -Confirm:<code>$false
'@

$vcsaName = 'vcsa.local.lab'
$credVCSA = Get-VICredentialStoreItem -Host $vcsaName
$user = $credVCSA.User
$pswd = $credVCSA.Password

$credVM = Get-VICredentialStoreItem -Host $vmName
$cred = New-Object System.Management.Automation.PSCredential ($credVM.User, (ConvertTo-SecureString $credVM.Password -AsPlainText -Force))

$sInvoke = @{
    VM = Get-VM -Name $vmName
    GuestCredential = $cred
    ScriptType = 'powershellv6'
    ScriptText = ($ExecutionContext.InvokeCommand.ExpandString($prefix) + "</code>n$($code -join "<code>n")</code>n" + $ExecutionContext.InvokeCommand.ExpandString($suffix))
    GuestOSType = 'Linux'
    InFile = '.\esxnames.csv'
    OutFile = 'report.csv'
    NoIPinCert = $true
}
$result = Invoke-VMScriptPlus @sInvoke
$result.ScriptOutput

</pre>



<p>The output from our script is now just a message we wrote in the original script.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="513" height="58" src="https://www.lucd.info/wp-content/uploads/2019/12/out4.png" alt="" class="wp-image-6906" srcset="https://www.lucd.info/wp-content/uploads/2019/12/out4.png 513w, https://www.lucd.info/wp-content/uploads/2019/12/out4-300x34.png 300w" sizes="auto, (max-width: 513px) 100vw, 513px" /></figure>



<p>But the result can now be found in the file <strong>report.csv</strong>.</p>



<table style="border-collapse: collapse; width: 100%;">
<tbody>
<tr>
<td style="width: 25%;">
<div>
<div>Name</div>
</div>
</td>
<td style="width: 25%;">NumCpu</td>
<td style="width: 25%;">Version</td>
<td style="width: 25%;">&nbsp;</td>
</tr>
<tr>
<td style="width: 25%;">esx1.local.lab</td>
<td style="width: 25%;">4</td>
<td style="width: 25%;">6.7.0</td>
<td style="width: 25%;">&nbsp;</td>
</tr>
<tr>
<td style="width: 25%;">esx3.local.lab</td>
<td style="width: 25%;">4</td>
<td style="width: 25%;">6.7.0</td>
<td style="width: 25%;">&nbsp;</td>
</tr>
</tbody>
</table>



<h2 class="wp-block-heading">Conclusion</h2>



<p>I hope the previous sections have shown that it doesn't require rocket science to run your scripts on instances deployed with the help of cloud-init.</p>



<p>Will the above methods always work for your scripts?</p>



<p>Probably not, but it shouldn't be too difficult to make them work.</p>



<p>I'm always interested to hear about cases that at first can't be run on 'cattle' stations. If you think you have such a case, let me know through the comments.</p>



<p>Enjoy!</p>


]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2019/12/09/cloud-init-part-4-running-scripts/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
