Startup Scripts

Posted on

Hello,

I've been struggling with putting together a script that will power on virtual machines in order through RHEL. Would anyone happen to have a template or an example? Any help is greatly appreciated.

This is an example that I have but I'm having issues:

Function Set-VMWareVM {

[cmdletbinding()]
param (
[parameter(Mandatory)]
$VIServer,
[parameter(Mandatory)]
[string[]]$VMName,
[parameter()]
$VIServerCredentials = (Get-Credential),
[Parameter(Mandatory)]
[ValidateSet("On","Off")]
[switch]$Toggle
)
begin
{
#Loads the VMWare Module
Add-PsSnapin VMware.VimAutomation.Core -ErrorAction "SilentlyContinue"

   #If Credentials have not been provided they will be prompted for and log in to VIServer will occur
   if (!$VIServerCredentials)
   {
       Connect-VIServer $VIServer -Credential (Get-Credential)
   }
   #If Credentials have been provided, log in to VIServer occurs
   else
   {
       Connect-VIServer $VIServer
   }

}
process
{
#If the toggle is set to On, power on of machines occurs
If ($Toggle -eq "On")
{
#Foreach vm in the $VMName Array Get-VM is run to collect the VM object and piped into Start-VM
foreach ($vm in $VMName)
{
Get-VM $vm | Start-VM
}
}
#If the toggle is set to Off, power off of machines occurs
Else
{
#Foreach vm in the $VMName Array Get-VM is run to collect the VM object and piped into Stop-VM
foreach ($vm in $VMName)
{
Get-VM $vm | Stop-VM
}
}
}
finally
{
#Disconnects from VI Server
Disconnect-VIServer
}

}

Responses