Powershell script to ping servers


image
I created the following script to check some VM’s at home. This script pings all the servers from the CSV file. If a ping has failed, the script will send an e-mail with the servername and ip-address.

If you want to use this script, you have to change/create the following items:

  • Create a CSV file with your servers and ip-addresses
  • Enter your SMTP Server and e-mail address in the powershell script
  • Schedule the powershell script

 

The CSV file will contain the following information:

ServerName,IpAddress
SERVER1,10.185.1.1
SERVER2,10.185.1.2

 

The Powershell script:

$servers = "D:\scripts\ps\servers.csv"
$csv = Import-CSV $servers 
$smtpServer = ""

foreach($item in $csv){
    $server = $item.ServerName
    $ip = $item.IpAddress

    $ping = new-object System.Net.NetworkInformation.Ping
    $rslt = $ping.send($ip)
        if ($rslt.status.tostring() –eq "Success") {
            write-host ping worked on $server with $ip -ForegroundColor Green}
        else {
            write-host ping failed on $server with $ip -ForegroundColor Red
        
            # Send E-Mail
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    
            $msg.From = "@"
            $msg.To.Add("@")
            $msg.Subject = "Warning: ping failed on $server"
            $msg.Body = "The server $server with ip adres $ip does not reply!"
                
            $smtp.Send($msg)
            }
}
$ping = $null

The source of the ping script: http://theessentialexchange.com/blogs/michael/archive/2007/11/13/checking-server-availability-in-powershell.aspx

Advertisement

Powershell: Check if KB patch is installed


Ik wilde weten of een bepaald KB nummer was geïnstalleerd op mijn VM’s. Dit wilde ik natuurlijk niet met het handje nakijken en heb hiervoor een script bij elkaar geraapt.

Het onderstaande script kun je gebruiken in een VMware VI omgeving.

#Add-PSSnapIn VMware.VimAutomation.Core

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

# Enter the KB#
$HotFixID = (Read-Host "Enter KB#")
Write-Host

# 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 Server*")
  { 
 
# Get all the info using WMI
  $results = get-wmiobject -class "Win32_QuickFixEngineering" -namespace "root\CIMV2" -ComputerName $VM 
  
    # Loop through $results and look for a match then output to screen
    foreach ($objItem in $results)
            {
                # Do the match
                if ($objItem.HotFixID -match $HotFixID)
                    {
                    write-host $objItem.CSName
                    write-host "Hotfix "$HotFixID" installed"
                    write-host
                    }
            } 
   }
}
# Disconnect from vCenter
Disconnect-VIServer -Confirm:$False

 

Zodra je het script start, word er gevraagd naar de vCenter server en het KB nummer wat je wilt nakijken.

image

De volgende output wordt gegenereerd:

image

 

Je kunt het onderstaande script gebruiken in een omgeving zonder VI3:

# Get content
$computers = get-content c:\computers.txt

# Get all the info using WMI
$results = get-wmiobject -class “Win32_QuickFixEngineering” -namespace “root\CIMV2″ -computername $computers

# Loop through $results and look for a match then output to screen
foreach ($objItem in $results)
{
    if ($objItem.HotFixID -match “KB932168″)
    {
        write-host $objItem.CSName
        write-host “Hotfix KB932168 installed”
        write-host
    }
}

 

Source: http://techittome.wordpress.com/2007/04/12/windows-powershell-script-to-check-for-specific-hotfix/

Script: VI Toolkit Healthcheck


Ivo Beerens just released his second version of his Healthcheck powershell script.

Version 2.0 of the Healthcheck script is released. The script reports the following:

– VMware ESX server Hardware and version         
– VMware vCenter version    
– Cluster information
– VMware statistics
– Active Snapshots    
– CDROMs connected to VMs   
– Floppy drives connected to VMs  
– Datastores Information such as free space
– RDM information 
– VM information such as VMware tools version,  processor and memory limits     
– VM’s and there datastore
– VMware timesync enabled  
– Percentage disk space used inside the VM
– VC error logs last 5 days

It is possible to schedule this script and output the content to HTML and e-mail it. 

image 

You can download the script over here: http://www.ivobeerens.nl/?p=256

Script: Defrag all Local VMDK’s.


powerShellIcon

De volgende function zoekt eerst alle *.VMX bestanden op en vervangt daarna de extentie *.VMX naar *.VMDK. Deze *.VMDK bestanden worden in de $List geplaatst. Daarna worden alle VMDK’s gedefragmenteerd via de vmware-vdiskmanager.exe.

function Defrag-allVMDKs{
param([string]$path)
$vdiskmanager = “C:\Program Files\VMware\VMware Workstation\vmware-vdiskmanager.exe”
$parameter = “-d”
$List = get-childitem $path -recurse | where {$_.extension -eq “.vmx”} |
foreach-object -process { $_.FullName } | ForEach-Object {$_ -replace “.vmx”, “.vmdk”}
ForEach($vmdk in $List)
{
echo $vmdk
& $vdiskmanager $parameter $vmdk
}
}

Via het volgende commando kun de bovenstaande function gebruiken.

Defrag-allVMDKs <drive>\<path>

Als je dit in de Shell uitvoerd ziet het er als volgt uit:

image

How To: Schedule a Powershell script with Scheduled Task


 

In deze post lees je hoe je een Powershell script kunt schedulen via Windows Scheduled Tasks.

Start de Scheduled Task Wizard.

image

Klik op Browse… en voeg het onderstaande commando in:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

image

Nadat de wizard klaar is, vink je Open advanced properties for this task when I click Finish aan. Daarna voeg je het volgende toe aan het Run veld: -command D:\Scripts\backupwsvm.ps1

image

Vervolgens druk je op OK om de task op te slaan.

Mocht je een Powershell script scheduled die gebruik maakt van de VI Toolkit. Dan moet je de onderstaande code toevoegen aan je script.

#ADD VMWARE PSSNAPIN
Add-PSSnapin -Name “VMware.VimAutomation.Core”

#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
[Reflection.Assembly]::LoadWithPartialName(“vmware.vim”)

Citrix: CPSUpdate script


Thomas Koetzing van http://www.thomaskoetzing.de heeft zijn CPSUpdate script voorzien van een update.

I have now extended the update script to automatically download ALL currently available Hotfixes/Rollups for the xenApp (Presentation Server) I have set. This is done with the Windows version of the unix command "wget". Wget also supports proxy settings and can be set in the script as well.

Right now xenApp 5.x is not supported because the is no fix available and I have to check what Citrix is doing there…This is the 1.8 release with the download option, so let me know of any bug etc.
Please leave your comment at the bottom of the article how to patch Presentation Server.

Download het script hier: CPSUpdate-v1.8.zip

Script: Powershell script for changing the AD Home Dir and Drive


image 

Stel je bent midden in een migratie naar een andere fileserver of naar bijvoorbeeld een NetApp filer. Daarnaast wil je ook alle Home directory’s migreren. Dit houdt in dat je ook de Active Directory moet aanpassen.  Dit kun je op verschillende manieren doen. Ik heb gekozen om eens te kijken naar de Quest ActiveRoles addin voor Powershell en dat bleek behoorlijk krachtig te zij, zoals je kunt zien in het onderstaande scriptje:

image

Voordat je dit script kunt gebruiken heb je een PC nodig die lid is van het domain, PowerShell en de Quest AD cmd-lets geïnstalleerd heeft. Daarna kun je het script als volgt uitvoeren:

PS Scriptdir> .\Change_Home_Dir_drive.ps1

image

Daarna kun je in de Active Directory nakijken of het script succesvol heeft gelopen.

image

Script: Removehba.sh updated


image

Bouke Groenescheij van www.jume.nl heeft een update van zijn removehba script online gezet. Met dit script kun je in een handomdraai op een linux distro een ESX cd zo aanpassen dat je tijdens de installatie van een ESX server de HBA’s niet hoeft los te koppelen.

"So I went ahead and made some changes to the script which pulls apart the extra files, edits them and repacks it all up in one go…

Thanks to both of you for your work. I’ve been using my updated install media now for http deployments for a while without any issues. Its a great install process not having to worry about the san being disconnected. using the nework boot iso and a kickstart file, I can have a host rebuilt in under 15mins. That includes a reboot and configuring all my networks, hp agents & tweaks etc."

De download vind je hier: http://www.jume.nl

Excellent (VI) Powershell Scripts


Hugo Peeters van http://www.peetersonline.nl heeft denk ik een van de beste blogs over VMware en Powershell.

Hier een paar voorbeelden van een aantal van zijn scripts:

Voor meer scripts en zeer nuttige powershell informatie verwijs ik naar zijn blog: http://www.peetersonline.nl

Hugo, ga gerust zo door 😉