I found a script and added it as an exe/advanced sensor as directed, however the sensor returns the following error:
XML: The returned XML does not match the expected schemas. (code: PE233) -- JSON: The returned JSON does not match the expected structure (Invalid JSON.). (code: PE231)
Below is the script
Import-Module Hyper-V $VMReplication = Get-VMReplication | select Name,Health,State if (($VMReplication).count -lt 1) { Write-Host 1, ": No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated!" Exit 4 } $ErrMessage = " " $Errors = 0 $Warning = 0 $Critical = 0 foreach ($Name in $VMReplication) { if ($Name.Health -Contains "Normal" -and $Name.State -Contains "Replicating") { $stat = 0 } elseif ($Name.Health = "Warning") { $stat = 1 $Warning = $Warning + 1 } elseif ($Name.Health = "Critical") { $stat = 1 $Critical = $Critical + 1 } if ($stat -gt 0) { $VM = $Name.Name $Health = $Name.Health $Status = $Name.State $ErrMessage = $ErrMessage, $VM, " " , $Health , " (", $Status, ") " $Errors = $Errors + 1 } } If ($Errors -eq 0) { Write-Host 0, ":OK", "Replication is working fine for", ($VMReplication).count, "of", (Get-VM).count, "VMs" Exit 0 } else { Write-Host $Errors, ":", $ErrMessage Exit 2 }
Article Comments
Ao realizar as ações recomendadas, deu a seguinte mensagem:
Erro do conteúdo:
The specified module 'Hyper-V' was not loaded because no valid module file was found in any module directory. At C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\replication_sensor.ps1:1 char:1 + Import-Module Hyper-V + ~~~~~~~~~~~~ (código: PE024)
Sep, 2021 - Permalink
Hello,
Thank you for the update.
According to the message you get, the Hyper-V module is not installed and as a consequence the script can't import it. Therefore, I invite you to open a PowerShell console (32bit) on the PRTG server and then make sure that the module is installed there.
To check if the PowerShell console opened is in 32bit you can use "[System.Environment]::Is64BitProcess" which should return the value "False".
Regards.
Sep, 2021 - Permalink
Olá!
Muito obrigado! Deu certo. Porém uma pergunta, no meu ambiente na mesma rede existe vários servidores com hyper-v replicando vms, porém na rede o probe está instalado somente em uma máquina, para selecionar outros servidores necessitaria instalar em cada servidor o probe? Ou nesse mesmo script tem como selecionar qual vm buscar? Poderia me informar qual parametro inserir no script, por gentileza?
Grato!
Sep, 2021 - Permalink
Hello,
Thank you very much for the update. Glad to hear that it works now!
Regarding your question, you do not have to install a remote probe on each server to monitor the VM replication there. Instead, you can modify the script to execute the code remotely by using the cmdlet Invoke-Command. This will allow you to create EXE sensors for each of your machines.
Note that to execute the code remotely you will need to pass the Windows credentials as well as address (IP/DNS) defined in the devices settings in PRTG. To do so, you can use the placeholders %host, %windowsuser and %windowspassword as follows:
"%host" "%windowsuser" "%windowspassword"
You also need to add the following lines at the beginning of the script to get these parameters:
Param ( [string]$host= "", [string]$winuser = "", [string]$winpass= "" )
Then, to use the cmdlet Invoke-Command you need to create a credential object with the values of $winuser and $winpass. I invite you to have a look at the following article for further information: https://adamtheautomator.com/powershell-get-credential/#Create_a_Credential_without_a_Prompt
$credential = New-Object System.Management.Automation.PSCredential ($winuser, $winpass)
Finally, execute the code expected to run on the machine within a scriptblock as follows:
Invoke-Command -ComputerName $host -Credential $credential -ScriptBlock { # Your code here (will be executed on the target device) }
Please, note that we do not provide official support for it. Therefore, if there is modifications required according to your environment, I won't be able to provide support for it.
Sep, 2021 - Permalink
Boa noite!
Muito obrigado pelo retorno, me perdoe o abuso, compreendo que que não dão suporte oficial nessa parte, mas realmente estou encontrando dificuldades e acabou não dando certo o procedimento, poderia por gentileza me exemplificar como ficaria as orientações no script? Poderia me passar um modelo no script, de como ficaria?
Import-Module Hyper-V $VMReplication = Get-VMReplication | select Name,Health,State if (($VMReplication).count -lt 1) { Write-Host 1, ": No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated!" Exit 4 } $ErrMessage = " " $Errors = 0 $Warning = 0 $Critical = 0 foreach ($Name in $VMReplication) { if ($Name.Health -Contains "Normal" -and $Name.State -Contains "Replicating") { $stat = 0 } elseif ($Name.Health = "Warning") { $stat = 1 $Warning = $Warning + 1 } elseif ($Name.Health = "Critical") { $stat = 1 $Critical = $Critical + 1 } if ($stat -gt 0) { $VM = $Name.Name $Health = $Name.Health $Status = $Name.State $ErrMessage = $ErrMessage, $VM, " " , $Health , " (", $Status, ") " $Errors = $Errors + 1 } } If ($Errors -eq 0) { Write-Host 0, ":OK", "Replication is working fine for", ($VMReplication).count, "of", (Get-VM).count, "VMs" Exit 0 } else { Write-Host $Errors, ":", $ErrMessage Exit 2 }
Grato!
Sep, 2021 - Permalink
Hi,
Here is the script slightly modified, of course it requires to be tested and probably modified to work properly:
Param ( [string]$host= "", [string]$winuser = "", [string]$winpass= "" ) $credential = New-Object System.Management.Automation.PSCredential ($winuser, $winpass) $VMReplication = Invoke-Command -ComputerName $host -Credential $credential -ScriptBlock { Import-Module Hyper-V Get-VMReplication | select Name,Health,State } if (($VMReplication).count -lt 1) { Write-Host 1, ": No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated!" Exit 4 } $ErrMessage = " " $Errors = 0 $Warning = 0 $Critical = 0 foreach ($Name in $VMReplication) { if ($Name.Health -Contains "Normal" -and $Name.State -Contains "Replicating") { $stat = 0 } elseif ($Name.Health = "Warning") { $stat = 1 $Warning = $Warning + 1 } elseif ($Name.Health = "Critical") { $stat = 1 $Critical = $Critical + 1 } if ($stat -gt 0) { $VM = $Name.Name $Health = $Name.Health $Status = $Name.State $ErrMessage = $ErrMessage, $VM, " " , $Health , " (", $Status, ") " $Errors = $Errors + 1 } } If ($Errors -eq 0) { Write-Host 0, ":OK", "Replication is working fine for", ($VMReplication).count, "of", (Get-VM).count, "VMs" Exit 0 } else { Write-Host $Errors, ":", $ErrMessage Exit 2 }
Sep, 2021 - Permalink
Bom dia!
Obrigado!
Porém está dando o seguinte erro:
Resposta não formada corretamente: "(Param ( [string]$host= "", [string]$winuser = ", [string]$winpass= "" ) $credential = New-Object System.Management.Automation.PSCredential ($winuser, $winpass) $VMReplication = Invoke-Command -ComputerName $host -Credential $credential -ScriptBlock { Import-Module Hyper-V Get-VMReplication | select Name,Health,State } if (($VMReplication).count -lt 1) { Write-Host 1, ": No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated!" Exit 4 } $ErrMessage = " " $Errors = 0 $Warning = 0 $Critical = 0 foreach ($Name in $VMReplication) { if ($Name.Health -Contains "Normal" -and $Name.State -Contains "Replicating") { $stat = 0 } elseif ($Name.Health = "Warning") { $stat = 1 $Warning = $Warning + 1 } elseif ($Name.Health = "Critical") { $stat = 1 $Critical = $Critical + 1 } if ($stat -gt 0) { $VM = $Name.Name $Health = $Name.Health $Status = $Name.State $ErrMessage = $ErrMessage, $VM, " " , $Health , " (", $Status, ") " $Errors = $Errors + 1 } } If ($Errors -eq 0) { Write-Host 0, ":OK", "Replication is working fine for", ($VMReplication).count, "of", (Get-VM).count, "VMs" Exit 0 } else { Write-Host $Errors, ":", $ErrMessage Exit 2 } )" (código: PE132)
Na parte aonde insiro os dados do servidor, inseri desta forma:
Param ( [string]$host= "nome do servidor", [string]$winuser = "login do usuário", [string]$winpass= "senha" )
É desta forma mesmo?
Grato!
Sep, 2021 - Permalink
Hello,
The values passed to the parameters are correct however you do not have to write them in the script. Regarding that matter, please make sure to remove them before publishing your post. We might not see them and therefore they would be visible to everyone.
In the script I provided, you do not have to write the credentials in the script. I invite you to use to add the following placeholders in the Parameter field of the EXE sensor:
"%host" "%windowsuser" "%windowspassword"
Then, before using the script with the sensor I invite you to make sure that it works properly and return a value following the format below:
value:message
If it doesn't, then there is an issue something that you need to troubleshoot. To avoid any issue with the module Hyper-V, make sure that it is installed on each server you would like to monitor with that script.
I have modified the script to return the value -1 as well as the error encountered in the sensor message field:
Param ( [string]$host= "", [string]$winuser = "", [string]$winpass= "" ) $credential = New-Object System.Management.Automation.PSCredential ($winuser, $winpass) try{ $VMReplication = Invoke-Command -ComputerName $host -Credential $credential -ScriptBlock { Import-Module Hyper-V Get-VMReplication | select Name,Health,State } if (($VMReplication).count -lt 1) { Write-Host "1:No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated!" Exit 4 } $ErrMessage = " " $Errors = 0 $Warning = 0 $Critical = 0 foreach ($Name in $VMReplication) { if ($Name.Health -Contains "Normal" -and $Name.State -Contains "Replicating") { $stat = 0 } elseif ($Name.Health = "Warning") { $stat = 1 $Warning = $Warning + 1 } elseif ($Name.Health = "Critical") { $stat = 1 $Critical = $Critical + 1 } if ($stat -gt 0) { $VM = $Name.Name $Health = $Name.Health $Status = $Name.State $ErrMessage = $ErrMessage, $VM, " " , $Health , " (", $Status, ") " $Errors = $Errors + 1 } } If ($Errors -eq 0) { Write-Host 0, ":OK", "Replication is working fine for", ($VMReplication).count, "of", (Get-VM).count, "VMs" Exit 0 } else { Write-Host $Errors, ":", $ErrMessage Exit 2 } }catch { Write-Host "-1:$($_.exception.message) At line $($_.InvocationInfo.ScriptLineNumber)" }
Regards.
Sep, 2021 - Permalink
Boa tarde!
Em um dos servidores hyper-v está dando esse erro ao adicionar o sensor:
Erro do conteúdo: No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated! (código: PE024)
Grato
Sep, 2021 - Permalink
Hello,
I'm afraid that I can't provide further support here, the script returns this error message when $VMReplication is null.
if (($VMReplication).count -lt 1) { Write-Host "1:No data returned from Get-VMReplication! Either no VMs are replicating, or have not elevated!" Exit 4 }
Therefore, I can only invite you to execute the cmdlets in the script block manually on the target server and check if that's the case.
Regards.
Sep, 2021 - Permalink
Hello,
Thank you for your message.
According to the output of the script, I invite you to use an EXE/Script sensor instead of the EXE/Script Advanced as the latter indeed expect to receive a JSON whereas the sensor returns the following output "value:message".
You can also replace the end of the script with the following instead of using many strings:
If you have questions, let us know.
Regards.
Sep, 2021 - Permalink