Script to backup and restore ESXi

Ok nice easy one
#Select the host you want to back up
$Computer = Read-Host "Please enter the VM host name"

#Enter the root password 
$Userpass = Read-Host "Please Enter the Root Password"

# Enter the location where you will be storing the backups. Can be local or file share 
$location = Read-host "enter the location where you will be storing the backups. Can be local or file share"

#Select to backup or restore the host.
$Backup = Get-VMHostFirmware -VMHost $Computer -BackupConfiguration "$Location" -HostUser root -HostPassword $Userpass
$Restore = Set-VMHostFirmware -VMHost $Computer -Restore -SourcePath "$Location\Configbundle-$Computer.TGZ" -HostUser root -HostPassword $Userpass

Open in new window


What I want to do is basically have it come up with an option box requesting whether you want to backup or restore the ESXi host. The code is fine but I can't figure out how to get the pop up.

Can someone provide me the CMDLet for this and I'll give it a shot :D

Thanks
Alex
LVL 17
Alex Green3rd Line Server SupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
Well Powershell at its core doesn't provide even the minimal interaction GUI unlike its predecessor (VbScript) but if you combine it with .NET framework can do wonders..
So start from here
0
Alex Green3rd Line Server SupportAuthor Commented:
Hmmm Ok,

It doesn't have to be a form, I'm happy with option boxes, or doing a select 1 or 2

#Select the host you want to back up
$Computer = Read-Host "Please enter the VM host name"

#Enter the root password 
$Userpass = Read-Host "Please Enter the Root Password"

# Enter the location where you will be storing the backups. Can be local or file share 
$location = Read-host "enter the location where you will be storing the backups. Can be local or file share"

#Select to backup or restore the host.

{$BUorRE = Read-host "Please Make a selection" 

'Backup'{Get-VMHostFirmware -VMHost $Computer -BackupConfiguration "$Location" -HostUser root -HostPassword $Userpass
}
'Restore'{Set-VMHostFirmware -VMHost $Computer -Restore -SourcePath "$Location\Configbundle-$Computer.TGZ" -HostUser root -HostPassword $Userpass
}
}

Open in new window

0
John TsioumprisSoftware & Systems EngineerCommented:
If i am not mistaken there aren't any...i think there is a simple messagebox but it has no interaction....maybe i am wrong but some months ago i was requested to deliver something similar i wend the Winforms solution and it was great.....
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Alex Green3rd Line Server SupportAuthor Commented:
OK but there must be a way where I can have

IF option 1 selected, run 'Backup' {Get-VMHostFirmware -VMHost $Computer -BackupConfiguration "$Location" -HostUser root -HostPassword $Userpass

If option 2 selected run 'Restore' {Set-VMHostFirmware -VMHost $Computer -Restore -SourcePath "$Location\Configbundle-$Computer.TGZ" -HostUser root -HostPassword $Userpass
}
0
John TsioumprisSoftware & Systems EngineerCommented:
I think this tutorial has the answers you need
0
Alex Green3rd Line Server SupportAuthor Commented:
Right
#Select the host you want to back up
$Computer = Read-Host "Please enter the VM host name"

#Enter the root password 
$Userpass = Read-Host "Please Enter the Root Password"

# Enter the location where you will be storing the backups. Can be local or file share 
$location = Read-host "enter the location where you will be storing the backups. Can be local or file share"

#Select to backup or restore the host.


$msgBoxInput =  [System.Windows.MessageBox]::Show('Would you like to Backup or Restore the configuration','Backup or Restore', 'Backup','Restore')

  switch  ($msgBoxInput) {

  'Backup' {

        Get-VMHostFirmware -VMHost $Computer -BackupConfiguration "$Location" -HostUser root -HostPassword $Userpass

           }

  'Restore' {
        Set-VMHostFirmware -VMHost $Computer -Restore -SourcePath "$Location\Configbundle-$Computer.TGZ" -HostUser root -HostPassword $Userpass
            }

}

Open in new window


I'm sure that's pretty much what I want, but I get the error

Cannot convert argument "button", with value: "Backup", for "Show" to type "System.Windows.MessageBoxButton": "Cannot convert value "Backup" to type "System.Windows.MessageBoxButton".
Error: "Unable to match the identifier name Backup to a valid enumerator name. Specify one of the following enumerator names and try again:
OK, OKCancel, YesNoCancel, YesNo""
At line:13 char:1
+ $msgBoxInput =  [System.Windows.MessageBox]::Show('Would you like to  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
0
John TsioumprisSoftware & Systems EngineerCommented:
You are getting errors because messagebox does not have override for the default values for its buttons...the example i gave you was a starting point for interaction with Powershell+Winforms
In order to proceed you need to combine the 2 tutorials to create a small form with 2 buttons one for backup, one for restore..
Right now the code is somewhere on my home machine ...so give it one more try and if you get an issue i will try to locate it...
0
John TsioumprisSoftware & Systems EngineerCommented:
Well i had some time...me regular job is so boring....:(
and this is a sample script i made
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Windows.Forms.Application]::EnableVisualStyles()

$Form0 = New-Object System.Windows.Forms.Form
$Form0.Text = "Virtual Machine Backup/Restore"
$Form0.ClientSize = New-Object System.Drawing.Size(600, 400)
$ButtonBackup = New-Object System.Windows.Forms.Button
$ButtonBackup.BackColor = "White"
$ButtonBackup.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 8.25, [System.Drawing.FontStyle]::Regular)
$ButtonBackup.ForeColor = "ControlText"
$ButtonBackup.Text = "Backup"
$ButtonBackup.Add_Click({$Form0.Close()})
$ButtonBackup.Size = New-Object System.Drawing.Size(200, 100)
$ButtonBackup.Location = New-Object System.Drawing.Point(32, 250)
$Form0.Controls.Add($ButtonBackup)
$ButtonRestore = New-Object System.Windows.Forms.Button
$ButtonRestore.BackColor = "Blue"
$ButtonRestore.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 8.25, [System.Drawing.FontStyle]::Regular)
$ButtonRestore.ForeColor = "ControlText"
$ButtonRestore.Text = "Restore"
$ButtonRestore.Add_Click({$Form0.Close()})
$ButtonRestore.Size = New-Object System.Drawing.Size(200, 100)
$ButtonRestore.Location = New-Object System.Drawing.Point(256, 250)
$Form0.Controls.Add($ButtonRestore)
 #Add Button event 
    $ButtonBackup.Add_Click(
        {    
		[System.Windows.Forms.MessageBox]::Show("Backup the VM" , "Backup")
        }
    )
	$ButtonRestore.Add_Click(
        {    
		[System.Windows.Forms.MessageBox]::Show("Restore the VM." , "Restore")
        }
    )
[void]$Form0.ShowDialog()

Open in new window


Just replace the Click event action with your own PS command and you should be good to go...oh not to forget...you should probably need to add some input text boxes to enter the data now that we have jumped to the GUI side of PS
0
John TsioumprisSoftware & Systems EngineerCommented:
Well its official...i must gather strength to return to normal....so here is enhanced with input...:)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Windows.Forms.Application]::EnableVisualStyles()

$Form0 = New-Object System.Windows.Forms.Form
$Form0.Text = "Virtual Machine Backup/Restore"
$Form0.ClientSize = New-Object System.Drawing.Size(600, 400)
$ButtonBackup = New-Object System.Windows.Forms.Button
$ButtonBackup.BackColor = "White"
$ButtonBackup.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 8.25, [System.Drawing.FontStyle]::Regular)
$ButtonBackup.ForeColor = "ControlText"
$ButtonBackup.Text = "Backup"
$ButtonBackup.Add_Click({$Form0.Close()})
$ButtonBackup.Size = New-Object System.Drawing.Size(200, 100)
$ButtonBackup.Location = New-Object System.Drawing.Point(32, 250)
$Form0.Controls.Add($ButtonBackup)
$ButtonRestore = New-Object System.Windows.Forms.Button
$ButtonRestore.BackColor = "Blue"
$ButtonRestore.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 8.25, [System.Drawing.FontStyle]::Regular)
$ButtonRestore.ForeColor = "ControlText"
$ButtonRestore.Text = "Restore"
$ButtonRestore.Add_Click({$Form0.Close()})
$ButtonRestore.Size = New-Object System.Drawing.Size(200, 100)
$ButtonRestore.Location = New-Object System.Drawing.Point(256, 250)
$Form0.Controls.Add($ButtonRestore)
$labelUsername = New-Object System.Windows.Forms.Label
$labelUsername.Location = New-Object System.Drawing.Point(10,20)
$labelUsername.Size = New-Object System.Drawing.Size(280,20)
$labelUsername.Text = 'UserName:'
$Form0.Controls.Add($labelUsername)

$Username = New-Object System.Windows.Forms.TextBox
$UserName.Location = New-Object System.Drawing.Point(10,40)
$Username.Size = New-Object System.Drawing.Size(260,20)
$Form0.Controls.Add($UserName)
 #Add Button event 
    $ButtonBackup.Add_Click(
        {    
		$strUserName = $UserName.Text
		[System.Windows.Forms.MessageBox]::Show($strUserName + " Its Time Backup the VM " , "Backup")
        }
    )
	$ButtonRestore.Add_Click(
        {    
		[System.Windows.Forms.MessageBox]::Show("Restore the VM." , "Restore")
        }
    )
[void]$Form0.ShowDialog()

Open in new window

ps.jpg
0
Alex Green3rd Line Server SupportAuthor Commented:
But how does that code pull the code to run? With the backup and Restore bit attached to this?

  'Backup' {

        Get-VMHostFirmware -VMHost $Computer -BackupConfiguration "$Location" -HostUser root -HostPassword $Userpass

           }

  'Restore' {
        Set-VMHostFirmware -VMHost $Computer -Restore -SourcePath "$Location\Configbundle-$Computer.TGZ" -HostUser root -HostPassword $Userpass
            }

}
0
John TsioumprisSoftware & Systems EngineerCommented:
Just change what you need
$ButtonBackup.Add_Click(
        {    
		$Userpass=  $UserPassword.Text
                $Computer =$ComputerName.Text
                $Location =  $BackupLocation.Text
		Get-VMHostFirmware -VMHost $Computer -BackupConfiguration "$Location" -HostUser root -HostPassword $Userpass
        }
    )

Open in new window

based on my code you need to "insert" 3 labels and 3 text boxes (UserPassword,ComputerName,BackupLocation ) ...just take my label/textbox as template and work on it
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.