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://helpdesk.paessler.com/en/support/solutions/articles/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!
Article Comments
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>"
Jan, 2018 - Permalink
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....
Apr, 2018 - Permalink
The modules can be downloaded here. Note that you need a MyVMWare account to obtain it.
Kind regards,
Stephan Linke, Tech Support Team
Apr, 2018 - Permalink
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
- 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)
I tried something but nothing worked... THX
Jun, 2018 - Permalink
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
Jun, 2018 - Permalink
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)
Jun, 2018 - Permalink
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
Jun, 2018 - Permalink
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?
Jun, 2018 - Permalink
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\
Jun, 2018 - Permalink
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?
Jun, 2018 - Permalink
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
Jun, 2018 - Permalink
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.
Aug, 2018 - Permalink
Hi Jacob,
Thanks for sharing! :)
Kind regards,
Stephan Linke, Tech Support Team
Aug, 2018 - Permalink
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 } }
Apr, 2020 - Permalink
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!
Jun, 2020 - Permalink
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.
Jun, 2020 - Permalink
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 = "account@domain.com", [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)
Jun, 2020 - Permalink
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.
The final Script:
if you see anything i could do better, please let me know. i usually dont use PowerShell or PowerCLI.
Dec, 2017 - Permalink