Posh: Run gpupdate on Multiple Computers


image

I created a Powershell script that will get all the computers from a certain OU and run GPUpdate /force on these machines. This script uses the Quest Active Directory cmdlets and PsExec.exe. You need to install the Quest cmdlets first. If PsExec doesn’t exist, the script will download it to the c:\tools directory.

When you start the script, you have to enter the FQDN and the OU name:

image

The script creates a text file with al the computer names in it. This text file will be used with the PsExec.exe command.

image

You can find the script on poshcode.org

Advertisement

Restart VMware Tools on all Windows VM’s


 image

After reading the post on http://www.virtualvcp.com/content/view/82/1/ about the VMware Tools status “not running” and in particular the part about the preferred work around:

I find that restarting the VMware Tools Service in the guest OS always gets by the problem, but loggin into every single VM that reports the wrong status for it’s VMware Tools could be a bit of a drag. So I choose to do this remotely rather that logging on to each VM.

From any Windows workstation/server, open a command pompt and run:

sc \\{vm-name-or-ip-address} stop "VMTools"
sc \\{vm-name-or-ip-address} start "VMTools"

I thought that can be done via Powershell and the VI Toolkit. So I created the following script that will restart the VMware Tools service on every running Windows VM.

$vCenter = Read-Host "Enter the vCenter servername"

Connect-VIServer $vCenter

$Service = "VMtools"
$VMs = Get-VM | Where-Object {
        $_.PowerState -eq "PoweredON" `
        -and `
        $_.Guest.OSFullName -match "Windows"
    }
    
foreach($VM in $VMs)
{
    Write-Host "-------------------------------------------"
    Write-Host "Restarting the VMware Tools Service on" $VM
        $Svc = Get-WmiObject -Computer $VM win32_service `
        -filter "name='$Service'"
            $Result = $Svc.StopService()
            sleep 5
            $Result = $Svc.StartService()
    Write-Host "Done.. "
    Write-Host "-------------------------------------------"
}

Disconnect-VIServer -Confirm:$false

This script generates the following output:

image

Other useful blog posts or kb articles on this subject:

Source for the restart service part in my script: http://blog.geekpoet.net/2008/10/manipulating-remote-services-with.html

Powershell: Get-SubFolderSize


image 

On every home directory will be a  folder called Files. I was curious how big these folders are. So this is why I created the following Powershell script:

function Get-Foldersize{
  param([string]$StartFolder,
        [string]$SubFolder)
  
  $Dirs = Get-ChildItem -path $StartFolder\* -Recurse -Include $SubFolder 
    
  foreach($Dir in $Dirs){
  $objFSO = New-Object -com  Scripting.FileSystemObject
  "$Dir = " + "{0:N2}" -f (($objFSO.GetFolder("$Dir").Size) / 1MB) + " MB" 
  }
}

Get-FolderSize C:\Users Files

 

The output of the script will look like this:

image

Here are some pictures (as hard evidence 😉 ) of the folder properties:

image

Source: http://www.microsoft.com/technet/scriptcenter/

Beta: Vizioncore Virtualization EcoShell


image

Vizioncore released the beta version of the Virtualization EcoShell.

The goal of the Vizioncore Virtualization EcoShell is to provide a freeware desktop application for novice and expert IT administrators leveraging Windows PowerShell scripts across their multi-platform virtual environments.   

Fostered and supported by The Virtualization EcoShell Initiative (VESI) – an online community-driven Web site sponsored by Vizioncore – the Virtualization EcoShell is enhanced by the participation of community members through the exchange of new ideas, value-add services and extendable scripts. To become a member of the VESI community, please visit VESI Registration.

 

One of the best features is based on a script from Alan Renouf (vdiagram) and it’s called Generate vDiagram.

image

With this feature/script you can generate a Visio drawing of your VI environment. You can choose the following options:

image

If all the options are set to true, the script will generate three workspaces in Visio with three different vDiagrams. The diagram will look like this:

image

If you are managing a VI environment, this will be a must have tool. So take a look and get your download here: http://www.thevesi.org/downloads.jspa

Powershell: Add User to a lot of Groups


image 

Update: Dimitry Sotnikov has posted a great article about the new cmdlets. You can read his post here.  After reading Dimitry’s post, I created a new script.

For some test purposes I created a new test user. This test user has to be a member off al my application groups in Active Directory. This job can easily  be done with Powershell and the Quest AD cmdlets.

 
$User = Read-Host ("Username")
Get-QADGroup -Name 'GG_APL_*' -NotContainsIndirectMember $User `
    | Add-QADGroupMember -Member $User

 

To verify the changes, you can run the following script:

$User = Read-Host ("Username")
Get-QADGroup $User | Select Name 

Remove files older than x days with PoSH


image

One of the Symantec Live Update Servers started to complain about low disk space. So I started Treesize Pro to see what files or folders where eating my disk space.  After a couple of seconds Treesize Pro showed me that the liveupdate files, in the c:\LiveUpdate folder where the problem.

I created the following Powershell Script (after reading the source) and scheduled this script to run once a week.

Warning: This script will remove everything that matches the remove-item cmdlet!



$Now = Get-Date
$Days = "3"
$TargetFolder = "C:\LiveUpdate"
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -include *.zip, *.x86 -recurse `
    | Where {$_.LastWriteTime -le "$LastWrite"} 

foreach ($File in $Files)
{write-host "Deleting File $File" -foregroundcolor "Red"; `
    Remove-Item $File | out-null}

Just change the $Days, $TargetFolder variables and the file extensions (*.zip, *.x86) for you environment.
Source of this script: powershell-script-to-delete-files-older-than-certain-days.aspx

VMware: Start the VI Toolkit on Win 7


After installing Windows 7 build 7057 x64 the VI Toolkit starts with the following error /warning. If  you run the Set-ExecutionPolicy unrestricted  command as a normal user, the command will not run successfully.

image

The solution is simple: Open the VI Toolkit with the Run as administrator permissions option.

image

When the VI Toolkit window pop’s up. Run the Set-ExecutionPolicy unrestricted command, and the VI Toolkit starts without the error.

image

Powershell: Check if partition is aligned or not


This script has the following Requirements:

  • VI Toolkit
  • Powershell
  • WMI query
  • Windows VM’s

The following script get al lists of all Windows VM’s which are powered on. The next step is a WMI query which queries the Win32_DiskPartition class. The final step is a match with the $StartingOffset variable.

Add-PSSnapIn VMware.VimAutomation.Core

# Connect to vCenter
$VC = Connect-VIServer (Read-Host "Enter vCenter server")

$StartingOffset = "65536"

# Get all VM’s with powerstate = PoweredOn
$VMS = Get-VM | Where {$_.PowerState -eq "PoweredOn"} |Sort Name
ForEach ($VM in $VMS)
    {
  
# Process only Windows Server VM’s
   if ($VM.Guest.OSFullName -match "Microsoft Windows*")
        {  
      
# Do a WMI Query
       $results = get-wmiobject -class "Win32_DiskPartition" -namespace "root\CIMV2" -ComputerName $VM

           foreach ($objItem in $results)
                {
              
# Do the match
               if ($objItem.StartingOffset -match $StartingOffset){
                  
write-host $objItem.SystemName
                  
write-host $objItem.Name
                  
write-host "Partition aligned" -foregroundcolor green
                  
write-host}
              
else{
                  
write-host $objItem.SystemName
                  
write-host $objItem.Name
                  
write-host "Partition NOT aligned" -foregroundcolor red
                  
write-host                  
                    }
                }
        }
    }
# Disconnect from vCenter
Disconnect-VIServer -Confirm:$False

after running the script, the following output will be generated:

image

Add Custom Fields to VI Client with Powershell (Samples)


Hugo Peeters heeft zijn Powershell scripts voor het vullen van Custom Fields in vCenter gepost:

Snapshot Count

I already showed you how to do this, but I have now added an IF-statement so that only changes are updated (equal values are not overwritten). And I have added Julian Wood’s correction. Add-VMSnapshotCount.ps1

Total Snapshot Size

The number of snapshots is quite inetresting, but even more interesting, is the total size of the delta files all snapshots are occupying. They might be eating up all your precious SAN space. Plus, reverting to or committing a large snapshot is tricky. Add-VMSnapshotSize.ps1

Host Hardware Model

Want to see what models of hardware you are using in your datacenter? You could look at the summary tab of each host. Or run this script to add the info to the every Hosts tab in the VI Client. Select your Datacenter, select the Hosts tab and enjoy! Add-VMHostModel.ps1

Host ESX Version

Did you update all your ESX Servers to the latest version? Check it quickly using this script. Add-VMHostVersion.ps1

Host LUN Count

Last but certainly not least: are you sure every datastore you are using is available to all your ESX Servers? It is visible at a glance when you add the LUN Count to your VI Client! Add-VMHostLUNCount.ps1

 

Check zijn site voor allerlei top Powershell scripts: http://www.peetersonline.nl/

 

Bron: http://www.peetersonline.nl/index.php/vmware/add-custom-fields-to-vi-client-with-powershell-samples/