Powershell: Delete a Folder of a Particular Age


image

I created this script to delete a folder which is older than two days. I needed this for a simple backup solution. I have a Powershell script that will backup a VM which runs in VMware Workstation. This script runs everyday and creates a folder with a date stamp. After a couple weeks there where a lot of copies of the particular VM and the free space of the backup hard disk became very low.

This script will remove all folders in the D:\vmbackup folder which are older then 2 days.

$Now = Get-Date
$Days = "2"
$TargetFolder = "D:\vmbackup"
$LastWrite = $Now.AddDays(-$Days)

$Folders = get-childitem -path $TargetFolder | 
Where {$_.psIsContainer -eq $true} | 
Where {$_.LastWriteTime -le "$LastWrite"} 

    foreach ($Folder in $Folders)
    {
    write-host "Deleting $Folder" -foregroundcolor "Red"
    Remove-Item $Folder -recurse -Confirm:$false
    }

Veeam Backup and Symantec Backup Exec side by side


imageimage

In this post I will show you how easy it is to run Backup Exec Job when Veeam Backup is ready with his job.

First you have to configure your backup job. The only thing different to the “normal” configuration is the Schedule tab. Set the job to Run now. Submit the job and cancel the job.

image

You will see the job under the Job Setup.

image

Now we’re going to test the job. Open a command prompt and run the following command:

“C:\Program Files\Symantec\Backup Exec\bemcmd.exe” -o01 -jJobName_Backup

image

If everything went ok, the Backup job should be running now.

More information about bemcmd.exe can be found here: http://seer.entsupport.symantec.com and in one of my earlier posts: backup-exec-start-a-backup-job-from-the-commandline

Now there is only one thing we need to configure in Veeam Backup. Open the Veeam Backup Job. Go to the Backup Destination window and click on Advanced.

image

Enable the Run the following command check box and enter the bemcmd.exe commando which we used earlier in the command prompt. After that, enable the Run every 1 backup cycle checkbox too.

image

Now you’re ready to go! When the Veeam Backup job is ready, it will start the Backup job of Symante Backup Exec and writes the Veeam Backup file to the tape drive.

PowerCLI: Reading the VMKERNEL logfiles with Powershell v2


image

I was reading Alan Renouf his post about reading the vmkernel log files with PowerCLI. Carter Shanklin posted a comment with the Powershell v2 way of doing this task. I grabbed his one-liner and added a foreach ( % ) loop and the Out-GridView cmdlet. Now with the foreach loop, the one-liner will run against all my ESX Servers.

Get-VMHost |% {Get-Log -VmHost $_ vmkernel |Select -expand Entries } |Out-GridView

The Out-GridView cmdlet will show us all the vmkernel logfiles together in one window.

image

If want to filter on Warning messages only, run the following one-liner:

Get-VMHost | % { Get-Log -VmHost $_ vmkernel | 
Select -expand Entries | Select-String WARNING } | Out-GridView

If there are Warnings in the vmkernel log, the following GridView will be created:

image

If you want to filter the output in the GridView, just use the filter box and if necessary, you can add a criteria:

image

PowerCLI: Get Running VM’s per VMHost and more


image

This post is inspired by a one-liner from Alan Renouf and question from Jason Boche on Twitter. The original one-liner can be found here: http://www.virtu-al.net

The first one-liner in this post will show the Cluster name, ESX host and the total running VM’s on the host.

Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name, ` 

@{N="NumVM";E={($_ |Get-VM | where {$_.PowerState -eq "PoweredOn"}).Count}}

 image

The second one-liner will return the hostname with the least running VM’s on it:

Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name,  
@{N="NumVM";E={($_ |Get-VM | where {$_.PowerState -eq "PoweredOn"}).Count}} `
| Sort-Object NumVM | Select-Object Name -first 1

 

image

The next “two-liner” will return the VMHost with the least running VM’s and set the VMHost into maintenance mode. This can become handy if you want to patch a VMHost:

$ESXHost = Get-VMHost |Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name,  
@{N="NumVM";E={($_ |Get-VM |where {$_.PowerState -eq "PoweredOn"}).Count}}`
|Sort-Object NumVM |Select-Object Name -first 1

Get-VMHost $ESXHost.Name |Set-VMHost -State maintenance

The last “two-liner” will return the VMHost with the least running VM’s, Set the host into maintenance mode and shutdown the VMHost:

$ESXHost = Get-VMHost |Select @{N="Cluster";E={Get-Cluster -VMHost $_}},Name,  
@{N="NumVM";E={($_ |Get-VM |where {$_.PowerState -eq "PoweredOn"}).Count}}`
|Sort-Object NumVM |Select-Object Name -first 1

Get-VMHost $ESXHost.Name |Set-VMHost -State maintenance `
|%{Get-View $_.ID} |%{$_.ShutdownHost_Task($TRUE)}

PowerCLI: Two One-Liners


image 

In this post I will show you two One-Liners that will get you some info of where the VM is placed or running on.

The first one-liner will show all the VMs’ and the ResourcePool where the VM’s are placed.

Get-VM |Select Name, @{N="ResourcePool";E={Get-ResourcePool -VM $_}}

 image

The second One-Liner will show the VM’s and the ESX server where the VM’s are running.

Get-VM |Select Name, @{N="ESX";E={Get-VMHost -VM $_}} 

image

PowerCLI: Set CPU Identification Mask


image

Today I received a question in my mailbox about setting the CPU Identification Mask on Multiple VM’s. The guy who asked the question wanted to set the following values:

image

So I started to search on the VMware Communities and I found the following function.

# Mask SSE 4.1 Extensions to the guest.
function Mask-Extensions($vm) {
    $view = get-view $vm.id

    $vmConfigSpec = new-object VMware.Vim.VirtualMachineConfigSpec
    $featureMask = new-object VMware.Vim.VirtualMachineCpuIdInfoSpec
    $featureMask.info = new-object VMware.Vim.HostCpuIdInfo

    $featureMask.info.ecx = "---- ---- ---- 0--- ---- ---- ---- ----"
    $featureMask.info.level = 1

    $vmConfigSpec.CpuFeatureMask = $featureMask

    $view.ReconfigVM($vmConfigSpec)
}

I changed it a little bit, so it matches the required settings (see picture above):

function Mask-Extensions($vm) {

    $view = get-view $vm.id
    write-host "Setting Mask for: "$vm.Name

    $vmConfigSpec = new-object VMware.Vim.VirtualMachineConfigSpec
    $featureMask = new-object VMware.Vim.VirtualMachineCpuIdInfoSpec
    $featureMask.info = new-object VMware.Vim.HostCpuIdInfo
    $featureMask.info.eax = "-----------------------------000"
    $featureMask.info.level = 0

    $vmConfigSpec.CpuFeatureMask = $featureMask

    $view.ReconfigVM($vmConfigSpec)
}

If you want to run this function against one VM, You can run the following command:

Mask-Extensions (get-vm "My VM")

You can also use this function in the pipeline:

get-vm | % {Mask-Extensions ($_)}

 

Source: http://communities.vmware.com/message/960123#960123

My vCenter Orchestrator Installation Notes


image 

In this post I will share my vCenter Orchestrator Installation Notes.  For the people who doesn’t know what vCenter Orchestrator is, click on the next url: http://www.vmware.com

Before you can install the vCenter Orchestrator service, you have to configure a database first. For this job a created a script. You can use the following lines to create a database on SQL 2005 server:

CREATE DATABASE VCO;
GO
CREATE LOGIN [VCOUSER] WITH PASSWORD=’vmware’, DEFAULT_DATABASE=[VCO], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO

USE VCO
GO
EXEC sp_grantdbaccess [VCOUSER]
GO
EXEC sp_addrolemember ‘db_owner’, [VCOUSER]
GO
EXEC sp_addsrvrolemember [VCOUSER] ’sysadmin’
GO

Joep has posted his installation notes here: http://www.virtuallifestyle.nl so I was curious about the product and wanted to see if I could install it. So with Joep his post as a reference I tried to install the product. I tried to find the vCenter Orchestartor Configuration service. But I could not find it.

After installation, open the Microsoft Services tool to enable the ‘VMware vCenter Orchestrator Configuration’ service. I recommend it to start every time the OS starts up, i.e. ‘Automatic’. Start it manually as well to start working with Orchestrator immediately

So I mounted the vCenter 4 cd-rom and installed the vCenter Orchestrator via:

<cd-rom>\vpx\vmo\vCenterOrchestrator.exe

orchestrator_1

When the installation is finished, open Services.msc and check if the vCenter VMware vCenter Orchestrator Configuration is started. If this is not the case, set the service to start automatic and start the service.  When the vCenter Orchestrator Configuration service is started, Start the vCenter Orchestrator Web Configuration service.

Now it’s time to configure the vCenter Orchestrator. Open a browser and go to http://vco-server:8282. Login with the default credentials: username vmware and password vmware.orchestrator_3

Change the default password. Go to General and press on the tab Change Password:

image

Click on Network to configure network settings. Select the IP Address of your vCenter server.

 image

Click on Database to configure your database. Configure the Database connection settings and press install database. Click on Apply changes.

image

The rest of the setup can you read in Joep his post: http://www.virtuallifestyle.nl

PowerCLI: Set Custom Fields


 image

Last week I posted a script to add Notes to a VM. After posting this script there was a little discussion on Twitter with @vmdoug, @gabvirtualworld if the Notes field is the right field to add this kind of information. So @vmdoug introduced me to the world of Custom Fields.

So I changed my CSV file and added the Field entry. This Field entry will be de name of your custom field. In my case I called it Description.

VMName,Field,Note
DC01,Description,Domain Controller
VC01,Description,vCenter Server

 
The following one-liner will fix this for you:

Import-Csv
"D:\*.csv" | % { Get-VM $_.VMName |
Set-CustomField -Name $_.Field -Value $_.Note -Confirm:$false }

After running the script you will see the Description field with a new entry:

image 

This one-liner will run very slow on a Large environment. So if you have large environment you should definitely check out LucD post on Alan Renouf’s Blog:  http://www.virtu-al.net

PowerCLI: Add Notes To Multiple VM’s


image

My colleague asked me to add some notes to the VM’s so he knows what this VM is doing. So this is a nice job for PowerCLI 🙂 . In this post you will see how powerful PowerCLI /Powershell is.  With only one line of code (one-liner),  you are able to achieve this job.

First you have to create a CSV file with the following entry’s:

VMName,Note
VM1,Domain Controller
VM2,Database Server

The second step is to run this one-liner. The one-liner will add the Notes:

Import-Csv "D:\*.csv" | % { Set-VM $_.VMName -Description $_.Note -Confirm:$false}

When the one-liner is ready, you will find the Notes in the vSphere Client:

image

Note: This script will overwrite the existing Notes, so use it at your own risk!