createsnapshots.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 7e9d1c51-0cfa-49ff-91c4-0290a2ed9757 .AUTHOR Rui Duarte .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION This script will allow to create snapshots on vm's in vcenter by specifying a folder name or a list of vms and will create a log #> Param() #Function function New-Foldersnapshot { [cmdletbinding()] param( [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "Specify Vcenter folder")] [String]$vmfolder, [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "Specify Ticket number")] [String]$IMnumber, [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "Enter FQDN of Virtual Center")] [string]$vcenter ) <# #Synopsis Take snapshots of vms in a vcenter folder .Author Rui Duarte .Date 18/05/2017 .How to use Specify a vm folder where you want to take vms snapshots Just run the function from powershell and it will ask for all the necessary parameters #> #### Import Modules if (!(Get-Module -Name vmware.vimautomation.core -ErrorAction SilentlyContinue)) { #VMware Module . "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" } #Credentials $credmatchtext = "2" while (($credmatchtext -ne $vccredtext) -or ($credmatch.username -ne $vccred.username)) { #VCENTER CRED $vccred = $host.ui.PromptForCredential("$vccreddomain credentials", "Enter your $vccreddomain credentials for vCenter access", "", "") $credmatch = $host.ui.PromptForCredential("Re-enter $vccreddomain credentials", "Re-Enter your $vccreddomain credentials", "", "") $vccredtext = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vccred.password)) $credmatchtext = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($credmatch.password)) } #Connect Vcenter if ($defaultviservers) { Disconnect-VIServer * -confirm:$false -ErrorAction SilentlyContinue } if ($vcenter) { Connect-VIServer $vcenter -Credential $vccred -ErrorAction Stop } #Start Transcript $time = (Get-Date).ToString('dd-MM_HHmm') Start-Transcript -Path "C:\snapshots$time.txt" #Create the Snapshots get-vm * -Location $vmfolder | New-Snapshot -Name "$IMNumber" -Server $vcenter -Description "Created $(Get-Date) by $env:USERNAME" -Quiesce #Stop Transcript Stop-Transcript Write-Host -foregroundcolor Yellow "Snapshots done, find the log at C:\snapshots$time.txt" } function New-VMsnapshot { [cmdletbinding()] param( [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "Specify VM from file .txt")] [String]$filename, [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "Specify Ticket number")] [String]$IMnumber, [parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "Enter FQDN of Virtual Center")] [string]$vcenter ) <# #Synopsis Take snapshots of vms in vcenter .Author Rui Duarte .Date 26/04/2017 .How to use Create a txt file with the vm names (one per line) or do vmname* do to multiple vms with that name for example: vmname* will take snapshots of all the vms that start with "vmname" Just run the script from powershell and it will ask for all the necessary parameters #> #### Import Modules if (!(Get-Module -Name vmware.vimautomation.core -ErrorAction SilentlyContinue)) { #VMware Module . "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" } #Credentials $credmatchtext = "2" while (($credmatchtext -ne $vccredtext) -or ($credmatch.username -ne $vccred.username)) { #VCENTER CRED $vccred = $host.ui.PromptForCredential("$vccreddomain credentials", "Enter your $vccreddomain credentials for vCenter access", "", "") $credmatch = $host.ui.PromptForCredential("Re-enter $vccreddomain credentials", "Re-Enter your $vccreddomain credentials", "", "") $vccredtext = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vccred.password)) $credmatchtext = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($credmatch.password)) } if (Test-Path -path $filename) { $VMs = Get-Content $filename } else { Write-Host "$filename file doesn't exist, please create the list of vms file"; break } #$VMs = (get-vm -Name "am3*han*p*"| Where-Object GuestId -Like "*window*").Name #Connect Vcenter if ($defaultviservers) { Disconnect-VIServer * -confirm:$false -ErrorAction SilentlyContinue } if ($vcenter) { Connect-VIServer $vcenter -Credential $vccred -ErrorAction Stop } #Start Transcript $time = (Get-Date).ToString('dd-MM_HHmm') Start-Transcript -Path "C:\snapshots$time.txt" #Create the Snapshots get-vm $VMs | New-Snapshot -Name "$IMNumber" -Server $vcenter -Description "Created $(Get-Date) by $env:USERNAME" -Quiesce #Stop Transcript Stop-Transcript Write-Host -foregroundcolor Yellow "Snapshots done, find the log at C:\snapshots$time.txt" } #Script While ( $question -notlike "y" -and $question -notlike "n") { $question = read-host "Snapshot a vm folder ex: customerProd? y/n" } if ($question -eq "y"){ New-Foldersnapshot } if ($question -eq "n"){ New-VMsnapshot } remove-variable question Disconnect-VIServer -Force -Confirm:$false |