Monday, June 27, 2011

Count the Documents and the Words in the Documents in a Directory

#Clear screen
cls

$intDocs = $intWords = $null
$path = "C:\Users\owner\Documents\Harlequin\Count"
$application = New-Object -ComObject word.application
$application.visible = $false
Get-ChildItem -Path $path -include *.doc,*.docx -Recurse |
ForEach-Object {
"Processing $($_.fullname)"
$document = $application.documents.open($_.fullname)
$intWords += $document.words.count
$intDocs ++
$document.close() | out-null
}
$application.quit()
$application = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
"There are $intDocs documents in the $path folder. They contain a total of $intWords words."

Tuesday, June 21, 2011

VMs with duplicate MACs

We had quite a bit of trouble with vms that had duplicate MAC addresses in our environment. VMware said it should happen rarely but that hasn't been the case for us. So I wrote this handy script to find them for us.

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#where $vCenters are the names of your vCenters
$vCenters = 'vCenter1','vCenter2'
foreach ($vCenter in $vCenters)
{
#connect to vCenter
Connect-VIServer $vCenter 
$mac = @{}
foreach ($vm in Get-VM)
    {
    Get-NetworkAdapter -VM $vm | foreach
        {
         $net = $_.networkname
         if($mac.ContainsKey($_.MacAddress) -and $vm.Name -notlike $mac[$_.MacAddress])
           {
           if ($net -notlike $_.networkname)
            {
            Write-Host "Duplicate MAC address" $_.MacAddress "in" $vm.Name "and" $mac[$_.MacAddress]
   $output = "Duplicate MAC address $_.MacAddress in $vm.Name and $mac[$_.MacAddress]"
   $output | Out-File c:\dupmac.txt -Append
        }else{
            $mac[$_.MacAddress] = $vm.Name
            }
        }
    }
}
Disconnect-VIServer -Server $vCenter -Confirm:$false
}

Friday, June 17, 2011

Witty Banter and EMC cmdlets

I should have warned you that unlike the "Hey Scripting Guy" guys, I don't really plan on providing witty banter or even any real instruction on the scripts that I post. They are yours for the taking to muddle through as best you can.

I also wanted to mention that the EMC cmdlets are out. https://community.emc.com/docs/DOC-8561
They are very cool for helping to manage your EMC storage. Look for scripts to come.

Power Off All VMs in a cluster

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#Connect to the vCenter where 'vCenter1' is the name of your vCenter
Connect-VIServer vCenter1 

#Get all VMs in the a cluster resource Pool
$vms = Get-VM
Foreach ($vm in $vms)

      #Power Down the Test VM Guests 
       $vm | Shutdown-VMGuest -Confirm:$false

Disconnect-VIServer vCenter1 -Confirm:$false

Wednesday, June 15, 2011

Add vLAN to ESX host

#clear screen
cls
#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
#add VMware snapin
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
#vCenter is the fqdn of your vCenter
Connect-VIServer -Server vCenter 
#Cluster1 is the name of your cluster
foreach ($ESXhost in get-cluster Cluster1 | get-vmhost)
{
  #VSWITCH1 is the name of your virtual swtich
  get-virtualswitch -vmhost $ESXhost -name VSWITCH1 | new-virtualportgroup -name vLAN_201 -vlanid 201
}

Tuesday, June 14, 2011

Count VMs

#clear screen
cls
#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
#add VMware snapin
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
#allow connection to mulitple vCenters
$set = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -confirm:$false
$set Out-Null #don't write output
foreach ($vCenter in 'vCenter1','vCenter2','vCenter3')
{
$connect = Connect-VIServer -Server $vCenter #connect to each vCenter
$connect Out-Null #don't write output
$vms = Get-VM
}
write-host "The total number of VMs in the environment is" $vms.count

The Scripting Gal

Hey everyone,

Most of you scripters are probably familiar with The Scripting Guys over at Microsoft. Well, I won't compare myself to them in the slightest, but I am going to be regularly posting some Powershell scripts that I've found useful. Hope you'll like them, too.

yours truly,

The SG