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
    }

Powershell: Set Logon Hours for all the users in an OU


image

With the script in this post you’re able to set logon hours to a bunch of users. All you have to do is to setup logon hours for a “template” user and define this “template” user into the $template variable.

image

The other step is to define the $ou variable with the path to the OU. In my case this was ict-freak/Gebruikers.

The script will now read the default logon hours and will apply them to the users in the OU.

$template = "" # This is a user with the default logon hours
$ou = "" # the full path to your ou "domainname/ouname1/ouname2"

# Get the logonhours from the template user
$template = Get-QADUser $template -IncludedProperties logonhours
[array]$logonHours = $template.DirectoryEntry.logonHours

# Get all users
$users = Get-QADUser -OrganizationalUnit $ou

# Loop through all the users
foreach($user in $users){
    Set-QADUser $user.Name -oa @{logonHours = $logonHours}
}

 

I found this trick here: http://www.powergui.org/thread.jspa?threadID=7860

Windows Update error code 80244019


image

On a fresh installed Windows Server 2008 X64 VM, a received the following error when I want to search for new Windows Updates:

image

The solution in my case was to delete the following registry key: 

HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WindowsUpdate

 

Source: http://social.technet.microsoft.com/Forums/en-US/itprovistasetup/thread/e788027e-9292-47c0-891e-90309cf13367

Powershell: Working with Excel and error HRESULT: 0x80028018


 image

If you want to use Microsoft Excel in your Powershell scripts.

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

 

You can run into the following error:

image

The work around for this issue was changing the Regional Options back to United States International.

image

Microsft created a work around. More info can be found in in: KB320369

guillermooo has created a port to Powershell. Just copy the following code and you should be able to open Excel and create a new workbook.

I didn’t test this script block so let me know if it works 😉

$ci = new-object system.globalization.cultureinfo "en-US"

$e = New-Object -COM "Excel.Application"
$e.Visible = $True
$e.UserControl= $True
$books = $e.Workbooks
$books.PSBase.GetType().InvokeMember( `
       "Add", `
       [system.reflection.bindingflags]::InvokeMethod, `
       $null, $books, $null, $ci)

Source: http://stackoverflow.com/questions/687891/exception-automating-excel-2007-with-powershell-when-calling-workbooks-add

How To: Uninstall Powershell 1.0


image

If you’re trying to install Powershell 2.0 CTP3 on a Windows PC with Powershell 1.0 already installed, the installation wizard will stop with the following warning.

image

Before the wizard will continue, you have to uninstall Powershell 1.0.

Open Control Panel. Select Show updates and browse to Windows XP – Software Updates. Click on Remove to uninstall Powershell 1.0.

UninstallPowerShell1

If you don’t have an entry in the Add/Remove Programs control panel, You can find the Powershell 1.0 Uninstaller here:

  • C:\WINDOWS\$NtUninstallKB926139-v2$\spuninst\spuninst.exe
  • C:\WINDOWS\$NtUninstallKB926139$\spuninst\spuninst.exe

After uninstalling Powershell 1.0 you’re able to install Powershell 2.0 CTP3.

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

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