update
This commit is contained in:
+168
@@ -0,0 +1,168 @@
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[object] $WebhookData
|
||||
)
|
||||
|
||||
function Get-ITDVMwareSharePointSnapshotRequestList {
|
||||
[CmdletBinding()]
|
||||
Param
|
||||
(
|
||||
[PSCredential]
|
||||
$Credential
|
||||
)
|
||||
|
||||
begin {
|
||||
$InvokeWebRequestParams = @{ }
|
||||
If ($Credential) { $InvokeWebRequestParams += @{Credential = $Credential } }
|
||||
Else { $InvokeWebRequestParams += @{UseDefaultCredentials = $true } }
|
||||
|
||||
$URL = "https://testshare.nd.gov/itd/Computer-Systems/Distributed-Systems/VMWare/_api/lists/getbytitle('VMware-Snapshots')/items" + '?$top=10000' + '&$select=ID,Title,DateTime,Duration,Author/Name,Author/Title,Author/EMail,Status,NotifyEmail&$expand=Author/Id'
|
||||
$InvokeWebRequestParams += @{
|
||||
Uri = $URL;
|
||||
Method = "Get";
|
||||
UseBasicParsing = $true;
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" }
|
||||
}
|
||||
|
||||
$List = (Invoke-WebRequest @InvokeWebRequestParams) -creplace '"Id":', '"Idx":' | ConvertFrom-Json
|
||||
}
|
||||
|
||||
process {
|
||||
|
||||
}
|
||||
|
||||
end {
|
||||
$List.d.results
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
If ($WebhookData) {
|
||||
If ($WebhookData.RequestHeader.ITD -eq 'w3gVRLEq2Yi7yzWW8fpKNCPTKqtHssnNGBref5BDtxXkr3mZA3NB5gUcnHfr9sdhXC53y7gWHAKpueFYU4L3NMXV7DszCWy63znqPGh3AJ2bn43uP7GjzfLcgqYLQRkirpR3kjiLgi3FpRPgUWus9Zx') {
|
||||
Write-Verbose "Header has required data"
|
||||
}
|
||||
Else {
|
||||
Write-Error "Header missing required data"
|
||||
exit;
|
||||
}
|
||||
|
||||
$InputParams = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
|
||||
$vCenterCredential = Get-AutomationPSCredential -Name 'VMware Auto'
|
||||
$SharePointCredential = Get-AutomationPSCredential -Name 'SharePoint IaaS ReadWrite'
|
||||
|
||||
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -ParticipateInCeip $false -Confirm:$false
|
||||
Connect-VIServer -Server itdvmvc1.nd.gov, itdvmvc2.nd.gov -Credential $vCenterCredential
|
||||
|
||||
$Id = $InputParams.Id
|
||||
$VMName = $InputParams.Hostname
|
||||
|
||||
$SnapshotList = Get-ITDVMwareSharePointSnapshotRequestList -Credential $SharePointCredential
|
||||
$Request = $SnapshotList | where-object ID -eq $id
|
||||
$SnapName = "AutoSnap_" + $Id
|
||||
|
||||
$SnapshotToRemove = Get-Snapshot -Name $SnapName -VM $VMName
|
||||
$Datastore = Get-VM $VMName | Get-Datastore
|
||||
$ViServer = $SnapshotToRemove.uid.split('@')[1].split(':')[0]
|
||||
|
||||
write-warning ($SnapshotToRemove.Name + " for " + $SnapshotToRemove.VM + " on " + $Datastore + " on " + $ViServer)
|
||||
|
||||
|
||||
#try {
|
||||
$SnapshotTaskCount = @(Get-Task -Server $ViServer | Where-Object {$_.Name -eq "RemoveSnapshot_Task" -and $_.State -eq "Running"}).count
|
||||
write-warning $SnapshotTaskCount
|
||||
while ($SnapshotTaskCount -ge 1) {
|
||||
$timestamp = Get-Date -UFormat "%Y%m%d%H%M%S"
|
||||
Write-Warning "[$timestamp]:$SnapshotTaskCount RemoveSnapshot_Task already running, pausing 60 seconds."
|
||||
Start-Sleep -Seconds 60
|
||||
|
||||
$timestamp = $null
|
||||
$SnapshotTaskCount = @(Get-Task | Where-Object {$_.Name -eq "RemoveSnapshot_Task" -and $_.State -eq "Running"}).count
|
||||
write-warning $SnapshotTaskCount
|
||||
}
|
||||
$timestamp = Get-Date -UFormat "%Y%m%d%H%M%S"
|
||||
Write-Warning "[$timestamp]:$SnapshotTaskCount RemoveSnapshot_Task already running, starting removal."
|
||||
$SnapshotToRemove | Remove-Snapshot -Confirm:$false -RunAsync
|
||||
<#}
|
||||
catch {
|
||||
Write-Error "Snapshot Removal failed";
|
||||
Write-Error $Error[1];
|
||||
exit
|
||||
}#>
|
||||
|
||||
#update SharePoint field
|
||||
Write-Verbose "Exactly one match found, updating SharePoint fields named Status and SnapshotTaken"
|
||||
$UrlContextInfo = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/contextinfo"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlContextInfo;
|
||||
Method = "Post";
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
$RequestDigest = Invoke-RestMethod @InvokeWebRequestParams
|
||||
$RequestDigest = $RequestDigest.GetContextWebInformation.FormDigestValue
|
||||
|
||||
$UrlList = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlList;
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
$List = Invoke-RestMethod @InvokeWebRequestParams
|
||||
$ListItemEntityTypeFullName = $list.entry.content.properties.ListItemEntityTypeFullName
|
||||
|
||||
$UrlListItem = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')/items" + '?$top=10000'
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlListItem;
|
||||
Method = "Get";
|
||||
UseBasicParsing = $true;
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" };
|
||||
Credential = $SharePointCredential
|
||||
}
|
||||
$ListItems = ((Invoke-WebRequest @InvokeWebRequestParams) -creplace '"Id":', '"Idx":' | ConvertFrom-Json).d.results
|
||||
|
||||
$header = @{
|
||||
"accept" = "application/json;odata=verbose"
|
||||
"X-RequestDigest" = $RequestDigest
|
||||
"IF-MATCH" = '*'
|
||||
"X-HTTP-Method" = "MERGE"
|
||||
}
|
||||
|
||||
$RecordToModify = $ListItems | Where-Object Id -eq $InputParams.Id
|
||||
If (@($RecordToModify).count -gt 1) { Write-Error "More than one result, stopping"; break }
|
||||
$IDtoModify = $RecordToModify.ID
|
||||
|
||||
$UrlItem = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')/items($IDtoModify)"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlItem;
|
||||
Method = "Get";
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" }
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential
|
||||
}
|
||||
|
||||
[PSCustomObject]$SetRecord = @{
|
||||
"__metadata" = @{type = $ListItemEntityTypeFullName };
|
||||
Status = 'Removed';
|
||||
SnapshotRemoved = [datetime](Get-Date);
|
||||
}
|
||||
|
||||
$body = $SetRecord | ConvertTo-Json
|
||||
$InvokeWebRequestParams = @{
|
||||
|
||||
Uri = $UrlItem;
|
||||
Method = "Post";
|
||||
Body = $body;
|
||||
ContentType = "application/json;odata=verbose";
|
||||
Headers = $header;
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
Invoke-RestMethod @InvokeWebRequestParams
|
||||
|
||||
Disconnect-VIServer -Server * -Confirm:$false
|
||||
}
|
||||
Else {
|
||||
Write-Error "Runbook must be started from webhook"
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $false)]
|
||||
[object]
|
||||
$WebhookData
|
||||
)
|
||||
|
||||
If ($WebhookData) {
|
||||
If ($WebhookData.RequestHeader.ITD -eq 'vBxbnq2g9M7cQJtFN3iGU4kD4pPJaSEgC5HML5ZuZk6wKfZQrpj2XfuQ5s') {
|
||||
Write-Verbose "Header has required data"
|
||||
}
|
||||
Else {
|
||||
Write-Error "Header missing required data"
|
||||
exit;
|
||||
}
|
||||
|
||||
$InputParams = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
|
||||
$vCenterCredential = Get-AutomationPSCredential -Name 'VMware Auto'
|
||||
$SharePointCredential = Get-AutomationPSCredential -Name 'SharePoint IaaS ReadWrite'
|
||||
|
||||
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -ParticipateInCeip $false -Confirm:$false
|
||||
Connect-VIServer -Server itdvmvc1.nd.gov, itdvmvc2.nd.gov -Credential $vCenterCredential
|
||||
|
||||
$VMName = $InputParams.HostName
|
||||
$Id = $InputParams.Id
|
||||
$Duration = $InputParams.Duration
|
||||
|
||||
#$VM = Get-VM -Name $VMName | where-object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
|
||||
|
||||
Write-Verbose "Duration = Clone"
|
||||
$VMClones = Get-VM | Where-Object Name -eq ($VMName + "_AutoClone_" + $Id)
|
||||
|
||||
If (@($VMClones).count -eq 1) {
|
||||
$Taken = $true
|
||||
$VMClone = $VMClones
|
||||
}
|
||||
else {
|
||||
if (@($VMClones).count -gt 1) {
|
||||
Write-Error "More than one VM clone match found, abort"
|
||||
Exit
|
||||
}
|
||||
else { Write-Verbose "Clone Id #$Id match not found for $VMName" }
|
||||
}
|
||||
|
||||
If (@($VMClones).count -eq 1) {
|
||||
$VMClone = $VMClones
|
||||
Write-Verbose "Exactly one clone match found, updating SharePoint fields named Status and SnapshotTaken"
|
||||
|
||||
$Task = ((Get-View ((Get-View ScheduledTaskManager).ScheduledTask)).info | where-object Description -like "AutoClone_$Id*")
|
||||
|
||||
If ($Taken -eq $true) {
|
||||
$UrlContextInfo = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/contextinfo"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlContextInfo;
|
||||
Method = "Post";
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
$RequestDigest = Invoke-RestMethod @InvokeWebRequestParams
|
||||
$RequestDigest = $RequestDigest.GetContextWebInformation.FormDigestValue
|
||||
|
||||
$UrlList = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlList;
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
$List = Invoke-RestMethod @InvokeWebRequestParams
|
||||
$ListItemEntityTypeFullName = $list.entry.content.properties.ListItemEntityTypeFullName
|
||||
|
||||
$UrlListItem = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')/items" + '?$top=10000'
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlListItem;
|
||||
Method = "Get";
|
||||
UseBasicParsing = $true;
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" };
|
||||
Credential = $SharePointCredential
|
||||
}
|
||||
$ListItems = ((Invoke-WebRequest @InvokeWebRequestParams) -creplace '"Id":', '"Idx":' | ConvertFrom-Json).d.results
|
||||
|
||||
$header = @{
|
||||
"accept" = "application/json;odata=verbose"
|
||||
"X-RequestDigest" = $RequestDigest
|
||||
"IF-MATCH" = '*'
|
||||
"X-HTTP-Method" = "MERGE"
|
||||
}
|
||||
|
||||
$RecordToModify = $ListItems | Where-Object Id -eq $Id
|
||||
If (@($RecordToModify).count -gt 1) { Write-Error "More than one record, stopping"; break }
|
||||
$IDtoModify = $RecordToModify.ID
|
||||
|
||||
$UrlItem = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')/items($IDtoModify)"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlItem;
|
||||
Method = "Get";
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" }
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential
|
||||
}
|
||||
|
||||
[PSCustomObject]$SetRecord = @{
|
||||
"__metadata" = @{type = $ListItemEntityTypeFullName };
|
||||
Status = 'Taken';
|
||||
SnapshotTaken = $Task.PrevRunTime;
|
||||
AutoExpire = ($Task.Description -split '[\r\n]+')[1].split('_')[1];
|
||||
AutoDelete = ($Task.Description -split '[\r\n]+')[2].split('_')[1];
|
||||
}
|
||||
|
||||
$body = $SetRecord | ConvertTo-Json
|
||||
$InvokeWebRequestParams = @{
|
||||
|
||||
Uri = $UrlItem;
|
||||
Method = "Post";
|
||||
Body = $body;
|
||||
ContentType = "application/json;odata=verbose";
|
||||
Headers = $header;
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
Invoke-RestMethod @InvokeWebRequestParams
|
||||
|
||||
Disconnect-VIServer * -Confirm:$false
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Error "Runbook must be started from webhook"
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory = $false)]
|
||||
[object]
|
||||
$WebhookData
|
||||
)
|
||||
|
||||
If ($WebhookData) {
|
||||
If ($WebhookData.RequestHeader.ITD -eq 'vBxbnq2g9M7cQJtFN3iGU4kD4pPJaSEgC5HML5ZuZk6wKfZQrpj2XfuQ5s') {
|
||||
Write-Verbose "Header has required data"
|
||||
}
|
||||
Else {
|
||||
Write-Error "Header missing required data"
|
||||
exit;
|
||||
}
|
||||
|
||||
$InputParams = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
|
||||
$vCenterCredential = Get-AutomationPSCredential -Name 'VMware Auto'
|
||||
$SharePointCredential = Get-AutomationPSCredential -Name 'SharePoint IaaS ReadWrite'
|
||||
|
||||
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -ParticipateInCeip $false -Confirm:$false
|
||||
Connect-VIServer -Server itdvmvc1.nd.gov, itdvmvc2.nd.gov -Credential $vCenterCredential
|
||||
|
||||
$VMName = $InputParams.HostName
|
||||
$Id = $InputParams.Id
|
||||
$Duration = $InputParams.Duration
|
||||
|
||||
$VM = Get-VM -Name $VMName | where-object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
|
||||
If (@($VM).count -eq 2) { $VM = Get-VM -Name $VMName -Server ($global:DefaultVIServers | sort-object Name | select -first 1).Name }
|
||||
|
||||
|
||||
Write-Verbose "Duration = Snapshot"
|
||||
$VMSnapshots = $VM | Get-Snapshot | where-object Description -like "AutoSnap_$Id*"
|
||||
|
||||
If (@($VMSnapshots).count -eq 1) {
|
||||
$Taken = $true
|
||||
$VMSnapshot = $VMSnapshots
|
||||
}
|
||||
else {
|
||||
if (@($VMSnapshots | where-object Description -like "AutoSnap_$Id*").count -gt 1) {
|
||||
Write-Error "More than one snapshot match found, abort"
|
||||
Exit
|
||||
}
|
||||
else { Write-Verbose "Snapshot Id #$Id match not found on $VMName" }
|
||||
}
|
||||
|
||||
If (@($VMSnapshots).count -eq 1) {
|
||||
Write-Verbose "Exactly one snapshot match found, updating SharePoint fields named Status and SnapshotTaken"
|
||||
|
||||
If ($Taken -eq $true) {
|
||||
$UrlContextInfo = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/contextinfo"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlContextInfo;
|
||||
Method = "Post";
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
$RequestDigest = Invoke-RestMethod @InvokeWebRequestParams
|
||||
$RequestDigest = $RequestDigest.GetContextWebInformation.FormDigestValue
|
||||
|
||||
$UrlList = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlList;
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
$List = Invoke-RestMethod @InvokeWebRequestParams
|
||||
$ListItemEntityTypeFullName = $list.entry.content.properties.ListItemEntityTypeFullName
|
||||
|
||||
$UrlListItem = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')/items" + '?$top=10000'
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlListItem;
|
||||
Method = "Get";
|
||||
UseBasicParsing = $true;
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" };
|
||||
Credential = $SharePointCredential
|
||||
}
|
||||
$ListItems = ((Invoke-WebRequest @InvokeWebRequestParams) -creplace '"Id":', '"Idx":' | ConvertFrom-Json).d.results
|
||||
|
||||
$header = @{
|
||||
"accept" = "application/json;odata=verbose"
|
||||
"X-RequestDigest" = $RequestDigest
|
||||
"IF-MATCH" = '*'
|
||||
"X-HTTP-Method" = "MERGE"
|
||||
}
|
||||
|
||||
$RecordToModify = $ListItems | Where-Object Id -eq $InputParams.Id
|
||||
If (@($RecordToModify).count -gt 1) { Write-Error "More than one record, stopping"; break }
|
||||
$IDtoModify = $RecordToModify.ID
|
||||
|
||||
$UrlItem = "https://testshare.nd.gov/itd/computer-systems/distributed-systems/vmware/_api/lists/getbytitle('VMware-Snapshots')/items($IDtoModify)"
|
||||
$InvokeWebRequestParams = @{
|
||||
Uri = $UrlItem;
|
||||
Method = "Get";
|
||||
Headers = @{ "Accept" = "application/json;odata=verbose" }
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential
|
||||
}
|
||||
|
||||
[PSCustomObject]$SetRecord = @{
|
||||
"__metadata" = @{type = $ListItemEntityTypeFullName };
|
||||
Status = 'Taken';
|
||||
SnapshotTaken = $VMSnapshot.Created;
|
||||
AutoExpire = ($VMSnapshot.Description -split '[\r\n]+')[1].split('_')[1];
|
||||
AutoDelete = ($VMSnapshot.Description -split '[\r\n]+')[2].split('_')[1];
|
||||
}
|
||||
|
||||
$body = $SetRecord | ConvertTo-Json
|
||||
$InvokeWebRequestParams = @{
|
||||
|
||||
Uri = $UrlItem;
|
||||
Method = "Post";
|
||||
Body = $body;
|
||||
ContentType = "application/json;odata=verbose";
|
||||
Headers = $header;
|
||||
UseBasicParsing = $true;
|
||||
Credential = $SharePointCredential;
|
||||
}
|
||||
Invoke-RestMethod @InvokeWebRequestParams
|
||||
|
||||
Disconnect-VIServer * -Confirm:$false
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Error "Runbook must be started from webhook"
|
||||
}
|
||||
Reference in New Issue
Block a user