DSCResources/DSC_PowerCLISnapShotCheck/DSC_PowerCLISnapShotCheck.psm1
function Test-TargetResource { [OutputType([boolean])] param ( [parameter(Mandatory)] [string] $vcentername, [parameter(Mandatory)] [string] $resourcepoolname, [Parameter(Mandatory)] [PSCredential]$DomainAdministratorCredential, [ValidateSet('Present','Absent')] [string] $Ensure = 'Present' ) try { #Addpssnapin VMware.VimAutomation.Core Add-PSSnapin VMware.VimAutomation.Core #$MyCredentials= Import-Clixml $credentialfile Write-Verbose "Connecting to vCenter $vcentername with credentials $credentialfile" Connect-VIServer -server $vcentername -Credential $DomainAdministratorCredential | Out-Null Write-Verbose "Checking if the snapshots exist or doesnot exist" $snapshotExist = Get-ResourcePool $resourcepoolname | Get-VM | Get-Snapshot if ($Ensure -eq "Present") { if ($snapshotExist) { Write-Verbose "Snapshots Exists for these set of VM's. No need of taking further action" return $true } else { Write-Verbose "Snapshots don't exists for these set of VM's in $resourcepoolname" return $false } } else { if ($snapshotExist) { Write-Verbose "Snapshots exist on these VM's; snapshots must be removed." return $false } else { Write-Verbose "Snapshots does not exist; nothing to remove." return $true } } } catch { $exception = $_ Write-Verbose "Error occurred while executing Test-TargetResource function" while ($exception.InnerException -ne $null) { $exception = $exception.InnerException Write-Verbose $exception.message } } return $false } #Test-TargetResource -vcentername test -credentialfile test -resourcepoolname vinith* #Test-TargetResource -vcentername "vc-itops-lab.vmware.com" -credentialfile "C:\evoraildocs\SecureCredentials.xml" -resourcepoolname vinith* function Set-TargetResource { param ( [parameter(Mandatory)] [string] $vcentername, [parameter(Mandatory)] [string] $resourcepoolname, [Parameter(Mandatory)] [PSCredential]$DomainAdministratorCredential, [ValidateSet('Present','Absent')] [string] $Ensure = 'Present' ) try { #Addpssnapin VMware.VimAutomation.Core Add-PSSnapin VMware.VimAutomation.Core #$MyCredentials= Import-Clixml $credentialfile Write-Verbose "Connecting to vCenter $vcentername" Connect-VIServer $vcentername -Credential $DomainAdministratorCredential if ($Ensure -eq "Present") { Write-Verbose "Creating Snapshots for all vm's in resource pool $resourcepoolname" $sn = Get-ResourcePool $resourcepoolname | Get-VM |% { New-Snapshot -Name $_.name -VM $_.name } Write-Verbose "Snapshots created for all vm's in resourcepool $resourcepoolname " Write-Verbose "$sn" } else { Write-Verbose "Removing Snapshots for all vm's in resource pool $resourcepoolname" Get-ResourcePool $resourcepoolname | Get-VM | Get-Snapshot | % {Remove-Snapshot $_ -Confirm:$false} Write-Verbose "All snapshots for VM's in resourcepool '$resourcepoolname' have been removed" } } catch { $exception = $_ Write-Verbose "An error occurred while running Set-TargetResource function" while ($exception.InnerException -ne $null) { $exception = $exception.InnerException Write-Verbose $exception.message } } } #Set-TargetResource -vcentername test -credentialfile test -resourcepoolname vinith* function Get-TargetResource { [OutputType([System.Collections.Hashtable])] param ( [parameter(Mandatory)] [string] $vcentername, [parameter(Mandatory)] [string] $resourcepoolname, [Parameter(Mandatory)] [PSCredential]$DomainAdministratorCredential ) Add-PSSnapin VMware.VimAutomation.Core #$MyCredentials= Import-Clixml $credentialfile Write-Verbose "Connecting to vCenter $vcentername" Connect-VIServer $vcentername -Credential $DomainAdministratorCredential $snapshotExist = Get-ResourcePool $resourcepoolname | Get-VM | Get-Snapshot $Configuration = @{ Snapshots = $snapshotExist } Write-Verbose "Checking if Snapshots exists for the VM's in resource pool " try { #Addpssnapin VMware.VimAutomation.Core $snapshotExist = Get-ResourcePool $resourcepoolname | Get-VM | Get-Snapshot if ($snapshotExist) { Write-Verbose "Snapshots exist for these VM's" $Configuration.Add('Ensure','Present') } else { Write-Verbose "Snapshots does not exist for these VM's" $Configuration.Add('Ensure','Absent') } return $Configuration } catch { $exception = $_ Write-Verbose "Error occurred while running Get-TargetResource function" while ($exception.InnerException -ne $null) { $exception = $exception.InnerException Write-Verbose $exception.message } } } #Get-TargetResource -vcentername test -credentialfile test -resourcepoolname vinith* Export-ModuleMember -Function *-TargetResource |