Interactive Script for Creating VM’s using PowerCLI

Applies to VMware, PowerCLI, Virtual Machines
Modules needed: vmware.powercli

As someone who works in a VMware environment, and likes the command line, PowerCLI is a lifesaver. Are you mad about the phasing-out of vSphere, forcing everyone to use the web-based vCenter manager? PowerCLI is the best way to get around it. I’ve been doing my research making sure I can do everything in PowerShell that I can in vSphere/vCenter. One of the most common tasks being creating virtual machines.

Before we get started, there are a few things you are going to need. First of all, you need a vCenter host. Got it? Great. Next, you need the module PowerCLI. This can be easily installed by typing install-module vmware.powercli.

In my current environment, we are utilizing Templates, Customization Specifications, and Tags.

  • Templates are pretty simple to make, since you can just convert a running VM to one. This is a good place to start.
  • In a future post, I’ll explain how to create Customization Specs using PowerCLI as well. Currently, I’m using OS Customization specs to set the network adapters, and join the machine to the domain.
  • Lastly, we use Tags to set when the VM will be backed up using Veeam.

Since there’s a good amount of code going into this, I’ve commented through the script to show you what’s happening. It goes like this:

Continue reading

Create Formatted xlsx Documents with PowerShell Outputs

Applies to PowerShell, Excel, Reporting
Modules needed: ImportExcel

Very frequently, I use PowerShell to extract data. Generating a list of users with expiring passwords, checking O365 licenses, finding stale Active Directory objects- you name it. Sometimes finding this information is easiest using PowerShell. But, if you need to give this data to your boss, or send it out to a manager, you need to extract it somehow. Export it to CSV? Gross!

Doug Finke has created a beautiful PowerShell module that has made the creation of Excel documents with PowerShell possible. (Without COM objects!) ImportExcel is my new favorite module- and I made it a little bit simpler to use for my needs.

I’ve set this script up as a function, because I want to be able to take what I’m currently working on, and quickly turn it into an Excel document. Here’s the code:

function xl {
  Write-Host
  Write-Host
  $getpath = Read-Host "File Name"
  $path = "c:\temp\" + $getpath + ".xlsx"
  [int]$sheetcount = Read-Host "Number of sheets"
  [int]$count = 0
  while ($count -lt $sheetcount) {
    Write-Host
    $sheetname = Read-Host "Sheet name"
    $command = Read-Host "Command"
    Write-Host
    Write-Host "Include Pivot Table?"
    Write-Host
    Write-Host "1) Yes"
    Write-Host "2) No"
    Write-Host
    $selection = Read-Host "Select"
    switch ($selection) {
      '1' { Write-Host "Working on it..." -ForegroundColor Yellow
        $ex = Invoke-Expression $command | Export-Excel -Path $path -AutoSize -FreezeTopRow -TableName $sheetname.Replace(' ','') -TableStyle Medium2 -WorkSheetname $sheetname -IncludePivotTable }
      '2' { Write-Host "Working on it..." -ForegroundColor Yellow
        $ex = Invoke-Expression $command | Export-Excel -Path $path -AutoSize -FreezeTopRow -TableName $sheetname.Replace(' ','') -TableStyle Medium2 -WorkSheetname $sheetname }
    }
    Write-Host
    Write-Host "Sheet Completed." -ForegroundColor Green
    $count = $count + 1 }

  Write-Host "Exporting to Excel..." -ForegroundColor Yellow
  Invoke-Item $path
}

What it does:

Continue reading

Send an Email with PowerShell

Applies to PowerShell
Modules needed: None

This is a fun little script I set up mostly to see if I could do it. It actually turned out to come in handy when I’m configuring email addresses and new mailboxes in exchange, and want to send a quick test email without changing windows.

The script asks for a recipient, a subject, and a body. It then confirms you want to send the email.

Note: The body is stored in HTML. This means if you want a line break, you need to type <br>in your line, as opposed to hitting ‘Enter’. 

You will want to change the “From”, “SmtpServer”, and “$Sig” lines to meet your needs. For the $sig variable, an HTML file containing your signature is expected.


Clear-Host
Write-Host "Send an Email" -ForegroundColor Yellow
Write-Host
Write-Host
$sig = Get-Content 
$to = Read-Host "To"
$subject = Read-Host "Subject"
$message = Read-Host "Message"
$body = $message + $sig
Write-Host
Write-Host
Write-Host "Message completed. Would you like to send?"
Write-Host
Write-Host "1) Yes"
Write-Host "2) No"
Write-Host
$selection = Read-Host "Select"
switch ($selection) {
'1' { Send-MailMessage -To $to -From  -Subject $subject -SmtpServer  -Body $body -BodyAsHtml
Write-Host "Message Sent." -ForegroundColor Green }
'2' { exit }
}