createsnapshots.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

<#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