update
This commit is contained in:
+143
@@ -0,0 +1,143 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
A short one-line action-based description, e.g. 'Tests if a function is valid'
|
||||
.DESCRIPTION
|
||||
A longer description of the function, its purpose, common use cases, etc.
|
||||
.NOTES
|
||||
Information or caveats about the function e.g. 'This function is not supported in Linux'
|
||||
.LINK
|
||||
Specify a URI to a help page, this will show when Get-Help -Online is used.
|
||||
.EXAMPLE
|
||||
Test-MyTestFunction -Verbose
|
||||
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
|
||||
#>
|
||||
|
||||
function New-ITDVMwareVMSnapshotTaskV3 {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string[]]
|
||||
$VMName,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]
|
||||
$Name,
|
||||
|
||||
[string]
|
||||
$Description,
|
||||
|
||||
[datetime]
|
||||
$DateTime,
|
||||
|
||||
[string[]]
|
||||
$Email
|
||||
)
|
||||
|
||||
begin {
|
||||
|
||||
}
|
||||
|
||||
process {
|
||||
Write-Verbose -Message "Start Loop"
|
||||
ForEach ($VName in $VMName) {
|
||||
Write-Verbose -Message "Start $VMName"
|
||||
$VM = $null
|
||||
|
||||
|
||||
$VM = Get-VM -Name $VMName | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" } -ErrorAction SilentlyContinue
|
||||
If ($VM) {
|
||||
$ViServer = $VM.Uid.split('@')[1].split(':')[0]
|
||||
|
||||
$NewSnapshotParams = @{
|
||||
Name = $Name;
|
||||
RunAsync = $true;
|
||||
}
|
||||
switch ($PSBoundParameters.Keys) {
|
||||
Description {
|
||||
$NewSnapshotParams.Description = $Description
|
||||
}
|
||||
Memory {
|
||||
$NewSnapshotParams.Memory = $Memory;
|
||||
$NewSnapshotParams.RunAsync = $false;
|
||||
}
|
||||
}
|
||||
|
||||
Write-Verbose -Message ("Validate DateTime is in the future")
|
||||
|
||||
If ($DateTime -lt (Get-Date)) {
|
||||
Write-Warning -Message "DateTime is in the past, recursively running the function again, adjusting start time to 60 seconds from now."
|
||||
$NewITDVMwareVMSnapshotTaskParams = @{
|
||||
VMName = $VMName;
|
||||
Name = $Name;
|
||||
Description = $Description;
|
||||
DateTime = (Get-Date).AddSeconds(60);
|
||||
Email = ($Email -join ',');
|
||||
}
|
||||
New-ITDVMwareVMSnapshotTaskV3 @NewITDVMwareVMSnapshotTaskParams
|
||||
}
|
||||
else {
|
||||
Write-Verbose -Message ("Attempt sched task creation")
|
||||
try {
|
||||
$si = Get-View ServiceInstance -Server $VIServer
|
||||
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager -Server $VIServer
|
||||
|
||||
$spec = New-Object VMware.Vim.ScheduledTaskSpec
|
||||
$spec.Name = $Name, $VName -join '_'
|
||||
$spec.Description = "$Description"
|
||||
$spec.Enabled = $true
|
||||
switch ($PSBoundParameters.Keys) {
|
||||
Email {
|
||||
$spec.Notification = $Email
|
||||
}
|
||||
}
|
||||
|
||||
$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
|
||||
$spec.Scheduler.runat = $DateTime
|
||||
|
||||
$spec.Action = New-Object VMware.Vim.MethodAction
|
||||
$spec.Action.Name = "CreateSnapshot_Task"
|
||||
|
||||
$Memory = $false
|
||||
$Quiesce = $false
|
||||
|
||||
@($Name, $Description, $Memory, $Quiesce) | ForEach-Object {
|
||||
$arg = New-Object VMware.Vim.MethodActionArgument
|
||||
$arg.Value = $_
|
||||
$spec.Action.Argument += $arg
|
||||
}
|
||||
Start-Sleep -Seconds 20
|
||||
|
||||
try {
|
||||
$scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)
|
||||
}
|
||||
catch [System.Management.Automation.MethodInvocationException] {
|
||||
Write-Warning -Message "System.Management.Automation.MethodInvocationException"
|
||||
If ($error[0].Exception.Message -like "*You have attempted to schedule the scheduler at a time value*" -and $error[0].Exception.Message -Like "* in the past,*") {
|
||||
Write-Warning -Message "DateTime is in the past, recursively running the function again, adjusting start time to 60 seconds in the future from now."
|
||||
$NewITDVMwareVMSnapshotTaskParams = @{
|
||||
VMName = $VMName;
|
||||
Name = $Name;
|
||||
Description = $Description;
|
||||
DateTime = (Get-Date).AddSeconds(60);
|
||||
Email = $Email;
|
||||
}
|
||||
New-ITDVMwareVMSnapshotTaskV3 @NewITDVMwareVMSnapshotTaskParams
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Warning -Message "Default"
|
||||
}
|
||||
|
||||
}
|
||||
catch {
|
||||
Write-Error $error[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
A short one-line action-based description, e.g. 'Tests if a function is valid'
|
||||
.DESCRIPTION
|
||||
Create a new snapshot of a VMware Virtual Machine
|
||||
.NOTES
|
||||
Create a new snapshot of a VMware Virtual Machine
|
||||
.LINK
|
||||
Specify a URI to a help page, this will show when Get-Help -Online is used.
|
||||
.EXAMPLE
|
||||
New-ITDVMwareVMSnapshot -VMName itdwinautot1.nd.gov -Name "Before Upgrade to 7.0U3k" -Description "Before Upgrade, delete after 3/3/2023"
|
||||
#>
|
||||
|
||||
function New-ITDVMwareVMSnapshotV3 {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string[]]
|
||||
$VMName,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]
|
||||
$Name,
|
||||
|
||||
[string]
|
||||
$Description,
|
||||
|
||||
[switch]
|
||||
$Memory
|
||||
)
|
||||
|
||||
begin {
|
||||
}
|
||||
|
||||
process {
|
||||
ForEach ($VName in $VMName) {
|
||||
$VM = Get-VM -Name $VName | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
|
||||
|
||||
$NewSnapshotParams = @{
|
||||
Name = $Name;
|
||||
RunAsync = $true;
|
||||
}
|
||||
switch ($PSBoundParameters.Keys) {
|
||||
Description {
|
||||
$NewSnapshotParams.Description = $Description
|
||||
}
|
||||
Memory {
|
||||
$NewSnapshotParams.Memory = $Memory;
|
||||
$NewSnapshotParams.RunAsync = $false;
|
||||
}
|
||||
}
|
||||
|
||||
$NewSnapshotParams
|
||||
$VM | New-Snapshot @NewSnapshotParams
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
This function for cleanup of AutoSnap* scheduled tasks that have completed their lifecycle.
|
||||
.DESCRIPTION
|
||||
This function for cleanup of vCenter scheduled tasks named AutoSnap*, all of which have completed their lifecycle.
|
||||
.NOTES
|
||||
Information or caveats about the function e.g. 'This function is not supported in Linux'
|
||||
.LINK
|
||||
Specify a URI to a help page, this will show when Get-Help -Online is used.
|
||||
.EXAMPLE
|
||||
Test-MyTestFunction -Verbose
|
||||
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
|
||||
#>
|
||||
|
||||
|
||||
function Remove-ITDVMwareVMSnapshotTaskExpired {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[switch]
|
||||
$WhatIf
|
||||
)
|
||||
|
||||
begin {
|
||||
|
||||
}
|
||||
|
||||
process {
|
||||
Write-Verbose -Message ("Gathering all scheduled tasks with AutoSnap in the task name, this will take some time")
|
||||
$si = Get-View ServiceInstance
|
||||
$scheduledTaskManager = Get-View $Si.Content.ScheduledTaskManager
|
||||
$AllScheduledTasks = Get-View -Id $scheduledTaskManager.ScheduledTask | Where-Object { $_.Info.Name -like "AutoSnap*" } | Select-Object -Unique
|
||||
ForEach ($ScheduledTask in $AllScheduledTasks) {
|
||||
Write-Verbose -Message ("Start " + $ScheduledTask.Info.Name)
|
||||
If ($ScheduledTask.Info.Name -like "AutoSnap*") {
|
||||
# Double check snapshot task
|
||||
If ($ScheduledTask.Info.PrevRunTime -lt (Get-Date) -and $ScheduledTask.Info.NextRunTime -eq $null) {
|
||||
# if run once and completed
|
||||
|
||||
If ($WhatIf){
|
||||
Write-Host -Message ("What if: Performing the operation RemoveScheduledTask() on target " + $ScheduledTask.Info.Name)
|
||||
} Else {
|
||||
Write-Verbose -Message ("Removing scheduled task named " + $ScheduledTask.Info.Name)
|
||||
$ScheduledTask.RemoveScheduledTask()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Else {
|
||||
# do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
A short one-line action-based description, e.g. 'Tests if a function is valid'
|
||||
.DESCRIPTION
|
||||
Create a new snapshot of a VMware Virtual Machine
|
||||
.NOTES
|
||||
Create a new snapshot of a VMware Virtual Machine
|
||||
.LINK
|
||||
Specify a URI to a help page, this will show when Get-Help -Online is used.
|
||||
.EXAMPLE
|
||||
New-ITDVMwareVMSnapshot -ComputerName itdwinautot1.nd.gov -Name "Before Upgrade to 7.0U3k" -Description "Before Upgrade, delete after 3/3/2023"
|
||||
#>
|
||||
|
||||
function Remove-ITDVMwareVMSnapshotV3 {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[string]
|
||||
$ComputerName,
|
||||
|
||||
[string]
|
||||
$Name
|
||||
)
|
||||
|
||||
begin {
|
||||
|
||||
}
|
||||
|
||||
process {
|
||||
|
||||
$VM = Get-VM -Name $ComputerName | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
|
||||
|
||||
$GetSnapshotParams = @{
|
||||
Name = $Name
|
||||
}
|
||||
|
||||
$VM | Get-Snapshot @GetSnapshotParams | Remove-Snapshot -Confirm:$false
|
||||
}
|
||||
|
||||
end {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user