I finally have the chance to really get started working with and learning PowerShell. There has been a big push at work to master this skill quickly. I have worked with a few different scripting languages over my career and I also seem to end up focusing on one at a time. Now it was really time to get heads down with PowerShell. I have started to create my own PowerShell toolkit and thought I would share some of the basic scripts that I have just starting building as the basis of my toolkit. I am going to spend the next couple of blog posts posting some of the scripts that I have built and as my confidence and skill level increase you should really see the scripts develop. I would like to give a shout out to Scott Herold and his work with TheVESI project. I have download this fantastic tool and have been using this to create my scripts. Kudos to Scott and TheVESI team for a job well done. If you have not had a chance to take a look that this I highly recommend it. So let’s start putting together our toolkit shall we?
First script is the most basic scripts, starting a VM.
#PowerShell script to start a VM
#Stephen Beaver
#Tripwire
#April 2009
#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
Add-PSSnapIn VMware.VimAutomation.Core
$VMachineName = $Args[0]
if ($VMachineName -eq $null){
Write-Host
Write-Host “Please specify a Virtual Machine name eg….”
Write-Host ” powershell.exe startvm.ps1 VM1″
Write-Host
Write-Host
exit
}
Connect-VIServer
$VMachineName = $Args[0]
# This next line will restart the without a prompt before the action is taken
Start-VM -VM $VMachineName -Confirm:$false
# Comment the line above and use the line below to be prompted
#Start-VM -VM $VMachineName -Confirm:$true
Next Stopping a VM
#PowerShell script to stop a VM
#Stephen Beaver
#Tripwire
#April 2009
#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
Add-PSSnapIn VMware.VimAutomation.Core
$VMachineName = $Args[0]
if ($VMachineName -eq $null){
Write-Host
Write-Host “Please specify a Virtual Machine name eg….”
Write-Host ” powershell.exe stopvm.ps1 VM1″
Write-Host
Write-Host
exit
}
Connect-VIServer
# This next line will restart the without a prompt before the action is taken
Stop-VM -VM $VMachineName -Confirm:$false
# Comment the line above and use the line below to be prompted
#Stop-VM -VM $VMachineName -Confirm:$true
Restarting a Virtual Machine
#PowerShell script to restart a VM
#Stephen Beaver
#Tripwire
#April 21 2009
#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
Add-PSSnapIn VMware.VimAutomation.Core
$VMachineName = $Args[0]
if ($VMachineName -eq $null){
Write-Host
Write-Host “Please specify a Virtual Machine name eg….”
Write-Host ” powershell.exe vm_cddrive_remove.ps1 VM1″
Write-Host
Write-Host
exit
}
Connect-VIServer
# This next line will restart the without a prompt before the action is taken
#Restart-VMGuest -VM $VMachineName -Confirm:$false
# Comment the line above and use the line below to be prompted
Restart-VMGuest -VM $VMachineName -Confirm:$true
So as you see not too much is really needed to get started. Until next time
Happy Scripting!!!
powershell, Scripting, TheVESI, virtualization, vmware, VMware Tool Kit
Previous Post
Partner Exchange and the vSphere Press Release
Next Post
Start of a PowerShell Tooklit Part 2
This entry was posted on Monday, April 27th, 2009 at 8:58 am and is filed under Virtual Tech. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.







April 27th, 2009 9:23 am
Steve, great to see you promoting PS & the VITK, or should I already say PowerCLI, in your blog.
One quick tip, instead of loading the pssnapin in each script, you can place the Add-PSSnapin cmdlet in your profile script. Then it is loaded each time you start a PS script.
See http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-10-scripts.aspx#four-different-profile-scripts for a good intro to PS profiles.
April 27th, 2009 9:27 am
Thanks for the Tip!!! I am on the accelerated learning curve!!! All tips welcome
Steve
April 28th, 2009 9:19 am
Steve,
don’t forget to do error handling. I hardly see any scripts that do error handling, logging etc.