What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Snapshot State vCenter 6.5

Votes:

0

Hi Guys

i'm trying to get the Snapshot state of my VMs monitored in PRTG. We just upgraded our ESXi Environment to 6.5.

Before the Upgrade everything worked fine. But now i'm getting Errors with the old Skript. I had to upgrade PowerCLI from Version 4 to Version 6.5. So i found this article https://kb.paessler.com/en/topic/29313-vmware-snapshots and tried the Skript. Unfortunately "Add-PSSnapin" doesnt work anymore.

So i changed the Line to "Get-Module -Name VMware* -ListAvailable | Import-Module". If i run it trough PowerShell everything workes fine and i'm able to Connect with the vCenter.

But in my Custom Sensor i get the Error "'connect-viserver' is not recognized as a cmdlet, function, operable program, or script file." It seems the Modules arent loaded yet.

If i change the Line to "Import-Module -Name VMware.VimAutomation.Core", then the following Error appears:

Import-Module : Das erforderliche Modul "VMware.VimAutomation.Cis.Core" ist nicht geladen. Laden Sie das Modul, oder entfernen Sie das Modul aus "RequiredModules" in der Datei "C:\Program Files (x86)\VMware\Infrastructure\Po werCLI\Modules\VMware.VimAutomation.Core\VMware.VimAutomation.Core.psd1

It means "The required Module "VMware.VimAutomation.Cis.Core" isnt loaded. Load the Module or remove the Module from the "RequiredModules" in the File VMware.VimAutomation.Core.psd1"

I'm grateful for every hint, i tried already a lot but i'm not very familiar with PowerShell.

Thanks!

esxi powercli powershell powershell-module snapshot vcenter

Created on Dec 6, 2017 10:55:27 AM



20 Replies

Votes:

1

So i did it by my self. For my Problem above the solution was to put the Modules into Global Scope.

I also changed the Skript for our Monitoring where each VM have its own Sensor. Now it counts how much snapshots per each VM exists.

If you want to use this Skript, you have to put the name of the VM in the Settings/Parameters of the Sensor.

IP_vCenter_or_ESX_Host https '%linuxuser' '%linuxpassword' 'Cluster_name' * 'vm_name'

The final Script:

#---------------------------------------------------------------------------------------------
# Get the number of VM snapshots on ESX host or vCenter
#
# This script requires the installation of the VMware Infrastructure (VI) Toolkit for Windows.
# 
# Args[0]:Server
# Args[1]:Protocol (HTTP/HTTPS)
# Args[2]:User
# Args[3]:Password
# Args[4]:Location (VI container(s), e.g. folders, datacenters, clusters). 
#		  Enclose in ' ' if location name contains spaces.
# Args[5]:VM power state (PoweredOn, PoweredOff, * for all)
#
# Example 1: Snapshot count for single DRS cluster, only powered-on VMs
# 10.23.112.235 https Administrator pass01 'My DRS Cluster' 'PoweredOn'
#
# Example 2: multiple datacenters, all VMs
# 10.23.112.235 https Administrator pass01 'My Data Center 1','My Data Center 2' '*'
# 
#---------------------------------------------------------------------------------------------


Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Sdk" -Scope Global 
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Common" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Cis.Core" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Core"



$global:textvar = "OK"

$server=Connect-VIServer -Server $Args[0] -Protocol $Args[1] -User $Args[2] -Password $Args[3]>$Null
$powerstate = [string]$Args[5]
$vm = [string]$Args[6]
$snapshot_count = 0

Get-Snapshot -VM $vm | 
ForEach-Object {
	$snapshot_count = $snapshot_count+1
}



$x=[string]$snapshot_count+":"+$global:textvar




write-host "<prtg>"
write-host "<result>"
write-host "<channel>Snapshots</channel>"
write-host "<value>"
write-host $snapshot_count
write-host "</value>"
write-host "<LimitMode>1</LimitMode>"
write-host "<LimitMaxError>3</LimitMaxError>"
write-host "<LimitMaxWarning>2</LimitMaxWarning>"
write-host "</result>"
write-host "</prtg>"

if you see anything i could do better, please let me know. i usually dont use PowerShell or PowerCLI.

Created on Dec 6, 2017 3:23:46 PM

Last change on Dec 7, 2017 9:24:41 AM by  Torsten Lindner [Paessler Support]



Votes:

0

We reworked the script so that we get a channel per VM.

#---------------------------------------------------------------------------------------------
# Get the number of VM snapshots on ESX host or vCenter
#
# This script requires the installation of the VMware Infrastructure (VI) Toolkit for Windows.
# 
# Args[0]:Server
# Args[1]:Protocol (HTTP/HTTPS)
# Args[2]:User
# Args[3]:Password
# Args[4]:Location (VI container(s), e.g. folders, datacenters, clusters). 
#		  Enclose in ' ' if location name contains spaces.
# Args[5]:VM power state (PoweredOn, PoweredOff, * for all)
#
# Example 1: 
# IP_vCenter_or_ESX_Host https '%linuxuser' '%linuxpassword' 'Cluster_name' * 'vm_name'
#
#---------------------------------------------------------------------------------------------


Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Sdk" -Scope Global 
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Common" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Cis.Core" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Core"



$global:textvar = "OK"

$server=Connect-VIServer -Server $Args[0] -Protocol $Args[1] -User $Args[2] -Password $Args[3]>$Null
$powerstate = [string]$Args[5]
#$vm = [string]$Args[6]
$Servers = Get-VM 
write-host "<prtg>"
foreach ($vm in $Servers)
{
$snapshot_count = 0

Get-Snapshot -VM $vm | 
ForEach-Object {
	$snapshot_count = $snapshot_count+1
}



$x=[string]$snapshot_count+":"+$global:textvar





write-host "<result>"
write-host "<channel>$VM</channel>"
write-host "<value>"
write-host $snapshot_count
write-host "</value>"
write-host "<LimitMode>1</LimitMode>"
write-host "<LimitMaxError>1</LimitMaxError>"
write-host "<LimitMaxWarning>0</LimitMaxWarning>"
write-host "</result>"

}

write-host "</prtg>"

Created on Jan 24, 2018 2:38:46 PM

Last change on Jan 24, 2018 3:06:32 PM by  Torsten Lindner [Paessler Support]



Votes:

0

Hi,

could anyone please give me a hint how to get the Modules the script tries to import? I can't find a download for them :-(

VMware.VimAutomation.Sdk VMware.VimAutomation.Common VMware.VimAutomation.Cis.Core VMware.VimAutomation.Cor

Thanks....

Created on Apr 23, 2018 7:45:48 AM



Votes:

0

The modules can be downloaded here. Note that you need a MyVMWare account to obtain it.


Kind regards,
Stephan Linke, Tech Support Team

Created on Apr 23, 2018 11:06:22 AM by  Stephan Linke [Paessler Support]



Votes:

0

Hi @ll, I´ve a question, I want you the PS-Script from top of the side but I´m not a Powershell Specialist, so could everyone be so nice and can me explain where and how I have to insert these values in the script?

Args[0]:Server

  1. Args[1]:Protocol (HTTP/HTTPS)
  2. Args[2]:User
  3. Args[3]:Password
  4. Args[4]:Location (VI container(s), e.g. folders, datacenters, clusters).
  5. Enclose in ' ' if location name contains spaces.
  6. Args[5]:VM power state (PoweredOn, PoweredOff, * for all)

I tried something but nothing worked... THX

Created on Jun 27, 2018 10:41:54 AM



Votes:

0

Hi JaBe,

You'll need to enter these parameters in the parameter field of the custom sensor when creating it:

'https' 'username' 'password' '*'

$Args[4] is seemingly not used within the script, hence I omitted it.


Kind regards,
Stephan Linke, Tech Support Team

Created on Jun 27, 2018 10:59:01 AM by  Stephan Linke [Paessler Support]



Votes:

0

Good Morning,

I put the Schript on our Probe in following folder C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\ and created a custom sensor with the needed Parameters. After the check ran following error occured, google can´t help me... any Idea how to fix it?

Antwort nicht wohlgeformt: "(Connect-VIServer : Die Datei oder Assembly "VMware.VimAutomation.Logging.SoapInterceptor, Version=1.0.0.314, Culture=neutral, PublicKeyToken=null" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden. In C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\Snap_test.ps1:32 Zeichen:9 + $server=Connect-VIServer -Server $Args[0] -Protocol $Args[1] -User $Args[2] -Pas ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [Connect-VIServer], FileNotFou ndException + FullyQualifiedErrorId : System.IO.FileNotFoundException,VMware.VimAutoma tion.ViCore.Cmdlets.Commands.ConnectVIServer Get-Snapshot : Die Datei oder Assembly "VMware.VimAutomation.Logging.SoapInterceptor, Version=1.0.0.314, Culture=neutral, PublicKeyToken=null" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden. In C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\Snap_test.ps1:37 Zeichen:1 + Get-Snapshot -VM $vm | + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-Snapshot], FileNotFoundEx ception + FullyQualifiedErrorId : System.IO.FileNotFoundException,VMware.VimAutoma tion.ViCore.Cmdlets.Commands.GetSnapshot <prtg> <result> <channel>Snapshots</channel> <value> 0 </value> <LimitMode>1</LimitMode> <LimitMaxError>3</LimitMaxError> <LimitMaxWarning>2</LimitMaxWarning> </result> </prtg> )" (Code: PE132)

Created on Jun 28, 2018 7:32:35 AM

Last change on Jun 28, 2018 7:48:49 AM by  Stephan Linke [Paessler Support]



Votes:

0

You need to put it in the EXEXML directory, since it's outputting XML. Otherwise, is the following requirement installed:


This script requires the installation of the VMware Infrastructure (VI) Toolkit for Windows.





Kind regards,
Stephan Linke, Tech Support Team

Created on Jun 28, 2018 7:50:06 AM by  Stephan Linke [Paessler Support]



Votes:

0

Hi Stephan, the VI Toolkit is installed on the Probe. Could it be that the custom sensor is missconfigured and if yes, which settings are right?

Created on Jun 28, 2018 9:30:02 AM



Votes:

0

That I'm unsure of. Is the script itself located on a remote probe?

Created on Jun 28, 2018 10:06:56 AM by  Stephan Linke [Paessler Support]



Votes:

0

Yes, it´s located on dedicated Windows 7 Professional Machine which act as Probe, the Script is in Folder: C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\

Created on Jun 28, 2018 11:50:11 AM



Votes:

0

Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Sdk" -Scope Global 
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Common" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Cis.Core" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Core"

What happens when you run these commands in a PowerShell, one by one?

Created on Jun 28, 2018 12:14:57 PM by  Stephan Linke [Paessler Support]



Votes:

0

It works without errors...

Created on Jun 29, 2018 6:36:59 AM



Votes:

0

When you modify the security context setting of the sensor so it uses the credentials of the parent device, does it work then? The script works when executed in plain PowerShell, right?


Kind regards,
Stephan Linke, Tech Support Team

Created on Jun 29, 2018 6:41:41 AM by  Stephan Linke [Paessler Support]



Votes:

1

Hi

I have been following this thread as I am working on a similar script. The problems was that I would only want the information on the machines with snapshots, and I do not want information on all the other machines as well. I have borrowed a lot from the script above and tweaked it a little. I am no expert in powershell or scripting, but I have tried.

#You need to install PowerCLI and Download VMware Infrastructure Toolkit for Windows
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Sdk" -Scope Global 
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Common" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Cis.Core" -Scope Global
Import-Module -Name "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules\VMware.VimAutomation.Core" 

$server=Connect-VIServer -Server $Args[0] -Protocol $Args[1] -User $Args[2] -Password $Args[3]>$Null #Arguments used as a parameter in PRTG example: vcenter.local https ntofab\User PASSWORD1

$data=Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-30)} #Count how many snapshots that are above x amount of days.
$count=$data.count
$VMdata=Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-30)} | Select VM | Out-String #Display which virtual machines that have the snapshots
$VM=$VMdata.ToString()

write-host "<prtg>"
	write-host "<result>"
	write-host "<channel>Old_snapshot</channel>"
	write-host "<value>$count</value>"
	write-host "</result>"
	write-host "<text>$VM</text>"
write-host "</prtg>"

I hope it can help other users as well.

Created on Aug 8, 2018 9:11:15 AM

Last change on Aug 8, 2018 9:48:27 AM by  Stephan Linke [Paessler Support]



Votes:

0

Hi Jacob,

Thanks for sharing! :)


Kind regards,
Stephan Linke, Tech Support Team

Created on Aug 8, 2018 9:48:58 AM by  Stephan Linke [Paessler Support]



Votes:

0

As a side note: You should always Disconnect-VIServer when using PowerCLI because Connect-VIServer allocates ressources on vCenter.

Like this:

try {
   $server = Connect-VIServer ...

   # your-code-here
} finally {
   if ($server) {
      $null = Disconnect-VIServer -Confirm:$false
   }
}

Created on Apr 29, 2020 8:53:57 AM



Votes:

0

Hi All, I'm not a script/powershell "guru", anyway I'm trying to use the Jacob variant of the script, I've installed PowerCLI and VItoolkit, if I try to execute in Powershell (row by row) I receive this error after execute the first command after vcenter connection:

PS C:\Users\administrator.xxxxxx> $VM=$VMdata.ToString()$data=Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-30)} At line:1 char:23 + $VM=$VMdata.ToString()$data=Get-VM | Get-Snapshot | Where {$_.Created ... + ~~~~~~~~~~~~ Unexpected token '$data=Get-VM' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

Thanks for help!

Created on Jun 9, 2020 1:03:55 PM



Votes:

0

Dear cipo80,

as custom scripts are not covers by support, the suggestion would be to manually run Get-VM to see if the command is available, and if yes, build the full command on the console step by step to see where an error occurs.

Created on Jun 10, 2020 1:23:12 PM by  Arne Seifert [Paessler Support]



Votes:

0

Hi All, I've found another script that working manually in my PS probe, it's published in the Paessler script world:

param(
    [string]$Server = "vc01.domain.com",
    [string]$Username = "[email protected]",
    [string]$Password = "SecurePassword",
    [string]$IgnoreVMNamesStartingWith = "replica",
    [int]$MinAgeInDays = 0
)

Try
{
    $conn = Connect-VIServer $Server -Username $Username -Password $Password -Force

    $Snapshots = Get-VM | Get-Snapshot | Select VM, Name,Description,@{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},@{Name="Creator";Expression={ Get-SnapshotCreator -VM $_.VM -Created $_.Created }},Created,@{Name="Days";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }} | where { $_.Days -ge $MinAgeInDays }

    $SnapshotsFiltered = @()
    Foreach ($Snapshot in $Snapshots)
    {
        If($IgnoreVMNamesStartingWith.Length -gt 0 -and $Snapshot.VM.ToString().StartsWith($IgnoreVMNamesStartingWith))
        {
            # Snapshot starts with ignored name. Skip entry.
        } else {
            $SnapshotsFiltered += $Snapshot
        }
    }

    Disconnect-VIServer $conn -Force -Confirm:$false

    if($SnapshotsFiltered.Length -eq 0)
    {
        Write-Host "0:No snapshots found with an age of at least $MinAgeInDays days."
        exit 0
    }

    $Result = $SnapshotsFiltered.Length.ToString() + ":"
    Foreach ($Snapshot in $SnapshotsFiltered)
    {
        $Result += "VM:" + $Snapshot.VM + ", Name:" + $Snapshot.Name + ", Description:" + $Snapshot.Description + ", Days: " + $Snapshot.Days + " ---- "
    }

    Write-Host $Result
    exit 1
}
catch
{
    Write-Host "-1:Error occurred while checking for snapshots:"$_.Exception.GetType().FullName, $_.Exception.Message. $_.Exception.Stacktrace
    exit 1
}

I Receive this error, seems to be the command connect-viserver not recognized, but from probe PS it's working.

Response not well-formed: "(-1:Error occurred while checking for snapshots: System.Management.Automation.CommandNotFoundException The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. . at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) )" (code: PE132)

Created on Jun 11, 2020 1:51:16 PM

Last change on Jun 11, 2020 5:30:53 PM by  Arne Seifert [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.