PowerCli wrapper for SSH actions

There is a “new” issue in town and this is a story about three colleagues helping us to tackle it.

tldr; go to the bullet points

ESXi installed on SD Cards can fail to reboot as the SD Medium wears silently out.

My colleague John Nicholson posted about it in March Using SD cards for embedded ESXi and vSAN?

The main problem with this issue is that the SD Cards are silently failing. Well, they are not when you know what to look out for, there are some lines in the log files to give this issue away but who reads them? You could probably set something up in LogInsight but that is for another day.

In Johns article, he referenced a nice script from an other colleague of mine William Lam. William created a handy ash script which you can run on the ESXi host to check if the SD card returns the same information several times from the boot sector area.

I took Williams script and put a PowerCLI wrapper around it (which utilizes Plink.Exe) because I needed to check on 180 servers. During a Workshop, I talked about that with an other colleague and he mentioned to me that I should share it with the community. So here it is, I hope you find it useful.

The script…

There are several things about this script you should know:

  1. You can find it here: CheckSDCard Script on Bitbucket
  2. It has some standard functions which I use when I write PowerShell scripts so you could make it slimmer when you remove those and rewrite it a bit
  3. You can choose either one ESXi or the whole Cluster to check for
  4. It requires the root password (either on the command line, per prompt or hardcoded in the script…. DON’T use that option 🙂 )
  5. If ssh is disabled on the server it will enable ssh first, do it’s magic and will disable ssh afterward again
  6. Oh, you need to be connected to the vCenter Server before running the script.

Looking a little bit closer at the script:

Line 60 – 69:
Will check if Plink.exe is in the directory of the script and exit if it is not.

Line 71 – 95:
Will determine based on the ParameterSetName if you want to check a host or a cluster. If it is a cluster the script will get all the ESXi in the cluster and then run the function checkSDCard.

Line 97 – 101:
Will Export the result as a CSV File. The filename is based on the ESXi/Cluster and a timestamp.

Line 102 – 125:
Will only execute this part of the script if the ESXi host is at least Version 6.x. This could be removed.

Line 120 – 123:
Almost the real thing. This Part of the code reads the SDCardCheck.txt file. This file is almost the same as the SDCardCheck_debug.txt file. The Debug Version contains the original script from William. The Non-Debug Version has every output remarked out except the important part which detects if the SD card is corrupted or not.

Line 151 – 208:
This function is almost 100% my default Invoke-SSHCommand.
It will check if SSH is enabled, if not it will enable SSH and afterward disable it again. It will then execute the command via plink and write the output of that command into $output. I also check if authentication is possible and will exit if it is not.

The only change can be found in

Line 192 – 193:
Here a new $row object is created with ESXi Name and the result of the stripped down result of the SDCardCheck script. Then the $row is added to the SDCardList which is at the end of the script exported into the CSV file.

Bonus Line 324 – 357:
This is a Update-Check function I wrote to check if the local version is different to the version in your git repository. If there is a difference a backup is created and the new version downloaded. After that, you need to rerun the script.

I hope you find this script and the functions in it useful.

 

 

Setting Syslog Configuration via Get-EsxCli -v2

Hi,

after battleing a little with Get-EsxiCli -v2 in the last couple of month I needed to write a quick script yesterday to reconfigure the Syslog Parameters on all Esxi Hosts.

A problem when you are coming from the old Get-EsxiCli is how to map the old command line to the new version. Luckily for us we have a helper:

$esxcli.system.syslog.config.set

will display a list of all functions you can use and

$esxcli.system.syslog.config.set.CreateArgs()

will return the hashtable values you can use to create the

$esxcli.system.syslog.config.set.invoke() 

syntax, for example

$esxcli.system.syslog.config.set.invoke(@{
defaultrotate = 8
loghost = 'udp://syslogServer:514'
})

Complete Source Code can be found here: syslogConfig.ps1

Powershell and UTF-8

Hi,

long time to updates so I thought I put up a workaround for UTF-8 issues with textfiles and powershell up.

As the German Language has umlauts… äöü … and several other languages too, people will stumble again and again on issues with how to handle those.

This is a problem in a lot of programming languages especially if you have different OS and language versions / settings.

I had the issue that even when a file was saved in UTF-8 the powershell script wouldn’t process the file correctly.

To make matters worse, the file was created in powershell 😦

So the solution I found was quite easy:

$rand = Get-Random
get-content $csv | out-file -encoding utf8 $fileLocation\temp-$rand.csv
$stuff = Import-Csv $fileLocation\temp-$rand.csv -UseCulture -Encoding UTF8
Remove-Item $fileLocation\temp-$rand.csv

This will make UTF-8 work for me.

BTW: The original file was created by:

Export-Csv “somefilename” -NoTypeInformation -UseCulture -Encoding UTF8

WebCommander is Open Source now.

WebCommander is a framework to wrap around your Powershell and / or PowerCLI scripts to give it an easy to use web interface.

Wrapping a program as a web service means using a web portal to gather parameters and then passing them to the wrapped program. By doing so, details on how the program is developed and what underlying system it depends on become transparent to others. People could make easy use of the program via browser manually or through any programming language that supports calling web services.

End users usually run many repeatable routines which could be automated by PowerCLI. WebCommander makes it even easier by providing a more user friendly UI and result report. Users have no need to setup their own environment to use PowerCLI, keep upgrading it, download and learn scripts written by others.

Started as a VMware fling this is now OpenSource and I guess usefull not only to people using PowerCLI.

https://github.com/vmware/webcommander

 

Get-ChildItem -Recursive with a limit

Today I had the need to use Get-ChildItem in recursive mode, but I also needed to be able to stop the recursion at a specific level.
Unfortunately the Get-ChildItem doesn’t have the capability to do this.
Luckily there are others around which had the same problem already and found a solution:
function Get-ChildItemToDepth {
  param(
    [String]$Path = $PWD,
    [String]$Filter = "*",
    [Byte]$ToDepth = 255,
    [Byte]$CurrentDepth = 0,
    [Switch]$DebugMode
  )
 
  $CurrentDepth++
  if ($DebugMode) { $DebugPreference = "Continue" }
 
  Get-ChildItem $Path | ForEach-Object {
    $_ | Where-Object { $_.Name -like $Filter }
 
    if ($_.PsIsContainer) {
      if ($CurrentDepth -le $ToDepth) {
        # Callback to this function
        Get-ChildItemToDepth -Path $_.FullName -Filter $Filter -ToDepth $ToDepth -CurrentDepth $CurrentDepth
      } else {
        Write-Debug $("Skipping GCI for Folder: $($_.FullName) " +
          "(Why: Current depth $CurrentDepth vs limit depth $ToDepth)")
      }
    }
  }
}