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

Copy User’s Groups to Another User

Applies to Active Directory, User Management
Modules needed: ActiveDirectory

This is a very useful script if you find yourself needing to add a user to all the groups another user is a part of. Typically, you could just copy the user in Active Directory to make the new user. But in this case, I was asked to do this for an existing user. There’s no simple way to do this within Active Directory, but this quick script gets it done.

The script asks for the user needing to be added, and the user to copy the groups from.

Import-Module ActiveDirectory
Clear-Host
$creds = (Get-Credential)
Clear-Host
$addedUser = Read-Host ("User to add to groups")
$copiedUser = Read-Host ("User to copy groups from")
Write-Host
Write-Host
Write-Host "Working on it..." -ForegroundColor Yellow
Write-Host
Write-Host
Get-ADUser -identity $copiedUser -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $addedUser -Credential $creds -Passthru | Format-Table -Property Name,GroupCategory,GroupScope,DistinguishedName -AutoSize

At the end of the script, the groups the user was added to are shown.

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 }
}

 

Add User to Distribution Group

Applies to Exchange, and O365.
Modules needed: Exchange, or Microsoft.Exchange.Management.ExoPowershellModule

This is a simple script that just asks for input on adding members to a distribution group. The reason I made this script was because I wanted to simplify this process for non-admin IT folks, and enter multiple users at a time.

To add multiple users, separate them with commas.
Example: Members: druggeri,tdanza,pporter


$dl = Read-Host "Distribution Group"
Write-Host
get-distributiongroup -identity $dl
Write-Host
Write-Host
$members = Read-Host "Members"
$members = $members.split(',')
foreach ($m in $members) {
  Add-DistributionGroupMember -identity $dl -member $m }
Write-Host
Write-Host "Confirmation:" -ForegroundColor Green
get-distributiongroupmember -identity $dl | Format-Table -AutoSize

At the end of the script, a confirmation is shown using get-distributiongroupmember on the entered group. This is just so you can verify your change applied, and that the correct users were added.