Category Archives: PowerShell

Workstation & Server Audit Script V3

One thing I wish I had was more time to re-visit my old scripts and re-write them as I often look at my old script and it sends a cold shiver down my spine as I now know a better way of doing things.

As I was away for the weekend and had no internet connection I took my old Audit Script with me, I had a few personal objectives that I wanted to achieve by re-writing this code and I was also asked by a friend who has started to adapt the code into other formats.

So, this is version 3 of the script, you can see how much I have learnt recently as V1 of this script was over 1000 lines, V2 of this script was 847 and now with the new improved code it is a miniscule 459 lines šŸ™‚

My main objectives for this version of the script were:

  1. Apply my newly learnt PS Skills to optimise the code
  2. Use a HTML format that worked in all browsers
  3. Make the code easy to follow
  4. Make the code easy for other people to produce similar reports of other systems

When you see the code and use the output I hope you will agree I have achieved each of these.Ā  The code now works with all browsers I have tested and is very easily customisable.

Check out a screenshot of the output below: Continue reading →

PowerCLI: Mass VM Portgroup Change

I was asked in my Script List if it was possible to change Multiple VMs to attach to a new PortGroup, this task is easy enough so long as you remember that every VM can have up to 4 virtual nics in VI3 and up to 10 in vSphere.

So to change all virtual machines we would do the following:

$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"

Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

Or to change all of the VMs for a particular cluster we could use:

$Cluster = "Non-Production"
$OldNetwork = "PG1 192.168.0"
$NewNetwork = "PG2 10.1.1"

Get-Cluster $Cluster |Get-VM |Get-NetworkAdapter |Where {$_.NetworkName -eq $OldNetwork } |Set-NetworkAdapter -NetworkName $NewNetwork -Confirm:$false

Note: By default the Set-NetworkAdapter cmdlet would prompt you for each change it was going to make, this is overwritten by using the –Confirm:$false parameter.

Continue reading →

The VESI 1.2 – Get it now

People who read my blog know how much I like the Virtualisation EcoShell, I love the way it adds the power of PowerCLI back into the GUI to enable you to create customised GUI’s all of your own enhancing the true power of PowerShell and allowing people who do not know how to code in PowerCLI (yet) the chance to harness the power.

Version 1.2Ā  of the VESI has now been released and it has some great new features:

Continue reading →

PowerCLI: Host Network Config

Whilst on a recent engagement with a customer we were looking at best practices and trying to quickly see how each PortGroup was configured, did all hosts have the same nics assigned to the same vSwitches, were all Portgroups setup the same to use the correct active and standby adapters ?

Have you ever tried doing this from the GUI ? Believe me it will take about 10 minutes to manually copy the relevant information from each host and if you have multiple PortGroups and multiple vSwitches then you can see how long this will take to check against best practice !

I think you know what is coming….

This came up again when someone asked me how to achieve exactly the same thing so I bumped it up my list of scripts I need to write.

Check out the below PowerCLI script, it lists all the needed information to perform this best practice analysis as you can see from the example output:

image

Continue reading →

PowerGUI 1.9.5 Released

As the readers of this blog probably know I am a big fan of PowerGUI and the VESI, not just for the great GUI console that enables fantastic powershell goodness in the form of a nice user interface but also for the fully functional script editor.

The one thing that amazes me about these applications is that they are FREE, yes that’s right, FREE, not free for x number of days or free to use 50% of the features but 100% free.

Another thing that also amazes me is the amount of work they put into this free app, i mean most companies release the free apps and then no further work or support is added to them, have you ever had a problem with PowerGUI ? If you have then you will have no doubt raised it in the PowerGUI forum and had it answered in a day at the latest !

Add to this the fact that Quest actually listen to what the people who use this want and it amazes me constantly.

Take for example my situation, in the previous versions of PowerGUI there was no GUI support for write-progress (a nice cmdlet that gives you a progress bar style popup), sometimes I had to second guess my scripts and what they were doing unless i ran them in the main PowerShell window, and if I did that I would get the write-progress output so at the recent UK PowerShell user group I hounded both Dmitry and Darin from Quest and told them I wanted a GUI version of the write-progress output, now if they got fed up of the constant emails or if they are just nice chaps (I think the later) they added this to the latest build in both the console and the script editor, proving that they do indeed listen.

So, head over to the PowerGUI site and download version 1.9.5 and try out the latest and greatest PowerGUI with added Write-Progress.

Continue reading →

HA Slot Size Information

Do you have HA enabled cluster ? If you do then you will need to know what a slot size is, that’s if you don’t already, if you do already then just cut straight to the cool PowerCLI example….

I’m not going to explain this completely as I could never write such an excellent explanation as Duncan over at Yellow bricks, I highly recommend reading this post as it is pure excellence.

So what is a slot size ?

If you have a HA enabled cluster and have set the admission control to ā€˜Prevent VMs from being powered on if they violate availability constraints’, this means when your cluster does not have enough room to start VMs if a host fails then you will violate the constraints of the cluster and vCenter will not allow you to power on your next VM, but how does it know when you do not have enough room for another VM to power on ?

Continue reading →

PowerCLI: One-Liners Checking Host Network Information

Sometimes as a consultant when performing an initial look or a healthcheck on a new system you will want to confirm all hosts have the correct network information, if there is a mis-configured vmotion portgroup or something similar then this could impact HA or if there is an incorrect DNS server then this may also have issues further down the line with your service console.

The following quick one-liners display some vital information for each host, errors can be seen at a glance, rather than checking each host individually, for us to gain this information we use the wonderful Get-VMHostNetwork cmdlet and expand a few areas of interest:

  • Remember these can be narrowed down further by adding the Get-Cluster ā€œMyClusterā€ | to the start of the line.
  • Also remember this information can also be exported to a csv by adding | Export-csv ā€œC:\info.csvā€ to the end of the line.

To list all ESX Hosts and their Service console information: Continue reading →

vTip – A VMware Expert updating your VI

We have all seen the message of the day setting which allows you to alter what you would like to tell your colleagues today and other than the normal fun type messages I have never seen it put to good use.Ā  (Yes I know this is used to tell people that a host is being managed by a VI).

So you could stick with your normal messages….

Fake Message

Or you could put it to use, how would you like a vExpert, no a vGuru, no a vGod to update your Message of the Day, giving you tips and help each day and helping you along the path to virtual enlightenment ?Ā  Well now you can.

Jason Boche has recently announced his vCalendar which is a great daily calendar with tip for each day, there is also a blog widget and netvibes or Google widget for this too, so my script takes these wonderful daily tips and adds them to a place we all visit on a daily basis…. The Virtual Infrastructure Client.

Now you can be taught a daily message by the vMan himself.

Message of the Day

The script should work in most cases apart from those companies that have complex proxy servers. – Hope it works for you !

Don’t forget to set it up as a scheduled task to run at 00:01 every night and update your VI with the next vTip.

Function vTip {
    $proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
    $proxy = new-object System.Net.WebProxy
    $proxy.Address = $proxyAddr
    $proxy.useDefaultCredentials = $true

    $webClient = New-Object System.Net.WebClient
    $webadd = "http://www.printedowl.com/CalendarNetvibeGadget.aspx?id=boche&render=1"
    $webClient.proxy = $proxy

    trap [System.Management.Automation.MethodInvocationException]{ Continue}

    $RAWTip = $webClient.DownloadString($webadd)

    if ($RAWTip -eq $null){
        $DailyTip = $null
    }
    Else {
        $DailyTip = $RAWTip -replace "[`r`n]",'' -replace '^.*<!-- google_ad_section_start -->(.*)<!-- google_ad_section_end -->.*$','$1'
    }

    $DailyTip

}

$Message = VTip

# Dont forget to change the below line to connect to your VI
Connect-VIServer MYVISERVER

$UpdateMessage = Get-View -Id SessionManager
$UpdateMessage.UpdateServiceMessage($message)

Or if your like me and do most of your work from the PowerCLI console you could remove the last 5 lines and add it to your PowerShell profile:

image

New PowerCLI Blogger

Anyone who has ever wondered how to do anything in PowerCLI and added a question to the PowerCLI community will know the name LucD or if you are on twitter, LucD22or if you were at VMWorld you will have seen the man in person presenting “TA2650 – Taking PowerCLI to the next level” together with Hal Rottenberg

After much peer pressure and beating with large sticks, Luc has started a blog, if his help on the communities is anything to go by you will know that this blog is surely to be a great place for PowerCLI and PowerShell knowledge in general.

So go over to Luc’s Blog now and add him to your RSS feeder, there are already a couple of nice posts on there explaining some of what happened at VMWorld and I cant wait for the future posts.

Dont forget he has also appeared a couple of times on this blog, giving very complex and informative posts.

Go now, stop reading this !

PowerCLI: Mass provision datastore’s

At the moment I am adding around 50 datastore’s to a host, now whilst I love using the great Virtual Infrastructure Client wizard to do this after adding two of the 50 i remembered what someone said (can’t remember who)….. If you do something more than once, script it.

So guess what I did….

The following script was used to add the datastore’s from a csv file whilst I happily worked on other things.

The csv file consisted of the following:

LUNid,Hostid
101,1
221,2
331,3
451,4
521,5
661,6

etc etc, the naming convention for my datastores is SAN_HOSThostid_LUNlunid so this was easily scripted as so:

$CSVFile = "C:\Temp\Datastores.csv"
$MYESXHost = "ESX01.virtu-al.local"
$VMHBA = "vmhba2:0:"

Connect-VIServer MYVISERVER
$CSV = Import-CSV $CSVFile
$ESXHost = Get-VMHost $MYESXHost
$ESXHost |Get-VMHostStorage -RescanAllHBA
Foreach ($Item in $CSV){
  
$HostID = $Item.HostId
  
$LunID = $Item.LunID
  
Write "Creating SAN_HOST$($HostID)_LUN$($LunID)…"
  
$ESXHost |New-Datastore -Vmfs -Name "SAN_HOST$($HostID)_LUN$($LunID)" -Path "$($VMHBA)$($HostID)"
}

The New-Datastore cmdlet also has a -BlockSizeMB parameter which can be used to specify the block size of VMFS in megabytes.

All in all it took me around 10 minutes to add 50 datastore’s which I think would have taken significantly longer through the GUI.  Yet another reason to start learning PowerCLI !