tag:blogger.com,1999:blog-88147360655609448492024-02-20T02:03:04.294+01:00Musings of a PowerShell Mad ManPowerShell, VMware, and anything else I find interesting or keep forgettingAkos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.comBlogger71125tag:blogger.com,1999:blog-8814736065560944849.post-77575777433537526512017-02-18T16:55:00.001+01:002017-02-18T16:55:54.402+01:00Changing Nano back to DHCPI've been playing around some more with Nano server, but I didn't want to create yet another VM, so I decided to use one of my existing Nano servers. But it had an IP set already, and was set to an Internal switch (so no internet connectivity possible). So let's PowerShell-fix this!<br />
<br />
First let's change the adapter of my VM called nano1:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-VM</span> <span style="color: blueviolet;">nano1</span> <span style="color: darkgrey;">|</span><span style="color: blue;">Get-VMNetworkAdapter</span><span style="color: darkgrey;">|</span><span style="color: blue;">Connect-VMNetworkAdapter</span> <span style="color: navy;">-SwitchName</span> <span style="color: blueviolet;">external </span></div>
</div>
<br />
There, that didn't hurt so much! Good, now let's change the settings inside the nano1 VM, to set the IP's back to DHCP. First, this very cool thing in Hyper-V called PowerShell Direct, allows me to enter the VM without needing any IP connectivity. All you need is the credentials of the VM, and to select the "-VMName" option with Enter-PSSession:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$cred</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Credential</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Enter-PSSession</span> <span style="color: navy;">-VMName</span> <span style="color: blueviolet;">nano1</span> <span style="color: navy;">-Credential</span> <span style="color: orangered;">$cred </span></div>
</div>
<br />
This is what it looks like:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: whitesmoke;">PS C:\> $cred = Get-Credential</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Enter-PSSession -VMName nano1 -Credential $cred</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">cmdlet Get-Credential at command pipeline position 1</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Supply values for the following parameters:</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">[nano1]: PS C:\Users\administrator\Documents> </span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;"> </span></div>
</div>
<br />
As Montgomery Burns says: "Excellent...": Did you notice the [nano1] in front of the last prompt? We're inside the VM! BTW, you don't need to add the credential, it will ask you for the credentials if you don't add them, but if you go in and out more than once, this is nicer.<br />
<br />
Now that we're in a session, let's change the IP address and DNS back to DHCP enabled:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-NetIPAddress</span> <span style="color: navy;">-InterfaceAlias</span> <span style="color: darkred;">"Ethernet"</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Set-NetIPInterface</span> <span style="color: navy;">-Dhcp</span> <span style="color: blueviolet;">Enabled</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Set-DnsClientServerAddress</span> <span style="color: navy;">-ResetServerAddresses</span> <span style="color: navy;">-InterfaceAlias</span> <span style="color: blueviolet;">Ethernet</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
Done!Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-10418578505694766702017-02-12T12:03:00.002+01:002018-05-08T12:41:49.018+02:00Linked clones in Hyper-V<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJajBvtjc95Vy2BPW1dhWca0PNX9S4qfUjkihudJ5idi38TDrvkAv_mbtiDm_wZxsjzeTcUBDtprSGwLKlogU_s8KAuobbHWtsSLhccG8hU5_Nzhk2cI-Ax0BxksFqO3LvsyFoHewI2kHo/s1600/darkside_newthumb.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJajBvtjc95Vy2BPW1dhWca0PNX9S4qfUjkihudJ5idi38TDrvkAv_mbtiDm_wZxsjzeTcUBDtprSGwLKlogU_s8KAuobbHWtsSLhccG8hU5_Nzhk2cI-Ax0BxksFqO3LvsyFoHewI2kHo/s200/darkside_newthumb.png" width="200" /></a><br />
<div style="text-align: left;">
I've allways been a fan of VMware Workstation, but I've been spending a bit of time with Hyper-V on Windows 10 now too to see how the cookies are on the other side. One thing I thought I missed was creating what is called linked clones in VMware Workstation.</div>
<br />
<br />
<br />
Linked clones allow a user to create multiple VM's that are referenced off of only one VM. The changes compared to the first VM are stored in a so called delta file under the covers. This way, you can build a large lab while needing a lot less space. You will however need access to the original VM files.<br />
<br />
<br />
It turns out Hyper-V has the same functionality, but the steps to create a linked clone are a bit more hidden, and the operations are a bit more manual. The steps are as follows:<br />
<ul>
<li>You need a template VM to reference your linked clones from. No different from VMware Workstation. The difference though, is that with VMware Workstation, you can keep working with this VM. With Hyper-V, this template is going to be left as an image on your harddrive, but you can't use it.</li>
<li>Sysprep the template VM. Theoretically you should do that in VMware Workstation too, but if you have some quick test you want to do and don't require domain functionality this could be omitted.</li>
<li>Turn off the VM, and mark the vhdx file of the VM as read-only. You can even remove that VM out of Hyper-V, so you don't accidentally turn it back on again</li>
<li>In Hyper-V, go to Action->New->Hard Disk. Select "Differencing Disk" as harddisk type, set the destination location and select the vhdx from the VM you created earlier.</li>
<li>Finally, create a new VM and select the newly created differenced disk instead of a new harddisk.</li>
</ul>
<div>
A lot of steps for what is relatively simple wizard in VMware Workstation. I think we can do better than that ;-) Powershell to the rescue! In the case below I've been lazy, and created a function that has paths and memory and such hardcoded inside, but the idea is what counts:<br />
<br />
<br />
<ul>
<li>You create a differencing disk with New-VHD</li>
<li>You create the VM</li>
<li>You add the harddisk</li>
</ul>
</div>
<div>
<br /></div>
<div style="font-family: "segoe ui"; font-size: 12px; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$Template</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"C:\VM\2012 Template.vhdx"</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkred;"><br /></span></div>
<div style="color: black;">
<br /></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">New-CloneVM</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
{</div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkgrey;">[</span><span style="color: deepskyblue;">CmdletBinding</span>()<span style="color: darkgrey;">]</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Alias</span>()<span style="color: darkgrey;">]</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkblue;">Param</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
(</div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Parameter</span>(Mandatory<span style="color: darkgrey;">=</span><span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
ValueFromPipelineByPropertyName<span style="color: darkgrey;">=</span><span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
Position<span style="color: darkgrey;">=</span><span style="color: purple;">0</span>)<span style="color: darkgrey;">]</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: orangered;">$Template</span><span style="color: darkgrey;">,</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: darkgrey;">[</span><span style="color: deepskyblue;">Parameter</span>(Mandatory<span style="color: darkgrey;">=</span><span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
ValueFromPipelineByPropertyName<span style="color: darkgrey;">=</span><span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0px;">
Position<span style="color: darkgrey;">=</span><span style="color: purple;">1</span>)<span style="color: darkgrey;">]</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: orangered;">$NewVM</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
)</div>
<div style="color: black;">
<br /></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: orangered;">$destdisk</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">“C:\VM\</span><span style="color: orangered;">$NewVM</span><span style="color: darkred;">.vhdx”</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
</div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: blue;">New-VHD</span> <span style="color: navy;">–ParentPath</span> <span style="color: orangered;">$Template</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$destdisk</span> <span style="color: navy;">–Differencing</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: blue;">New-VM</span> <span style="color: orangered;">$NewVM</span> <span style="color: navy;">-MemoryStartupBytes</span> <span style="color: purple;">2048MB</span> <span style="color: navy;">-SwitchName</span> <span style="color: blueviolet;">VLAN2</span> <span style="color: navy;">-Generation</span> <span style="color: purple;">2</span> <span style="color: navy;">-BootDevice</span> <span style="color: blueviolet;">CD</span></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: blue;">Add-VMHardDiskDrive</span> <span style="color: navy;">-VMName</span> <span style="color: orangered;">$NewVM</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$destdisk</span> <span style="color: navy;">-ControllerType</span> <span style="color: blueviolet;">SCSI</span> </div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
}</div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<br /></div>
<div style="color: black;">
<br /></div>
<div style="background-color: white; color: black; font-family: "lucida console"; margin: 0px;">
<span style="color: blue;">New-CloneVM</span> <span style="color: navy;">-template</span> <span style="color: orangered;">$Template</span> <span style="color: navy;">-NewVM</span> <span style="color: blueviolet;">Test1 </span></div>
<div style="color: black;">
<br /></div>
<div style="color: black;">
<br /></div>
</div>
<br />
If you quickly want to create a bunch of VM's for your testlab, Powershell and Hyper-V are a good alternative to VMware workstation, and in fact, since VMware Workstation doesn't support Powershell, Hyper-V might even be better.. I know, I know. blasphemy.<br />
<div>
<br /></div>
Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-8312436307040829652017-02-08T23:00:00.001+01:002017-02-08T23:00:14.105+01:00Turning on nested virtualization Hyper-VI've been playing around with Hyper-V these past few weeks, and it seems to work rather well (VMware needs to start worrying).<br />
<br />
One of the features I've been playing with is nested virtualization. Unlike VMware Workstation where you can select Hyper-V as one of the install options, in Hyper-V on Windows 10 and Windows 2016, you can turn it on on a per VM basis, with the following command:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Set-VMProcessor</span> <span style="color: navy;">-VMName</span> <span style="color: blueviolet;">NestHostVM</span> <span style="color: navy;">-ExposeVirtualizationExtensions</span> <span style="color: orangered;">$true </span></div>
</div>
<br />
Now you can have a Hyper-V inside your Hyper-V. I'm so hyped about this! <drumroll><br />
<br />
To get a list of which VM's have it turned on, do:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-VMProcessor</span> <span style="color: navy;">-VMName</span> <span style="color: blueviolet;">*</span> <span style="color: darkgrey;">|</span><span style="color: blue;">select</span> <span style="color: blueviolet;">VMName</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">ExposeVirtualizationExtensions </span></div>
</div>
Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-77519991511475234382017-01-29T22:30:00.000+01:002017-01-29T22:30:36.127+01:00F5 management through PowerShellI've mentioned in my <a href="http://dont-trust-the-admin.blogspot.nl/2017/01/automatically-upgrade-vcpus-part-1.html" target="_blank">vCPU upgrade article</a> that one of the requirements of me being allowed to shut down the VM was to be sure that there were no connections on the F5 loadbalancer for the webserver.<br />
<br />
So I needed to script against the F5 loadbalancer. The first thing you need to do, is to get the <a href="https://devcentral.f5.com/d/microsoft-powershell-with-icontrol" target="_blank">F5 PowerShell Snapin</a> from the F5 site. An account can be made for free, then you can login and download the plugin and follow the instructions to get it installed.<br />
<br />
Once installed, you can add the snapin with:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Add-PSSnapIn</span> <span style="color: blueviolet;">iControlSnapIn </span></div>
</div>
<br />
Now you have commands to manage the F5. Great, now it's time to login. First you need to add some credentials to a variable, and I've mentioned this in a <a href="http://dont-trust-the-admin.blogspot.nl/2017/01/store-credentials-securely-for-later-use.html" target="_blank">previous article</a> too. After that, you can set up the connection to the loadbalancer:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$creds</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Import-CliXml</span> <span style="color: navy;">-Path</span> <span style="color: darkred;">"D:\akos\LB01.Cred"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Initialize-F5.iControl</span> <span style="color: navy;">-HostName</span> <span style="color: darkred;">"192.168.1.10"</span> <span style="color: navy;">-Credentials</span> <span style="color: orangered;">$creds</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
Great, now you have the power to enable and disable nodes within the loadbalancer pool with actions like:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Disable-F5.LTMNodeAddress</span> <span style="color: navy;">-Node</span> <span style="color: darkred;">"10.0.20.3" </span></div>
</div>
<br />
and<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Enable-F5.LTMNodeAddress</span> <span style="color: navy;">-Node</span> <span style="color: darkred;">"10.0.20.3" </span></div>
</div>
<br />
Now disabling and enabling is fine, but knowing if there's really no connections left is what you want to have. Luckily the Internet is a great place for info, and someone on <a href="http://stackoverflow.com/questions/17244218/automated-deployment-to-an-f5-load-balanced-environment" target="_blank">Stackoverflow created the following excellent code</a>, which I adapted a little to suit my needs. These two excellent functions do exactly what I want it to do, namely wait around for connections to drop (with a max time, don't want to spend waiting forever), and a supporting function to get the number of connections:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: darkblue;">function</span> <span style="color: blueviolet;">WaitForConnectionsToDrop</span>(</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgrey;">[</span><span style="color: teal;">int</span><span style="color: darkgrey;">]</span><span style="color: orangered;">$MaxWaitTime</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">300</span><span style="color: darkgrey;">,</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">]</span><span style="color: orangered;">$Node</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
)</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
{</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$connections</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">GetCurrentConnections</span> <span style="color: navy;">-Node</span> <span style="color: orangered;">$Node</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$elapsed</span> <span style="color: darkgrey;">=</span> <span style="color: darkgrey;">[</span><span style="color: teal;">System.Diagnostics.Stopwatch</span><span style="color: darkgrey;">]::</span>StartNew();</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">while</span>(<span style="color: orangered;">$connections</span> <span style="color: darkgrey;">-gt</span> <span style="color: purple;">0</span> <span style="color: darkgrey;">-and</span> <span style="color: orangered;">$elapsed</span><span style="color: darkgrey;">.</span>ElapsedMilliseconds <span style="color: darkgrey;">-lt</span> (<span style="color: orangered;">$MaxWaitTime</span> <span style="color: darkgrey;">*</span> <span style="color: purple;">1000</span>)){ </div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Start-Sleep</span> <span style="color: navy;">-Seconds</span> <span style="color: purple;">10</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$connections</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">GetCurrentConnections</span> <span style="color: navy;">-Node</span> <span style="color: orangered;">$Node</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">GetCurrentConnections</span>(</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgrey;">[</span><span style="color: teal;">string</span><span style="color: darkgrey;">]</span><span style="color: orangered;">$Node</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
)</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
{</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$ic</span><span style="color: darkgrey;">=</span><span style="color: blue;">Get-F5.iControl</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$connections</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$ic</span><span style="color: darkgrey;">.</span>LocalLBNodeAddress<span style="color: darkgrey;">.</span>get_statistics(<span style="color: orangered;">$Node</span>) <span style="color: darkgrey;">|</span> <span style="color: blue;">foreach</span>{<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>statistics<span style="color: darkgrey;">.</span>statistics <span style="color: darkgrey;">|</span> <span style="color: blue;">where</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>type <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"STATISTIC_SERVER_SIDE_CURRENT_CONNECTIONS"</span>} <span style="color: darkgrey;">|</span> <span style="color: blue;">foreach</span>{<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>value<span style="color: darkgrey;">.</span>low} }</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: darkred;">"</span><span style="color: orangered;">$Node</span><span style="color: darkred;"> has </span><span style="color: orangered;">$connections</span><span style="color: darkred;"> Connections"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">return</span> <span style="color: orangered;">$connections</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
<br /></div>
(note, that last few lines are broken up due to the format of the blog, it should be one line)<br />
The great thing is, now you can easily do the following:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: darkblue;">foreach</span> (<span style="color: orangered;">$node</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$serverlist</span>){</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Disable-F5.LTMNodeAddress</span> <span style="color: navy;">-Node</span> <span style="color: darkred;">"</span><span style="color: orangered;">$node</span><span style="color: darkred;">"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">WaitForConnectionsToDrop</span> <span style="color: navy;">-Node</span> <span style="color: darkred;">"</span><span style="color: orangered;">$node</span><span style="color: darkred;">"</span> <span style="color: navy;">-MaxWaitTime</span> <span style="color: purple;">300</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;">#insert whatever code you want to do, like, say upgrade vCPU's, or patch the host</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Enable-F5.LTMNodeAddress</span> <span style="color: navy;">-Node</span> <span style="color: darkred;">"</span><span style="color: orangered;">$node</span><span style="color: darkred;">"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
As you can tell, it is easy to create a script with this info that will easily disable and enable nodes. Yeay, PowerShell!Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-22523820588787007762017-01-28T23:11:00.000+01:002017-01-29T20:46:47.030+01:00Adding a Windows Nano server to the domain<br />
In my <a href="http://dont-trust-the-admin.blogspot.nl/2017/01/creating-nano-server-image.html" target="_blank">previous article</a>, I created a Nano server image. This one is about adding the Nano server to a domain. Adding Nano servers to a domain isn't a pretty little single cmdlet like many others are, but consists of a list of things to do:<br />
<br />
<ul>
<li>Configure DNS on the Nano server</li>
<li>Create what's called a blob file on a DC (or a machine that is joined to the domain, and a user that has rights to add a computer to the domain or as a Domain admin user)</li>
<li>Copy the file over to the Nano server</li>
<li>Use the blob file to add the server to the domain</li>
<li>Reboot</li>
</ul>
<div>
I've found the following code on petri.com site, with an ingenious way to copy the blob file over. If you have the Storage package installed, you can of course just copy it using a share like \\nanosrv1\c$</div>
<div>
<br /></div>
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: darkgreen;">#Set dns for Nano server</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">netsh</span> <span style="color: blueviolet;">interface</span> <span style="color: blueviolet;">ip</span> <span style="color: blueviolet;">set</span> <span style="color: blueviolet;">dnsservers</span> <span style="color: blueviolet;">name="Ethernet"</span> <span style="color: blueviolet;">static</span> <span style="color: blueviolet;">10.0.0.4</span> <span style="color: blueviolet;">primary</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;">#Create domain blob file</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">djoin.exe</span> <span style="color: blueviolet;">/provision</span> <span style="color: blueviolet;">/domain</span> <span style="color: blueviolet;">mydomain.int</span> <span style="color: blueviolet;">/machine</span> <span style="color: blueviolet;">nanosrv1</span> <span style="color: blueviolet;">/savefile</span> <span style="color: blueviolet;">c:\temp\odjblob</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;">#get file over to nano server, needs trustedhosts first</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Set-Item</span> <span style="color: blueviolet;">WSMan:\localhost\Client\TrustedHosts</span> <span style="color: darkred;">"10.0.0.5"</span> <span style="color: navy;">-Concatenate</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;">#trick from https://www.petri.com/join-windows-server-2016-nano-domain</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;">#to copy the blob over to the target server</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$filePath</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'c:\temp\odjblob'</span> </div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$fileContents</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$filePath</span> <span style="color: navy;">-Encoding</span> <span style="color: blueviolet;">Unicode</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$session</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-PSSession</span> <span style="color: navy;">-ComputerName</span> <span style="color: blueviolet;">10.0.0.5</span> <span style="color: navy;">-Credential</span> <span style="color: blueviolet;">nanosrv1\username</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Invoke-Command</span> <span style="color: navy;">-Session</span> <span style="color: orangered;">$session</span> <span style="color: navy;">-ArgumentList</span> @(<span style="color: orangered;">$filePath</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$fileContents</span>) <span style="color: navy;">-ScriptBlock</span> {</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">param</span>(<span style="color: orangered;">$filePath</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$data</span>)</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">New-Item</span> <span style="color: navy;">-ItemType</span> <span style="color: blueviolet;">directory</span> <span style="color: navy;">-Path</span> <span style="color: blueviolet;">c:\temp</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Set-Content</span> <span style="color: navy;">-Path</span> <span style="color: orangered;">$filePath</span> <span style="color: navy;">-Value</span> <span style="color: orangered;">$data</span> <span style="color: navy;">-Encoding</span> <span style="color: blueviolet;">Unicode</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;">#Now add the nano to the domain with the blobfile</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">djoin</span> <span style="color: blueviolet;">/requestodj</span> <span style="color: blueviolet;">/loadfile</span> <span style="color: blueviolet;">c:\temp\odjblob</span> <span style="color: blueviolet;">/windowspath</span> <span style="color: blueviolet;">c:\windows</span> <span style="color: blueviolet;">/localos</span> </div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">shutdown</span> <span style="color: blueviolet;">/r </span></div>
<br /></div>
It is a bit convoluted, but once you get the hang of it, it is doable..Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-44377034305920844962017-01-28T23:09:00.000+01:002017-01-30T21:47:30.400+01:00Creating a Nano Server ImageInstalling Windows 2016 Nano server isn't simply adding the DVD and clicking next-next-finish, but it needs some preparation. The base nano image wim file is only 168MB, but that has no functionality in itself. More needs to be added through features, drivers and packages.<br />
<br />
From a management workstation, mount the Windows 2016 ISO (i.e. doubleclick it, take note of the driveletter). On the ISO file you will see a NanoServer directory:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj8WI9eMgQYaPhl0YroRMCUMEKM1OO_nm9WE7bB1dhnneAqciMmzCydgU1j_WZeAaGS2bbL_i9vzUSIWZOA5p_WUJzPRRqHzMzYtBvooRlfgoN1_pWH4Hmc_9C4H-uZnEoodrkun1NB6q1/s1600/nano+dir.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj8WI9eMgQYaPhl0YroRMCUMEKM1OO_nm9WE7bB1dhnneAqciMmzCydgU1j_WZeAaGS2bbL_i9vzUSIWZOA5p_WUJzPRRqHzMzYtBvooRlfgoN1_pWH4Hmc_9C4H-uZnEoodrkun1NB6q1/s1600/nano+dir.png" /></a></div>
<br />
That directory has the NanoServer.wim file, Packages folder and a NanoServerImageGenerator folder. That last folder contains a PowerShell Module, which you need to import in an Administrator PowerShell window (which in my case is on the E: drive):<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Import-Module</span> <span style="color: blueviolet;">E:\NanoServer\NanoServerImageGenerator\NanoServerImageGenerator.psm1 </span></div>
</div>
<br />
Now you should have a bunch of extra commands available:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: whitesmoke;">PS C:\Users\Akos> get-command -Module *nano* | select Name</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;"><br /></span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Name </span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">---- </span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Edit-NanoServerImage </span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Get-NanoServerPackage</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">New-NanoServerImage </span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;"> </span></div>
</div>
<br />
The New-NanoServerImage cmdlet is the one you will need to create a fully functional image. The options are a slew of information. I've created a splatted variable set below, otherwise the line becomes far too long for the blog ;-)<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$Pwd</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">ConvertTo-SecureString</span> <span style="color: navy;">-String</span> <span style="color: darkred;">"Pa</span><span style="color: orangered;">$$</span><span style="color: darkred;">w0rd!"</span> <span style="color: navy;">-AsPlainText</span> <span style="color: navy;">-Force</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$Options</span> <span style="color: darkgrey;">=</span> @{</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
MediaPath <span style="color: darkgrey;">=</span> <span style="color: darkred;">'E:'</span> <span style="color: darkgreen;">#Location of DVD</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
BasePath <span style="color: darkgrey;">=</span> <span style="color: darkred;">'D:\TMP\Base'</span> <span style="color: darkgreen;">#Location of install files</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
TargetPath <span style="color: darkgrey;">=</span> <span style="color: darkred;">'D:\TMP\NanoWeb01.vhdx'</span> <span style="color: darkgreen;">#Where the vhdx will come</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
ComputerName <span style="color: darkgrey;">=</span> <span style="color: darkred;">'NanoWeb01'</span> <span style="color: darkgreen;">#No idea, I think the computername..</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
AdministratorPassword <span style="color: darkgrey;">=</span> <span style="color: darkred;">"</span><span style="color: orangered;">$Pwd</span><span style="color: darkred;">"</span> <span style="color: darkgreen;">#A secure string password</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
DeploymentType <span style="color: darkgrey;">=</span> <span style="color: darkred;">'Guest'</span> <span style="color: darkgreen;">#This will make it a vhdx file</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
Edition <span style="color: darkgrey;">=</span> <span style="color: darkred;">'Standard'</span> <span style="color: darkgreen;">#Windows version (Standard or Datacenter)</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
Package <span style="color: darkgrey;">=</span> <span style="color: darkred;">'Microsoft-NanoServer-IIS-Package'</span> <span style="color: darkgreen;">#Packages, in this case IIS</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">New-NanoServerImage</span> <span style="color: navy;">-EnableRemoteManagementPort</span> <span style="color: navy;">-Storage</span> <span style="color: orangered;">@Options</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
<br /></div>
This example creates a VM called NanoWeb01, with a password of "Pa$$w0rd!", and has IIS installed. The switch "<span style="background-color: white; color: navy; font-family: "lucida console"; font-size: 12px;">-EnableRemoteManagementPort</span>" does what it says. You can then connect to it via PSRemoting. The switch "<span style="background-color: white; color: navy; font-family: "lucida console"; font-size: 12px;">-Storage</span>" allows the Nano server to have filesharing available. If you don't need that, you can let it go, but for testing it's nice to have. This VM is ready for Hyper-V, and is about 560MB as it is configured like this.<br />
<br />
There are several packages you can choose from, which can be found by the Get-NanoServerPackage cmdlet:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: whitesmoke;">PS C:\Users\Akos> Get-NanoServerPackage -MediaPath e:</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-Compute-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-Containers-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-DCB-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-Defender-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-DNS-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-DSC-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-FailoverCluster-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-Guest-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-Host-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-IIS-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-OEM-Drivers-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-SCVMM-Compute-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-SCVMM-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-SecureStartup-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-ShieldedVM-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-SoftwareInventoryLogging-Package</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">Microsoft-NanoServer-Storage-Package </span></div>
</div>
<br />
Next to this there are other switches too, of which a complete list and more info can be found on <a href="https://technet.microsoft.com/windows-server-docs/get-started/deploy-nano-server" target="_blank">Microsoft's site</a>. In all, Nano server takes a little work to get it running, but once it does, it starts in seconds.<br />
<br />
Edit: You may have noticed the -Storage and -EnableRemoteManagementPort switches in my New-NanoServerImage command. I just saw in a different article, you can put them in the splatted options as well, by doing "Storage = $true" and the same for EnableRemoteManagementPort. You learn something new every day..Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-90907246604587136452017-01-26T13:20:00.000+01:002017-01-26T21:06:27.486+01:00Getting rid of trailing spaces in CSV's with PowershellThere I was, banging my head against the table because I thought I was perfectly able to select stuff from a CSV file, but I could not figure out why my selections weren't showing the stuff I wanted. It turns out my CSV file entries were full of trailing spaces!<br />
<br />
Googling around, I saw this in a forumpost somewhere, but to prevent me from losing it, I've put the script below:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$CSV</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">import-csv</span> <span style="color: darkred;">'C:\scripts\trailingspaces.csv'</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$CSV</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Foreach-Object</span> { </div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>PSObject<span style="color: darkgrey;">.</span>Properties <span style="color: darkgrey;">|</span> <span style="color: blue;">Foreach-Object</span> { <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>Value <span style="color: darkgrey;">=</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>Value<span style="color: darkgrey;">.</span>Trim() }</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$CSV</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">ConvertTo-Csv</span> <span style="color: navy;">-UseCulture</span> <span style="color: navy;">-NoTypeInformation</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Out-File</span> <span style="color: darkred;">'C:\scripts\trimmed.csv'</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
Yeay, no more going mad!Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com1tag:blogger.com,1999:blog-8814736065560944849.post-70161956234396877532017-01-26T11:33:00.000+01:002017-01-26T11:33:14.349+01:00Sending emailsShort one for my own recollection. Sending emails is easy from within PowerShell<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Send-MailMessage</span> <span style="color: navy;">-From</span> <span style="color: darkred;">"fromthevcenter@whatever.com"</span> <span style="color: navy;">-To</span> <span style="color: darkred;">"first.email@someaddress.com"</span><span style="color: darkgrey;">,</span> <span style="color: darkred;">"second.email@someaddress.com"</span> <span style="color: navy;">-SmtpServer</span> <span style="color: darkred;">"thesmtpaddress"</span> <span style="color: navy;">-Subject</span> <span style="color: darkred;">"VM Expansion </span><span style="color: orangered;">$VM</span><span style="color: darkred;"> Completed"</span> <span style="color: navy;">-Body</span> <span style="color: orangered;">$body</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
This was one of my requirements of <a href="http://dont-trust-the-admin.blogspot.co.uk/2017/01/automatically-upgrade-vcpus-part-1.html" target="_blank">my other post</a> about upgrading the vCPU's ;-)Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-58627953022294431632017-01-25T13:15:00.000+01:002017-01-25T13:15:33.062+01:00Store credentials securely for later useA command you use quite often is Get-Credential. For instance for creating a PSSession to a server:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$cred</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Credential</span> <span style="color: darkred;">"admin"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$s</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-PSSession</span> <span style="color: navy;">-Computername</span> <span style="color: blueviolet;">myserver</span> <span style="color: navy;">-Credential</span> <span style="color: orangered;">$cred</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
It's nice for on the spot use, but in a script that might not work. There are <a href="https://www.adminarsenal.com/admin-arsenal-blog/secure-password-with-powershell-encrypting-credentials-part-1/" target="_blank">ways</a> of doing that plaintext, but I saw a webpage that unfortunately I don't have the link for anymore, but if I find that reference I will add, but you can store those credentials securely too:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$Credential</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Credential</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$Credential</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Export-CliXml</span> <span style="color: navy;">-Path</span> <span style="color: darkred;">"D:\akos\Myserver.Cred"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
This small piece of code lets you store credentials in a directory that you can secure with NTFS rights, and also, it is encrypted, so you don't see the password in plain text in your script.<br />
<br />
Now if you want to use those credentials again, you can just load in the file:
<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$cred</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Import-CliXml</span> <span style="color: navy;">-Path</span> <span style="color: darkred;">"D:\akos\Myserver.Cred" </span></div>
</div>
<br />
Very nice!!Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-39932542252705647332017-01-25T11:57:00.000+01:002017-01-25T11:57:13.550+01:00Waiting for a VM to come backIn my previous post, I wrote about upgrading vCPU's, and that I want to do several other things as well. If you upgrade a vCPU on a VM, it could very well be that the Windows guest wants a second reboot. I've found that simply rebooting and waiting for a while is inefficient. So I usually check if the VMware tools are back online.<br />
<br />
I've made that into a function, to exactly that:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: darkblue;">function</span> <span style="color: blueviolet;">WaitforVM</span>(<span style="color: orangered;">$VM</span>)</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
{</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">while</span> (((<span style="color: blue;">get-vm</span> <span style="color: orangered;">$VM</span> )<span style="color: darkgrey;">.</span>ExtensionData<span style="color: darkgrey;">.</span>Guest<span style="color: darkgrey;">.</span>ToolsRunningStatus ) <span style="color: darkgrey;">-ne</span> <span style="color: darkred;">"guestToolsRunning"</span> )</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
{</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: darkred;">"Waiting for machine to come back....."</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">Yellow</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Sleep</span> <span style="color: purple;">5</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
} </div>
</div>
<br />
Now all you have to do, is to start the VM after the CPU addition, wait, then restart and wait again:<br />
<span style="color: blue; font-family: "lucida console"; font-size: 12px;"><br /></span>
<span style="color: blue; font-family: "lucida console"; font-size: 12px;">Start-VM</span><span style="font-family: "lucida console"; font-size: 12px;"> </span><span style="color: navy; font-family: "lucida console"; font-size: 12px;">-VM</span><span style="font-family: "lucida console"; font-size: 12px;"> </span><span style="color: orangered; font-family: "lucida console"; font-size: 12px;">$VM</span><span style="font-family: "lucida console"; font-size: 12px;"> </span><span style="color: navy; font-family: "lucida console"; font-size: 12px;">-Confirm:</span><span style="color: orangered; font-family: "lucida console"; font-size: 12px;">$False</span><br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">WaitforVM</span> <span style="color: navy;">-VM</span> <span style="color: orangered;">$VM</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Restart-VMGuest</span> <span style="color: navy;">-VM</span> <span style="color: orangered;">$VM</span> <span style="color: navy;">-Confirm:</span><span style="color: orangered;">$False</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">WaitforVM</span> <span style="color: navy;">-VM</span> <span style="color: orangered;">$VM </span></div>
</div>
<br />
Nice, right? Obviously this function can be used for other things, like being used in scripts when you create a new VM from template and boot it up to see if that VM is live already.Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-77406747299784512622017-01-25T11:16:00.002+01:002017-01-27T22:55:28.199+01:00Automatically upgrade vCPU'sI am lazy. Well, not lazy, I just don't like working at ungodly hours. Here's the story: I got this request to upgrade 2 customer VM's from 3 vCPU's to 4 vcpu's. No biggy, I thought. But he wanted it done after 1 AM.. Wait, that's not during office hours!<br />
<br />
OK, I could fix that with my trusty Powershell toolkit, and a scheduled task. But then I talked to the customer who went like: "Sure that's fine with me to script it, but make sure the sessions are drained from the webserver".. Hmm, F5 loadbalancers. I've not worked too much with those before, really. I get the general ideas within loadbalancing, but let's try and script against it.<br />
<br />
So I had a number of things I wanted to happen:<br />
<ul>
<li>Log in to the F5 loadbalancer, preferably not with a script filled with credentials hardcoded into it. </li>
<li>Wait an X amount of time for connections to drop from the specific pool</li>
<li>Shut down the machine, and wait for it to be properly turned off.</li>
<li>Set the number of CPU's and memory. This had an extra challenge, namely that the person who built the VM put in 1 vCPU with 3 cores, instead of 3 vCPU's.</li>
<li>Send an email with the results, so I can see what happened from my phone.</li>
</ul>
<div>
I'll spend a few posts on those different things.</div>
<div>
<br /></div>
<div>
Firstly the vCPU upgrade, which is the basis of this entire story:</div>
<div>
<br /></div>
<div>
Normally, you can turn off a VM, upgrade vCPU's, then start a VM again, through this simple set of commands:</div>
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$VM</span><span style="color: darkgrey;">=</span><span style="color: blue;">Get-VM</span> <span style="color: navy;">-Name</span> <span style="color: darkred;">'WEB01'</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Stop-VMGuest</span> <span style="color: navy;">–VM</span> <span style="color: orangered;">$VM</span> <span style="color: navy;">–Confirm:</span><span style="color: orangered;">$False</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">do</span> {</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$status</span> <span style="color: darkgrey;">=</span> (<span style="color: blue;">get-VM</span> <span style="color: orangered;">$VM</span>)<span style="color: darkgrey;">.</span>PowerState</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}<span style="color: darkblue;">until</span>(<span style="color: orangered;">$status</span> <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"PoweredOff"</span>)</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$VM</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Set-VM</span> <span style="color: navy;">-NumpCPU</span> <span style="color: purple;">4</span> <span style="color: navy;">–Confirm:</span><span style="color: orangered;">$False</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Start-VM </span></div>
<br /></div>
However, since someone used 1 vCPU with multiple cores instead of multiple vCPU sockets, things work a little differently. If you would try this command, the VM would be shut down, but no upgrade would happen, and start back up with the same amount as before.<br />
<br />
Fortunately there's another trick for that:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$VMSpec</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: navy;">-Type</span> <span style="color: blueviolet;">VMware.Vim.VirtualMachineConfigSpec</span> <span style="color: navy;">-Property</span> @{<span style="color: darkred;">"NumCoresPerSocket"</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">4</span>;<span style="color: darkred;">"numCPUs"</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">4</span>}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$VM</span><span style="color: darkgrey;">.</span>ExtensionData<span style="color: darkgrey;">.</span>ReconfigVM_Task(<span style="color: orangered;">$VMSpec</span>)</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
(That first line shouldn't be cut off like that, should be 1 line, but alas: blog template gets in the way)<br />
<br />
Now the VM gets upgraded to 4 vCPU's, although you would think it'd be 4x4 cpu's looking at the syntax.<br />
<br />
So now the final complete code would be:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Add-PSsnapin</span> <span style="color: blueviolet;">VMware.VimAutomation.Core</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Connect-VIServer</span> <span style="color: blueviolet;">myVcenter</span> <span style="color: navy;">-ErrorAction</span> <span style="color: blueviolet;">Stop</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$VM</span><span style="color: darkgrey;">=</span><span style="color: blue;">Get-VM</span> <span style="color: navy;">-Name</span> <span style="color: darkred;">'WEB01'</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Stop-VMGuest</span> <span style="color: navy;">–VM</span> <span style="color: orangered;">$VM</span> <span style="color: navy;">–Confirm:</span><span style="color: orangered;">$False</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">do</span> {</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$status</span> <span style="color: darkgrey;">=</span> (<span style="color: blue;">get-VM</span> <span style="color: orangered;">$VM</span>)<span style="color: darkgrey;">.</span>PowerState</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}<span style="color: darkblue;">until</span>(<span style="color: orangered;">$status</span> <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"PoweredOff"</span>)</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$VMSpec</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: navy;">-Type</span> <span style="color: blueviolet;">VMware.Vim.VirtualMachineConfigSpec</span> <span style="color: navy;">-Property</span> @{<span style="color: darkred;">"NumCoresPerSocket"</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">4</span>;<span style="color: darkred;">"numCPUs"</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">4</span>}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Start-VM</span> <span style="color: orangered;">$VM </span></div>
<br /></div>
<br />
Yeay, success! Now on to the next bit...Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-7948602686007329092016-07-29T11:26:00.000+02:002016-07-29T11:26:24.360+02:00Selecting and removing old snapshotsMe: "Say Customer X, I see you still have some snapshots here and there. Can I remove them? It's not wise to keep them for a long time."<br />
Customer X: "Oh yeah, I forgot about those. Well: those updates we did worked out fine, so anything from before 11-07-2016 can be deleted"<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-VM</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-Snapshot</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>Created <span style="color: darkgrey;">-lt</span> (<span style="color: blue;">Get-Date</span> <span style="color: blueviolet;">11-07-2016</span>) <span style="color: darkgrey;">|</span> <span style="color: blue;">Remove-Snapshot</span> <span style="color: navy;">-confirm:</span><span style="color: orangered;">$false </span></div>
</div>
<br />
Me: "OK, done!"<br />
<br />
Another possibility: "Can you give me a list of snapshots that are older than a week?"<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-VM</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-Snapshot</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>Created <span style="color: darkgrey;">-lt</span> ((<span style="color: blue;">Get-Date</span>)<span style="color: darkgrey;">.</span>AddDays(<span style="color: purple;">-7</span>))} <span style="color: darkgrey;">|</span> <span style="color: blue;">Select</span> <span style="color: blueviolet;">VM</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">Name </span></div>
</div>
<br />
Did I say how much I love Powershell/PowerCLI? ;-)Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-65088711758737461062016-07-27T13:52:00.001+02:002016-07-27T14:02:29.722+02:00Get the serialnumber via Powershell<span style="background-color: white; color: #333333; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 14px; line-height: 20px;">I quickly wanted to know a serial number for a certain HP Server, but didn't want to log on to the ILO card to get it from there. You don't have to, either:</span><br />
<span style="background-color: white; color: #333333; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 14px; line-height: 20px;"><br /></span>
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">gwmi</span> <span style="color: blueviolet;">win32_bios</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">fl</span> <span style="color: blueviolet;">SerialNumber </span></div>
</div>
<span style="background-color: white; color: #333333; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 14px; line-height: 20px;"><br /></span>
<span style="background-color: white; color: #333333; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica neue" , "helvetica" , sans-serif; font-size: 14px; line-height: 20px;">Works on most machines I've seen so far (HP and Dell).</span>Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-6243819133692401652016-07-21T21:54:00.002+02:002016-07-21T21:54:58.728+02:00Decode AsSecureString into cleartext I needed a way to enter a password from the console. This is done by typing:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$pwd</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Read-Host </span></div>
</div>
<br />
It works, but I didn't want this text to be visible on the screen while I'm typing it:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$pwd</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Read-Host</span> <span style="color: navy;">-AsSecureString </span></div>
</div>
<br />
Success! Now let's see the password in action:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: whitesmoke;">PS C:\Users\Akos> $pwd</span></div>
<div style="background-color: #012456; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: whitesmoke;">System.Security.SecureString </span></div>
</div>
<br />
.. Hmm, not quite what I had hoped for. But there is hope yet:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$cleartextpwd</span> <span style="color: darkgrey;">=</span> <span style="color: darkgrey;">[</span><span style="color: teal;">Runtime.InteropServices.Marshal</span><span style="color: darkgrey;">]::</span>PtrToStringAuto(<span style="color: darkgrey;">[</span><span style="color: teal;">Runtime.InteropServices.Marshal</span><span style="color: darkgrey;">]::</span>SecureStringToBSTR(<span style="color: orangered;">$pwd</span>)) </div>
</div>
<br />
Decodes the secure string into cleartext! Now you can type a password securely and use it. Be sure to remove the variable once you're done ;-)Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-9439944408769624182016-06-25T14:38:00.000+02:002016-06-25T15:07:18.022+02:00Using Cloudstack with PowershellIf you are trying to work with Powershell with a Cloudstack environment through the REST API, you will soon find that there aren't that many solutions available. For Linux/Python environments there is Cloudmonkey which is excellent, but there is no native "VMware PowerCLI" like tool available from either Apache or Citrix, and even googling for Cloudstack and Powershell give basically just two Github projects:<br />
<br />
<ul>
<li>A project that was last edited 3 years ago on Github from <a href="https://github.com/fifthecho/CloudStack-PowerShell" target="_blank">fifthecho</a></li>
<li>A project on Github from one of the engineers of Schuberg Philis, called <a href="https://github.com/schubergphilis/psCloudstack" target="_blank">psCloudstack</a></li>
</ul>
<br />
There is a project from <a href="https://www.exoscale.ch/syslog/2014/03/20/powershell-exoscale/" target="_blank">Exoscale</a> as well, but that is basically the Github contents from fifthecho, with perhaps some additions to it, and also from 2013.<br />
<br />
<b><span style="font-size: large;">Fifthecho</span></b><br />
The fifthecho project consists of a number of Powershell functions spread over a few.ps1 files, and a Cloudstack module that is called by every ps1 file to give the core functionality to talk to the Cloudstack API. Unfortunately it is not actively maintained, and all the files have an expired signature giving you an error when you try to execute the Powershell scripts. You can get around this by doing:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Set-ExecutionPolicy</span> <span style="color: navy;">-ExecutionPolicy</span> <span style="color: blueviolet;">Bypass</span> <span style="color: navy;">-Scope</span> <span style="color: blueviolet;">Process </span></div>
</div>
<br />
Basically for the Powershell session you are in, you are allowing everything, so it is not very elegant. Next to this, the Powershell scripts that are delivered only cover a small fraction of what is possible through the API (10 or so out of almost 250), and the way it is programmed is a bit dated, and I didn't feel like spending ages to create an extensive library myself, so I will let that be.<br />
<br />
<b><span style="font-size: large;">psCloudstack</span></b><br />
Then there is psCloudstack. I saw a <a href="https://www.youtube.com/watch?v=AC8MAOpCXcY" target="_blank">video about this module</a> a while ago from the creator himself, Hans van Veen, and I was impressed by the capability. This Powershell module takes a different approach: Instead of having a bunch of Powershell commands that talk to the API, it lists all the API commands (by virtue of the "listApis" command in the Cloudstack API library), and builds actual Powershell functions from them, complete with get-help functionality. These functions reside in memory, and will need to be "re-built" whenever you close the Powershell window. The elegance of this is that whenever you have a new version of Cloudstack, you have the complete API set to your disposal.<br />
<br />
I had issues with getting the module to work though, but since it was the most comprehensive Cloudstack module for Powershell, I wanted to make it work, so I took some time to troubleshoot what happens. I'll go over installation first:<br />
<br />
<b>Installation</b><br />
To download the module, go to the <a href="https://github.com/schubergphilis/psCloudstack" target="_blank">psCloudstack</a> Github page, and download the files.<br />
<br />
Create a module directory if you don't have one:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$PSModulePath</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Env:PSModulePath</span> <span style="color: darkgrey;">-split</span> <span style="color: darkred;">";"</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Select</span> <span style="color: navy;">-Index</span> (<span style="color: darkgrey;">[</span><span style="color: teal;">int</span><span style="color: darkgrey;">][</span><span style="color: teal;">bool</span><span style="color: darkgrey;">]</span><span style="color: orangered;">$Global</span>)</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">New-Item</span> <span style="color: navy;">-ItemType</span> <span style="color: blueviolet;">Directory</span> <span style="color: orangered;">$PSModulePath</span><span style="color: blueviolet;">\psCloudstack </span></div>
</div>
<br />
Then unzip all psCloudstack files into that directory (especially the .psm1 and psd1 file). Windows recognizes things that are downloaded from the Internet so you need to rightclick the psm1 and psd1 file and select Properties, then click the Unblock button.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimVzxY71AxrQYO8B-6Sd_4zd7w4O2tYNESWUOavFErfZ-AJLCRQ8Tu5fdPT7icnuq_J9Dv72eEl2NHMP72wGKux_BT6STJIwmyxRdAO_WGSLhWU0L21y6uy-3jNEUkua7YsBM-4LXL3TU3/s1600/unblock.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimVzxY71AxrQYO8B-6Sd_4zd7w4O2tYNESWUOavFErfZ-AJLCRQ8Tu5fdPT7icnuq_J9Dv72eEl2NHMP72wGKux_BT6STJIwmyxRdAO_WGSLhWU0L21y6uy-3jNEUkua7YsBM-4LXL3TU3/s1600/unblock.png" /></a></div>
<br />
If you have a Windows 10 machine, or have WMF5 installed, you can do it with this script:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$PSModulePath</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Env:PSModulePath</span> <span style="color: darkgrey;">-split</span> <span style="color: darkred;">";"</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Select</span> <span style="color: navy;">-Index</span> (<span style="color: darkgrey;">[</span><span style="color: teal;">int</span><span style="color: darkgrey;">][</span><span style="color: teal;">bool</span><span style="color: darkgrey;">]</span><span style="color: orangered;">$Global</span>)</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">New-Item</span> <span style="color: navy;">-ItemType</span> <span style="color: blueviolet;">Directory</span> <span style="color: orangered;">$PSModulePath</span><span style="color: blueviolet;">\psCloudstack</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Invoke-WebRequest</span> <span style="color: navy;">-Uri</span> <span style="color: darkred;">"https://raw.githubusercontent.com/schubergphilis/psCloudstack/master/PSCloudstack.psm1"</span> <span style="color: navy;">-OutFile</span> <span style="color: orangered;">$PSModulePath</span><span style="color: blueviolet;">\psCloudstack\PSCloudstack.psm1</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Invoke-WebRequest</span> <span style="color: navy;">-Uri</span> <span style="color: darkred;">"https://raw.githubusercontent.com/schubergphilis/psCloudstack/master/PSCloudstack.psd1"</span> <span style="color: navy;">-OutFile</span> <span style="color: orangered;">$PSModulePath</span><span style="color: blueviolet;">\psCloudstack\PSCloudstack.psd1</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Unblock-File</span> <span style="color: orangered;">$PSModulePath</span><span style="color: blueviolet;">\psCloudstack\PSCloudstack.psd1</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Unblock-File</span> <span style="color: orangered;">$PSModulePath</span><span style="color: blueviolet;">\psCloudstack\PSCloudstack.psm1</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
<b>Configuration</b><br />
So, now you have psCloudstack on your machine. Time to add a configuration. If you list the commands from the module you get the following list:<br />
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> Get-Command -Module pscloudstack
CommandType Name Version Source
----------- ---- ------- ------
Function Add-CSConfig 3.2.1 pscloudstack
Function Connect-CSManager 3.2.1 pscloudstack
Function Convert-CSConfig 3.2.1 pscloudstack
Function Get-CSConfig 3.2.1 pscloudstack
Function Invoke-CSApiCall 3.2.1 pscloudstack
Function Remove-CSConfig 3.2.1 pscloudstack
Function Set-CSConfig 3.2.1 pscloudstack
Function Start-CSConsoleSession 3.2.1 pscloudstack
</pre>
Most commands in the module have to do with the configuration, but we just need Add-CSConfig, so let's look how we should use it:
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> get-help Add-CSConfig
NAME
Add-CSConfig
SYNOPSIS
Adds connection configuration information to the configuration file
SYNTAX
Add-CSConfig [[-Zone] <string>] [-Server] <string> [[-SecurePort] <int32>] [[-UnsecurePort] <int32>]
[-Apikey] <string> [-Secret] <string> [-UseSSL] [[-ConfigFile] <string>] [<commonparameters>]
DESCRIPTION
This function is used to add connection configuration information to the configuration file.
PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> get-help Add-CSConfig -Examples
NAME
Add-CSConfig
SYNOPSIS
Adds connection configuration information to the configuration file
-------------------------- EXAMPLE 1 --------------------------
PS C:\># Create/Update the content of the default config file
C:\PS> Add-CSConfig -Server www.xxx.yyy.zzz -Api xxxxxxx -Secret yyyyyyyyy
</commonparameters></string></string></string></int32></int32></string></string></pre>
This, unfortunately, is where the first "bug" occurs for me, when trying to execute this command. When executing this command without the -UseSSL switch you get an error:<br />
<br />
<pre class="PoshConsole">Add-CSConfig -Server https://myservices.interoute.com/myservices/api/vdc -Apikey xxx -Secret yyy
<span style="color: red;">Add-CSConfig : Cannot process argument transformation on parameter 'UseSSL'. Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.</span></pre>
<br />
When try use it with the -UseSSL (and without the "https://" prefix, there is no feedback, so let's try to connect:<br />
<pre class="PoshConsole">Add-CSConfig -Server myservices.interoute.com/myservices/api/vdc -Apikey xxx -Secret yyy -UseSSL
</pre>
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> Connect-CSManager
Welcome to psCloudstack V3.2.1, ...
API Call Error:
Oops...
The application has encountered an unknown error.
Our technical staff have been automatically notified and will be looking into this with the u
tmost urgency.
psCloudstack-version displaytext
-------------------- -----------
3.2.1 ...
</pre>
<br />
Whoops, what happened?.. It was time to start looking at the module itself, because something was happening, but I didn't know what. I see that by default, the config file is put in $env:LocalAppData (a.k.a. C:\Users\<username>\AppData\Local ), so when I look at that file, I see unusual ports being used (8080 and 8096):<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizMhSch64_FWacgf9ymYaXJf3f1NJWOXDLOSgEXy9fOJnFgfDJvUkxBTBBZm9Evx1YeSPYB_W_8qk8js_kaLxsCcFEoCkCLE0W0fFE9ogCGiRen1ae4w8z2YL5BXmXIYQ_gE5FOSjDI29Q/s1600/xml.png" imageanchor="1"><img border="0" height="70" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizMhSch64_FWacgf9ymYaXJf3f1NJWOXDLOSgEXy9fOJnFgfDJvUkxBTBBZm9Evx1YeSPYB_W_8qk8js_kaLxsCcFEoCkCLE0W0fFE9ogCGiRen1ae4w8z2YL5BXmXIYQ_gE5FOSjDI29Q/s640/xml.png" width="640" /></a><br />
<br />
Instead of trying to redo the Add-CSConfig, I decided to edit the file directly, putting 443 an 80. Now lets try again:<br />
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> Connect-CSManager
Welcome to psCloudstack V3.2.1, ...
API Call Error:
Oops...
The application has encountered an unknown error.
Our technical staff have been automatically notified and will be looking into this with the u
tmost urgency.
psCloudstack-version displaytext
-------------------- -----------
3.2.1 ...
</pre>
<br />
More troubleshooting was needed. I will spare you the gritty details, but eventually I used the fifthecho module to create a connectionstring that I knew worked, and compared it to the connectionstring that the psCloudstack module produced, and I found what was the issue for me:<br />
<br />
In line 1036, there is the following statement:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$baseUrl</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"{0}://{1}:{2}/client/{3}?{4}"</span> <span style="color: darkgrey;">-f</span> <span style="color: orangered;">$Protocol</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$InputObject</span><span style="color: darkgrey;">.</span>Server<span style="color: darkgrey;">,</span> <span style="color: orangered;">$Port</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$InputObject</span><span style="color: darkgrey;">.</span>Type<span style="color: darkgrey;">,</span> <span style="color: orangered;">$InputObject</span><span style="color: darkgrey;">.</span>Command </div>
</div>
<br />
which creates something like <b>https://url:443/client/api?convertedstringwhichholdsapicall</b>.<br />
<br />
In the mean time, the Cloudstack implementation from Interoute VDC wants:<br />
<br />
<b>https://url/?convertedstringwhichholdsapicall</b><br />
<br />
So by changing the way the string is connecting, the problem should be solved. Comment out the line, and put the line below under it.<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: darkgreen;">#$baseUrl = "{0}://{1}:{2}/client/{3}?{4}" -f $Protocol, $InputObject.Server, $Port, $InputObject.Type, $InputObject.Command</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$baseUrl</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"{0}://{1}?{2}"</span> <span style="color: darkgrey;">-f</span> <span style="color: orangered;">$Protocol</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$InputObject</span><span style="color: darkgrey;">.</span>Server<span style="color: darkgrey;">,</span> <span style="color: orangered;">$InputObject</span><span style="color: darkgrey;">.</span>Command </div>
</div>
<br />
Now let's try it once more (do a "remove-module psCloudstack" first to reload the module):<br />
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> Connect-CSManager
Welcome to psCloudstack V3.2.1, ...generating 252 api functions for you
</pre>
Yeay, success! Now when you do Get-Command -Module psCloudstack you'll see a whole bunch of functions. These functions are the actual API calls that you can make to the Cloudstack environment:<br />
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> Get-Command -Module psCloudstack
CommandType Name Version Source
----------- ---- ------- ------
Function activateProject 3.2.1 PSCloudstack
Function addAccountToProject 3.2.1 PSCloudstack
Function Add-CSConfig 3.2.1 PSCloudstack
Function addIpToNic 3.2.1 PSCloudstack
Function addNicToVirtualMachine 3.2.1 PSCloudstack
Function addVpnUser 3.2.1 PSCloudstack
Function archiveEvents 3.2.1 PSCloudstack
Function assignCertToLoadBalancer 3.2.1 PSCloudstack
Function assignToGlobalLoadBalancerRule 3.2.1 PSCloudstack
Function assignToLoadBalancerRule 3.2.1 PSCloudstack
Function assignVirtualMachine 3.2.1 PSCloudstack
Function associateIpAddress 3.2.1 PSCloudstack
Function attachIso 3.2.1 PSCloudstack
Function attachVolume 3.2.1 PSCloudstack
Function authorizeSecurityGroupEgress 3.2.1 PSCloudstack
Function authorizeSecurityGroupIngress 3.2.1 PSCloudstack
Function changeServiceForRouter 3.2.1 PSCloudstack
Function changeServiceForVirtualMachine 3.2.1 PSCloudstack
Function configureInternalLoadBalancerElement 3.2.1 PSCloudstack
Function configureVirtualRouterElement 3.2.1 PSCloudstack
Function Connect-CSManager 3.2.1 PSCloudstack
Function Convert-CSConfig 3.2.1 PSCloudstack
.
.
.
</pre>
252 modules are created. Note that your Cloudstack provider could deny access to some API calls.<br />
<br />
Example to list and create VM's:
<br />
<pre class="PoshConsole">PS C:\Users\Akos\Documents\WindowsPowerShell\Modules> listVirtualMachines |select name, templatename
name templatename
---- ------------
server01 ubuntu1404_minimal
</pre>
<br />
Awesome! and when you do Get-Help on one of these functions, you see actual help: Super stuff from Hans van Veen. He is maintaining this module, and even while I was writing this blogpost, he updated the module again. Unfortunately, the most recent update he made will give you an error, as it is trying to use some API calls that have been disallowed from usage by the Interoute VDC API (which is using Cloudstack), but the functions are still created and work.<br />
<br />
I may want to modify the module so that the functions don't just live in memory (and you first need to run Connect-CSManager), but are in a module of its own, and I may want to modify the connection functionality, so it "just works" (at least with the Cloudstack environment that I have at my disposal).<br />
<br />
Somewhere in another post I will also add some examples to use this excellent module.Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com1tag:blogger.com,1999:blog-8814736065560944849.post-173920575016572902016-06-16T22:50:00.000+02:002016-06-16T22:50:09.087+02:00Desired State Configuration - Part 1I have been diving into Powershell Desired State Configuration lately, as the technology is now a few years old and a little more mature, especially with the coming of Powershell 5. Many in the industry named this technology <i>the</i> technology that <a href="https://www.youtube.com/watch?v=kuzFUI5Id0g" target="_blank">you should learn</a> last year or the year before that already and not to mention that Microsoft keeps <a href="https://channel9.msdn.com/Events/TechNetVirtualConference/TechNetVC2016/JeffreySnoverThrivingthroughTransition" target="_blank">hammering</a> on it that you should learn about it.<br />
<br />
So what is Powershell DSC? In its core it is a configuration management platform. It allows for systems and applications to be installed and configured by means of a configuration document, and can manage configuration drift. Configuration management tools are not new, and have been around for a while with other tools like <a href="https://puppet.com/" target="_blank">Puppet</a>, <a href="https://www.chef.io/chef/" target="_blank">Chef</a>, <a href="https://www.ansible.com/" target="_blank">Ansible</a>, <a href="https://saltstack.com/" target="_blank">Salt</a> and others.They serve the same purpose with varying features, but are mostly catered to Linux platforms. The same way that Powershell was an answer to having a proper commandline automation tool for Windows that supported its .Net and object oriented nature, DSC is Microsoft's answer to Configuration Management. But Microsoft went further by making DSC a platform that other tools use. So instead of competing against companies like Pupped and Chef, Microsoft has actively engaged with these companies so they could use the technology to better automatically configure Windows systems and applications.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_1yuSm_nmESwlh5z0T1hFp47qxeze5upTPDAD0xiL47Fn0kJzqFrOE08oUxAJxsf0zv8IZWMkDbdHX_pei8pZbFwYbEzWRTFWoulQ-illDex_Vfmz_shBS9vxoEIxNBmGrfQGBiZFrhSz/s1600/giphy.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="237" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_1yuSm_nmESwlh5z0T1hFp47qxeze5upTPDAD0xiL47Fn0kJzqFrOE08oUxAJxsf0zv8IZWMkDbdHX_pei8pZbFwYbEzWRTFWoulQ-illDex_Vfmz_shBS9vxoEIxNBmGrfQGBiZFrhSz/s320/giphy.gif" width="320" /></a><br />
Powershell and DSC inventor Jeffrey Snover describes DSC as captain Picard on the Starship Enterprise ordering Riker to go to that distant planet by saying "Make it so number one", and Riker executes that order: Picard desires something, and Riker "makes it so". DSC is basically the same thing inside your computer: With DSC, you desire something to be in a certain state through a configuration file and the local agent (called the Local Configuration Manager a.k.a. LCM) makes it so. This LCM is not all knowing in itself, but it uses things called DSC Resources to do the tasks you want it to do. These resources are now rapidly being developed by both Microsoft as well as the community around it, but if you can't find the thing you want to have configured, you can create them yourself. The main source for resources is the Powershell Gallery, and Powershell 5 has got the PowerShellGet module which now lets you install modules straight from this repository, just like you would use apt-get from a linux repository.<br />
<br />
<b>What makes DSC so cool?</b><br />
<ul>
<li>You can deploy a system complete with applications and settings, nuke it and deploy the exact same machine (which makes devs happy because your dev matches your prod environment exactly)</li>
<li>Your OS/application is as you documented it in your configuration file, and configuration drift can be corrected by DSC</li>
<li>The DSC configuration is a Powershell based script, so you can use Powershell for variables and such, and do troubleshooting with it too</li>
<li>DSC can be used not only for Windows systems, but is slowly making its way to hardware devices too (e.g. Cisco Nexus)</li>
<li>There's linux support too, but as far as I've seen it, that's a bit of a work in progress. Cool nonetheless. </li>
<li>Puppet/Chef/Salt/Ansible support the use of the DSC resources!</li>
<li>Windows Nano Server doesn't support Group Policies, but it supports DSC instead</li>
</ul>
There are more cool things about it, and DSC resources are added daily to make it even cooler. In another post I will go over the more technical details.Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-25335606722409247272016-04-14T12:03:00.000+02:002016-04-14T12:03:37.160+02:00Surpress SSH and shell warnings on ESXiSmall oneliner to surpress the warning you get when you enable SSH: <br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7zEDgE3a-_hQF-EVxVwEcTyDXn_NC68mpl_jJ9kg09coYvWfuTbBqQWSijAdVyC1Bns2EvcJdyKKf8IRqEC_WicUQ_IvP9IyEW7zcM6mddMlY_b_1KC-lP_9LQ8roxh5dqVDeEBgSr3OG/s1600/warning.png" imageanchor="1"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7zEDgE3a-_hQF-EVxVwEcTyDXn_NC68mpl_jJ9kg09coYvWfuTbBqQWSijAdVyC1Bns2EvcJdyKKf8IRqEC_WicUQ_IvP9IyEW7zcM6mddMlY_b_1KC-lP_9LQ8roxh5dqVDeEBgSr3OG/s400/warning.png" /></a><br />
<br />
Like so:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-VMHost</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Set-VmHostAdvancedConfiguration</span> <span style="color: navy;">-Name</span> <span style="color: blueviolet;">UserVars.SuppressShellWarning</span> <span style="color: navy;">-Value</span> <span style="color: purple;">1 </span></div>
</div>
Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-19493541260324289042016-03-14T15:44:00.000+01:002016-03-14T15:44:14.450+01:00Wait-Job, how niceA few posts ago I had a bit of a tacky way to wait until a job is done:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: darkblue;">While</span> (<span style="color: blue;">Get-Job</span> <span style="color: navy;">-State</span> <span style="color: darkred;">"Running"</span>) {</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">Yellow</span> <span style="color: darkred;">"Running..."</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Start-Sleep</span> <span style="color: purple;">1</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
Turns out, there is a much more elegant function to do this:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Wait-Job</span> <span style="color: navy;">-State</span> <span style="color: blueviolet;">Running </span></div>
</div>
<br />
or as the built in example shows:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Get-Job</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Wait-Job </span></div>
</div>
<br />
This command waits for all of the background jobs running in the session to complete. Nice!Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-83862803209057935242016-03-11T15:22:00.003+01:002016-03-11T15:24:50.908+01:00Beautify HTML output from ConvertTo-HTMLQuick one to write down before I forget it again, and have to search the interwebs again:<br />
<br />
Whenever you use ConvertTo-Html, the output is usually.. meh. However, you can add some CSS styling and include it in the converting process:<br />
<br />
<DIV STYLE="text-align:Left;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:12;color:#000000;"><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="font-size:16;"><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$Header</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>=</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN>@"</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN><style></SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>Body{background-color:white;font-family:Arial;font-size:10pt;}</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>Table{border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>TH{border-width: 1px; padding: 2px; border-style: solid; border-color: black; background-color: #cccccc;}</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>TD{border-width: 1px; padding: 5px; border-style: solid; border-color: black; background-color: white;}</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN></style></SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>"@</SPAN></SPAN></P><P /><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#0000FF;"><SPAN>Get-Process</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>|</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#0000FF;"><SPAN>ConvertTo-Html</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#000080;"><SPAN>-Head</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$Header</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>|</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#0000FF;"><SPAN>Out-File</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8A2BE2;"><SPAN>C:\temp\process.htm</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN><SPAN> </SPAN></SPAN></P></DIV>
<br />
Which gives a little more oomph to your HTML output:<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt73GNd3ilkDIBb-OYMVCuWK9f9swGu-qMmriYuKbHpG3dp3HUAxPdrhic7nUfeZdsTj22UjCgowdTKV3k0XLuEpz04yiYqRN6OwTvLbrpKunKdSt19UfNlpscpWe5pH4KIe_hCijUYNsn/s1600/process.png" imageanchor="1"><img border="0" height="217" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt73GNd3ilkDIBb-OYMVCuWK9f9swGu-qMmriYuKbHpG3dp3HUAxPdrhic7nUfeZdsTj22UjCgowdTKV3k0XLuEpz04yiYqRN6OwTvLbrpKunKdSt19UfNlpscpWe5pH4KIe_hCijUYNsn/s400/process.png" width="400" /></a><br />
<br />
You can also put the CSS code in a separate file (e,g, D:\table.css), and include the CSS with <br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">ConvertTo-Html</span> <span style="color: navy;">-CssUri</span> <span style="color: darkred;">"D:\table.css" </span></div>
</div>Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-86329922920387623312016-02-25T22:09:00.000+01:002016-02-25T22:10:06.918+01:00Getting an overview of reboots revisitedIn my <a href="http://dont-trust-the-admin.blogspot.nl/2016/02/a-quick-one-that-i-wanted-to-keep-for.html" target="_blank">previous post</a> I mentioned invoke-command using the -AsJob option. But then I saw a post from <a href="http://tommymaynard.com/quick-learn-keep-powershell-cmdlets-powerful-2016/" target="_blank">Tommy Maynard</a> that made things even quicker and certainly a bit easier.<br />
<br />
Instead of doing a "foreach server in serverlist" loop to run the invoke-command scriptblock, you can do an invoke-command on the entire serverlist in one go!<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$ServerList</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: blueviolet;">C:\Scripts\ServerList.txt.txt</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;"><br /></span>
<span style="color: orangered;">$block</span> <span style="color: darkgrey;">=</span> {<span style="color: blue;">Get-EventLog</span> <span style="color: blueviolet;">System</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where-Object</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1074"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"6008"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1076"</span>} <span style="color: darkgrey;">|</span> <span style="color: blue;">Select</span> <span style="color: blueviolet;">Machinename</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">TimeWritten</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">UserName</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">EventID</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">Message</span>}</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;"><br /></span>
<span style="color: blue;">Invoke-Command</span> <span style="color: navy;">-ComputerName</span> <span style="color: orangered;">$ServerList</span> <span style="color: navy;">-ScriptBlock</span> <span style="color: orangered;">$block</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">ft</span> <span style="color: navy;">-AutoSize</span> <span style="color: navy;">-Wrap</span> <span style="color: darkgrey;">|</span><span style="color: blue;">Out-File</span> <span style="color: blueviolet;">C:\scripts\ServerRestart.txt</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
</div>
</div>
<br />
This certainly cuts down on scriptsize :-)<br />
<br />
How much time you save depends on the amount of servers, but I tested it with 5 VM's, and saw some surprising things in my test: Invoke-command feeding the serverlist as described above actually took longer than invoke-command using the -Asjob option.<br />
<br />
Usually I get 14 seconds for the old fashioned "foreach" test, then about 6 seconds for the invoke-command feeding the serverlist, and about 2 seconds for the invoke-command with -Asjob.<br />
<br />
Here's the test I ran a few times after another:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$ServerList</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: blueviolet;">C:\Scripts\ServerList.txt.txt</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"># Test 1: Old fashioned way</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">green</span> <span style="color: darkred;">"Foreach server in serverlist"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Measure-Command</span> {<span style="color: darkblue;">foreach</span> (<span style="color: orangered;">$server</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$ServerList</span>){<span style="color: blue;">Get-EventLog</span> <span style="color: blueviolet;">System</span> <span style="color: navy;">-ComputerName</span> <span style="color: darkred;">"</span><span style="color: orangered;">$server</span><span style="color: darkred;">"</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where-Object</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1074"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"6008"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1076"</span>} <span style="color: darkgrey;">|</span> <span style="color: blue;">ft</span> <span style="color: blueviolet;">Machinename</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">TimeWritten</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">UserName</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">EventID</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">Message</span> <span style="color: navy;">-AutoSize</span>}}<span style="color: darkgrey;">|</span><span style="color: blue;">select</span> <span style="color: blueviolet;">Seconds</span><span style="color: darkgrey;">|</span><span style="color: blue;">ft</span> <span style="color: navy;">-AutoSize</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"># Test 2: Doing the test with invoke-command, feeding the serverlist in one go</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$block</span> <span style="color: darkgrey;">=</span> {<span style="color: blue;">Get-EventLog</span> <span style="color: blueviolet;">System</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where-Object</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1074"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"6008"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1076"</span>}}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">green</span> <span style="color: darkred;">"invoke with serverlist"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Measure-Command</span> {<span style="color: blue;">Invoke-Command</span> <span style="color: navy;">-ComputerName</span> <span style="color: orangered;">$ServerList</span> <span style="color: navy;">-ScriptBlock</span> <span style="color: orangered;">$block</span>}<span style="color: darkgrey;">|</span><span style="color: blue;">select</span> <span style="color: blueviolet;">Seconds</span><span style="color: darkgrey;">|</span><span style="color: blue;">ft</span> <span style="color: navy;">-AutoSize</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkgreen;"># Test 3: Doing the test with "Job" functionality</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">green</span> <span style="color: darkred;">"Job Functionality"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Measure-Command</span> {</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">foreach</span> (<span style="color: orangered;">$Server</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$ServerList</span>){</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Invoke-Command</span> <span style="color: navy;">-ComputerName</span> <span style="color: orangered;">$Server</span> <span style="color: navy;">-ScriptBlock</span> <span style="color: orangered;">$block</span> <span style="color: navy;">-AsJob</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">While</span> (<span style="color: blue;">Get-Job</span> <span style="color: navy;">-State</span> <span style="color: darkred;">"Running"</span>) {</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
} </div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Get-Job</span><span style="color: darkgrey;">|</span> <span style="color: blue;">Receive-Job</span> <span style="color: darkgrey;">|</span><span style="color: blue;">ft</span> <span style="color: navy;">-AutoSize</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Get-Job</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Remove-Job</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">Yellow</span> <span style="color: darkred;">"Done!"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}<span style="color: darkgrey;">|</span><span style="color: blue;">select</span> <span style="color: blueviolet;">Seconds</span><span style="color: darkgrey;">|</span><span style="color: blue;">ft</span> <span style="color: navy;">-AutoSize</span></div>
<br /></div>
The weird thing however, is if I swap test 2 and 3 around, invoke-command using -Asjob consistently works slower than the invoke-command with serverlist. The old fashioned test still took 14 seconds, but the invoke-command with -Asjob took 2 to 6 seconds, and invoke-command feeding the serverlist consistently took 1 second.<br />
<br />
<span style="font-size: large;">Last try:</span><br />
<br />
So I wasn't sure why the last piece of the script went faster than the middle part, but swapping it consistently made the latter one faster. So I ran each of the two tests in a loop for about 10 times in a row, and lo and behold: The invoke-command using -Asjob was running at 1-2 seconds, and feeding the serverlist in one go ran consistently for 1 second. For both tests, the first run took about 6 seconds, and this could be the same delay that is apparent in the test I listed above.<br />
<br />
I assume that somehow a connection is made using Invoke-Command, and this session somehow stays alive.<br />
<br />
<u>In the end</u>: Feed invoke-command the serverlist as a whole. It's easier to manage. and a little bit faster consistently.Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-19622171322213010872016-02-19T16:32:00.002+01:002016-02-23T11:52:27.943+01:00Getting an overview of server rebootsA quick one that I wanted to keep for reference, after a forum post on <a href="http://powershell.org/wp/forums/topic/running-powershell-comand-against-a-list-of-servers/" target="_blank">powershell.org</a>. Someone wanted to have a list of reboot times from the eventlog and put it in a text file, so I commented:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$ServerList</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: darkred;">"C:\Scripts\ServerList.txt"</span></div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">foreach</span> (<span style="color: orangered;">$i</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$ServerList</span>){</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Output</span> <span style="color: orangered;">$i</span> <span style="color: darkred;">"`n"</span><span style="color: blueviolet;">+"=========================="</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Out-File</span> <span style="color: navy;">-FilePath</span> <span style="color: blueviolet;">c:\scripts\ServerRestart.txt</span> <span style="color: navy;">-Append</span><br />
<span style="color: navy;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$Output</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-EventLog</span> <span style="color: blueviolet;">System</span> <span style="color: navy;">-ComputerName</span> <span style="color: darkred;">"</span><span style="color: orangered;">$i</span><span style="color: darkred;">"</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where-Object</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1074"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"6008"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1076"</span>} <span style="color: darkgrey;">|</span> <span style="color: blue;">ft</span> <span style="color: blueviolet;">Machinename</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">TimeWritten</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">UserName</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">EventID</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">Message</span> <span style="color: navy;">-AutoSize</span> <span style="color: navy;">-Wrap</span><br />
<span style="color: navy;"><br /></span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$Output</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Out-File</span> <span style="color: navy;">-FilePath</span> <span style="color: blueviolet;">c:\scripts\ServerRestart.txt</span> <span style="color: navy;">-Append</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
} </div>
</div>
<br />
This obviously only works if you have enough credentials to access the remote server. This in itself should do the job for not too many servers, or lots in due time, because it runs on one server at a time.<br />
<br />
If you want to kick things up a notch, and parallelize things, you can run it as a job with Invoke-Command with "-Asjob":<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: orangered;">$ServerList</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Content</span> <span style="color: darkred;">"C:\Scripts\ServerList.txt"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<br /></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: orangered;">$block</span> <span style="color: darkgrey;">=</span> {<span style="color: blue;">Get-EventLog</span> <span style="color: blueviolet;">System</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Where-Object</span> {<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1074"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"6008"</span> <span style="color: darkgrey;">-or</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span>EventID <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">"1076"</span>} <span style="color: darkgrey;">|</span> <span style="color: blue;">ft</span> <span style="color: blueviolet;">Machinename</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">TimeWritten</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">UserName</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">EventID</span><span style="color: darkgrey;">,</span> <span style="color: blueviolet;">Message</span> <span style="color: navy;">-AutoSize</span> <span style="color: navy;">-Wrap</span> }</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">foreach</span> (<span style="color: orangered;">$Server</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$ServerList</span>){</div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Invoke-Command</span> <span style="color: navy;">-ComputerName</span> <span style="color: orangered;">$Server</span> <span style="color: navy;">-ScriptBlock</span> <span style="color: orangered;">$block</span> <span style="color: navy;">-AsJob</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
}</div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: darkblue;">While</span> (<span style="color: blue;">Get-Job</span> <span style="color: navy;">-State</span> <span style="color: darkred;">"Running"</span>) { </div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">Yellow</span> <span style="color: darkred;">"Running..."</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Start-Sleep</span> <span style="color: purple;">1</span> </div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
} </div>
<br />
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Get-Job</span><span style="color: darkgrey;">|</span> <span style="color: blue;">Receive-Job</span> <span style="color: darkgrey;">|</span><span style="color: blue;">Out-File</span> <span style="color: blueviolet;">C:\scripts\ServerRestart.txt</span></div>
<span style="background-color: white; color: blue; font-family: "lucida console";">Write-Host</span><span style="background-color: white; font-family: "lucida console";"> </span><span style="background-color: white; color: navy; font-family: "lucida console";">-ForegroundColor</span><span style="background-color: white; font-family: "lucida console";"> </span><span style="background-color: white; color: blueviolet; font-family: "lucida console";">Yellow</span><span style="background-color: white; font-family: "lucida console";"> </span><span style="background-color: white; color: darkred; font-family: "lucida console";">"Done!"</span><br />
<br />
<br /></div>
Which will give the following output:<br />
<br />
<pre class="PoshConsole">Get-Job| Receive-Job |Out-File C:\scripts\ServerRestart.txt
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
35 Job35 RemoteJob Running True localhost Get-EventLog System | ...
37 Job37 RemoteJob Running True 127.0.0.1 Get-EventLog System | ...
Running...
Running...
Running...
Running...
Running...
Done!
</pre>
<br />
This should speed things up a bit, as now all machines run it simultaneously. The only thing that doesn't get done that way is the divider-line at the top.<br />
<br />
To be totally neat, you should clean up the jobs too:<br />
<br />
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Write-Host</span> <span style="color: navy;">-ForegroundColor</span> <span style="color: blueviolet;">Yellow</span> <span style="color: darkred;">"Clearing Up completed jobs"</span></div>
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="color: blue;">Get-Job</span> <span style="color: navy;">-State</span> <span style="color: darkred;">"Completed"</span> <span style="color: darkgrey;">|</span><span style="color: blue;">Remove-Job </span></div>
</div>
<br />
Now you should be able to do a Get-Job and see if there are any failed jobs. Yes yes, that could be automated too..Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-66178771801544573562016-02-16T15:36:00.002+01:002016-02-16T15:39:44.884+01:00SSH via PowershellIn hosting environments, you rarely see only one flavor of operating system, so to be able to manage linux systems with Powershell via SSH is a big help. With Windows Server 2016, SSH connectivity will be <a href="https://redmondmag.com/articles/2015/06/03/openssh-windows-and-powershell.aspx" target="_blank">built in</a>, but until that time you can quite easily get it right now.<br />
<br />
<b>Step 1:</b><br />
Go to <a href="https://sshnet.codeplex.com/">https://sshnet.codeplex.com</a> and download the .Net 4.0 binary from the downloads section and place the "Renci.SshNet.dll" file somewhere where you can access it. In my example I put it in C:\Scripts<br />
<br />
Most likely Chrome will ask you whether it is safe to download, obviously say "Keep":<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXbQ5nIVW4hPBTHMZjp7BwzqAUge6zxOMhr8PLSygk99LRtcMGM1ls132M78AyvhTxnlGB1UithB9hSyktRY88UHS402zCaXY7RZtihdBA_8qB9ifC5PzJXvW5FowIPiN5patJA1-hw7aH/s1600/keepfile.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXbQ5nIVW4hPBTHMZjp7BwzqAUge6zxOMhr8PLSygk99LRtcMGM1ls132M78AyvhTxnlGB1UithB9hSyktRY88UHS402zCaXY7RZtihdBA_8qB9ifC5PzJXvW5FowIPiN5patJA1-hw7aH/s1600/keepfile.jpg" /></a><br />
<br />
<br />
<br />
<br />
<b><br /></b>
<b>Step 2:</b><br />
Go to the file, rightclick and select Properties, then unblock the file.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjES159LqONVjomi79dK8XExkHFWERvwc4i9h1NSaFmpZh63Ch2Yg-KYlLMy5A-G7qmY2DqGl5ZCDBRf31UCzETPOxgo-8C1tWpa0zjZ4xLhWYXR6B7ufKrWTkTw2UkM8bBScAk2ACrw2CG/s1600/unblock.jpg" imageanchor="1"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjES159LqONVjomi79dK8XExkHFWERvwc4i9h1NSaFmpZh63Ch2Yg-KYlLMy5A-G7qmY2DqGl5ZCDBRf31UCzETPOxgo-8C1tWpa0zjZ4xLhWYXR6B7ufKrWTkTw2UkM8bBScAk2ACrw2CG/s320/unblock.jpg" width="235" /></a><br />
<br />
<b>Step 3:</b><br />
Here comes the example code. Quick explanation: First you load the DLL, then you create an object with the DLL file (I'm sure there's a technical programmy way of saying this, but I have no clue), then you suddenly have all these cool features like "Connect" and "RunCommand". It is not an interactive thing (as far as I know), and you only have a single command you can run, so you can't create a scriptblock and run it all at once.<br />
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px;">
<span style="color: darkgreen;"><br /></span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px;">
<span style="color: darkgreen;">#Load the SSHNet Library</span></div>
<div>
<span style="font-family: "lucida console"; font-size: 12px;">[</span><span style="color: teal; font-family: "lucida console"; font-size: 12px;">void</span><span style="font-family: "lucida console"; font-size: 12px;">][</span><span style="color: teal; font-family: "lucida console"; font-size: 12px;">reflection.assembly</span><span style="font-family: "lucida console"; font-size: 12px;">]::</span><span style="font-family: "lucida console"; font-size: 12px;">LoadFrom( (</span><span style="color: blue; font-family: "lucida console"; font-size: 12px;">Resolve-Path</span><span style="font-family: "lucida console"; font-size: 12px;"> </span><span style="color: darkred; font-family: "lucida console"; font-size: 12px;">"C:\Scripts\Renci.SshNet.dll"</span><span style="font-family: "lucida console"; font-size: 12px;">) )</span></div>
<div style="color: black; font-style: normal; font-weight: normal; text-align: left;">
<div style="font-family: 'segoe ui'; font-size: 12px;">
<br /></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;"><span style="color: darkgreen;">#The variables you need to log on</span></span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$server</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"Server01"</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$login</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"someuser"</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$password</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"somepassword"</span></div>
<div style="font-family: 'segoe ui'; font-size: 12px;">
<br /></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: darkgreen;"><br /></span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: darkgreen;">#Call the SSHNet Library as an object</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$SshClient</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: blueviolet;">Renci.SshNet.SshClient</span>(<span style="color: orangered;">$server</span><span style="color: darkgrey;">,</span> <span style="color: purple;">22</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$login</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$password</span>)</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: darkgreen;">#Connect to client using SSH</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$SshClient</span><span style="color: darkgrey;">.</span>Connect()</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: darkblue;">if</span> (<span style="color: orangered;">$SshClient</span><span style="color: darkgrey;">.</span>IsConnected) {</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: blue;">Write-Host</span> <span style="color: darkred;">"Connected to "</span> <span style="color: orangered;">$server</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: darkgreen;">#Run the command on the linux host</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$cmd</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$SshClient</span><span style="color: darkgrey;">.</span>RunCommand(<span style="color: darkred;">'ls -l'</span>)</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: darkblue;">if</span>(<span style="color: orangered;">$cmd</span><span style="color: darkgrey;">.</span>Result <span style="color: darkgrey;">-ne</span> <span style="color: darkred;">''</span>) </div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
{</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: blue;">Write-Host</span> <span style="color: blueviolet;">Output</span> <span style="color: blueviolet;">from</span> <span style="color: orangered;">$server</span></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: blue;">Write-Host</span> <span style="color: orangered;">$cmd</span><span style="color: darkgrey;">.</span>Result</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
}</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
}</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$SshClient</span><span style="color: darkgrey;">.</span>Disconnect()</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<span style="color: orangered;">$SshClient</span><span style="color: darkgrey;">.</span>Dispose()</div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
<br /></div>
<div style="background-color: white; font-family: 'lucida console'; font-size: 12px; margin: 0px;">
</div>
<span style="font-family: inherit;">Which gives the following output:</span><br />
<div style="font-family: 'segoe ui'; font-size: 12px;">
<br /></div>
</div>
<pre class="PoshConsole">Connected to Server01
Output from Server01
total 12
-rw-r--r-- 1 Akos users 136 Feb 21 2008 local.cshrc
-rw-r--r-- 1 Akos users 157 Feb 21 2008 local.login
-rw-r--r-- 1 Akos users 174 Feb 21 2008 local.profile
</pre>
<br />
Of course this is a simple example, but you can load in a CSV file with server, login and password info, and then have a command run on however many servers as you want. It's always fun being able to tell your boss that you're doing that thing on 50 servers, and then getting a cup of tea while your script is doing your work for you. This SSHNet client can also <a href="http://scriptinghell.blogspot.co.uk/2012/07/sshnet-powershell.html" target="_blank">upload</a> files using sftp but I haven't tried that yet.Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-13429035730171534022016-02-16T10:53:00.001+01:002016-02-16T11:01:57.324+01:00Adding PowerShell code in blog posts<span style="font-family: inherit;">For a while I was adding Powershell code in this blog, and it didn't look as neat as all the other blogs I was visiting, so I looked around, and of course I could have been beautifying my blog for a long long time:</span><br />
<span style="font-family: inherit;"><br /></span>
<a href="http://www.lazywinadmin.com/2013/10/blogger-adding-powershell-code-in-your.html?spref=bl" target="_blank"><span style="font-family: inherit;">LazyWinAdmin: Blogger - Adding PowerShell code in your blog post...</span></a><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">I looked at the code that LazyWinAdmin provided, but that did not work as well as the Falchion plugin from <a href="http://blog.falchionconsulting.com/index.php/2012/10/Windows-PowerShell-V3-ISE-Copy-As-HTML-Add-On/" target="_blank">here</a>. </span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">Now I can type something in the ISE, select the code and do "CTRL+Shift+C" and it then runs a Powershell script which generates the HTML code which I can paste into the Blogger site. I may eventually create a Github account and publish Powershell code through that, but this is a little bit simpler for now.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">That left me with the Powershell window output, which LazyWinAdmin gave CSS code for that you can insert into the custom code for the template of your Blogger site:</span><br />
<u><span style="font-family: inherit;"><br /></span></u>
<u><b><span style="font-family: inherit;">CSS Code</span></b></u><br />
<br />
<pre class="brush: css;collapse:false;toolbar:false; ruler: true; first-line: 1;gutter: true;">.PoshConsole {
color: #EEEDF0;
background-color: #012456;
font-family: consolas;
font-size: 0.99em;
padding: .25em;
padding-top: 0.25em;
padding-right: 0.25em;
padding-bottom: 0.25em;
padding-left: 0.25em;
}
</pre>
<br />
Which can then be used with adding <pre class="PoshConsole">Something</pre> in the HTML editor.<br />
<br />
One thing I did notice, is that my black background and gray text did not work so well with all of this beautification, that's why I moved to a more light template.<br />
<br />
I noticed some other CSS style sheets in the sourcecode of the LazyWinAdmin page to make different types of styles, but I will let people hunt around for that themselves. He didn't put that in his blogpost, so I'm not sure if he wants the world to see that ;)Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-45232160765573550622015-06-09T12:55:00.000+02:002016-02-11T16:00:55.865+01:00Rename interfaces based on IP range with Invoke-VMScriptInvoke-VMScript is a lifesaver if you need to do things inside a VM, and you don't want to spend hours logging on to each machine.<br />
<br />
Here I needed to rename multiple network interface labels based on IP range:<br />
<br />
<DIV STYLE="text-align:Left;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:12;color:#000000;"><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="font-size:16;"><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$renamescript</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>=</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN>@'</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>$adapter1 = Get-NetAdapter | Get-NetIPAddress |where IPaddress -Like "10.168.73.*"| Select interfacealias</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>$adapter2 = Get-NetAdapter | Get-NetIPAddress |where IPaddress -Like "192.168.72.*"| Select interfacealias</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#8B0000;"><SPAN>'@</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#0000FF;"><SPAN>Rename-NetAdapter</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#000080;"><SPAN>-Name</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$adapter1</SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>.</SPAN></SPAN><SPAN><SPAN>interfacealias </SPAN></SPAN><SPAN STYLE="color:#000080;"><SPAN>-NewName</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN>"Frontend Network"</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#0000FF;"><SPAN>Rename-NetAdapter</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#000080;"><SPAN>-Name</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$adapter2</SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>.</SPAN></SPAN><SPAN><SPAN>interfacealias </SPAN></SPAN><SPAN STYLE="color:#000080;"><SPAN>-NewName</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN>"Backend Network" </SPAN></SPAN></P></DIV>
<div>
<br />
For each machine you can then do:</div>
<div>
<br /></div>
<div style="color: black; font-family: "segoe ui"; font-size: 12; font-style: normal; font-weight: normal; text-align: left;">
<div style="background-color: white; font-family: "lucida console"; margin: 0 0 0 0;">
<span style="font-size: 16;"> </span><span style="color: blue;">Invoke-VMScript</span> <span style="color: navy;">-VM</span> <span style="color: orangered;">$hostname</span> <span style="color: navy;">-GuestUser</span> <span style="color: blueviolet;">administrator</span> <span style="color: navy;">-GuestPassword</span> <span style="color: blueviolet;">P4ssw0rd</span> <span style="color: navy;">-ScriptText</span> <span style="color: orangered;">$renamescript </span></div>
</div>
<br />
Combine this with the CSV file post I made before, and you can start your script, get a cup of whatever, and have Powershell do the hard work for you.Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0tag:blogger.com,1999:blog-8814736065560944849.post-31617465379508405612015-06-09T12:44:00.001+02:002016-02-11T15:51:00.739+01:00Reading in a CSV file in PowershellCSV files make my life a whole lot easier, especially with large numbers. If you have an excel file with a hostname and an IP address, you get a csvfile like:<br />
<br />
Server,IPAddress<br />
server1,192.168.1.10<br />
server2,192.168.1.11<br />
<br />
<br />
You can import it with the following statement:<br />
<br />
<DIV STYLE="text-align:Left;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:12;color:#000000;"><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="font-size:16;"><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$csvfile</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>=</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#0000FF;"><SPAN>Import-Csv</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8A2BE2;"><SPAN>C:\Book2.csv</SPAN></SPAN></P><P /><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#00008B;"><SPAN>foreach</SPAN></SPAN><SPAN><SPAN> (</SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$value</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#00008B;"><SPAN>in</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$csvfile</SPAN></SPAN><SPAN><SPAN>){</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#FF4500;"><SPAN>$hostname</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>=</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$value</SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>.</SPAN></SPAN><SPAN><SPAN>server</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#FF4500;"><SPAN>$IP</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>=</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$value</SPAN></SPAN><SPAN STYLE="color:#A9A9A9;"><SPAN>.</SPAN></SPAN><SPAN><SPAN>IPAddress</SPAN></SPAN></P><P /><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN STYLE="color:#0000FF;"><SPAN>Write-Host</SPAN></SPAN><SPAN><SPAN> </SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN>"do whatever here to </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$hostname</SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN> with IP address: </SPAN></SPAN><SPAN STYLE="color:#FF4500;"><SPAN>$IP</SPAN></SPAN><SPAN STYLE="color:#8B0000;"><SPAN>"</SPAN></SPAN></P><P STYLE="font-family:Lucida Console;margin:0 0 0 0;background-color:#FFFFFF;"><SPAN><SPAN>} </SPAN></SPAN></P></DIV>Akos Batorfihttp://www.blogger.com/profile/00364624149127262720noreply@blogger.com0