Converting a VHDx file to Windows Image File and back

 

Hardware prerequisites: N/A

Software prerequisites: N/A

Things to keep in mind:

  • We must ensure if we are going to reinstall that the license key is valid for this purpose.

Results expected:

  • We should be able to convert any VHD or VHDx file into an installable WIM image.

Knowledges required:

  • Basic Powershell
  • Basic Windows Server Setup

Versions used:

  • PowerShell 4.0

Sources: N/A

Extra Downloads: N/A

OBJECTIVE

We should be able to convert any VHD or VHDx file into an installable WIM image. This way we could export any virtual drive to an installable WIM which we can reuse within Microsoft Windows Deployment Services o via a DVD/USB drive to customize our installations. It is very useful for unattended installs, or simply to keep updated and patched your installation media.

STEPS

  1. Mounting a Windows image

     

    Where we have to provide the path to the hard drive , and a path where we mount it (\temp, which we must create priously).

  2. Now we will modify it, and when we are happy with it, convert back to a new WIM image, which we can use to generate the VHDx via installation:

    Here we can see how we use the tempo folder where we mounted the original VHDx to convert it again into a new WIM image, including Name, Description and Path to the new WIM file.

    In this case (I always do it) as a good practice, I verify the WIM file to ensure all runs smoothly.

  3. Once we have finished with the VHD ir VHDx file, we simply unmount it,

    We have in my case, just to use the proper cmdlet letting it know that we had in mounted in our temp folder, and adding that we discard any possible changes made to the drive, to ensure there will be no problems when we reuse it in the VM. Obviously, the VM must be powered off.

TESTING THE PROCEDURE

 

Noe is the moment simply to import r replace the existing image in Microsoft Windows Deployment Services, or create a new ISO or USB installation media, replacing the install.wim file located within the .\sources folder of the media. As simple as that. A good way to keep your media always patched.

And again we are done, is this simple!

Tanks for reading this, hope it can result helpful! And please post if you have doubts or if know a way better, I’m sure there’s always a possible improvement!

P.

Automating Pause and Resume in Paessler PRTG for monitored Windows Boxes

 

Hardware prerequisites: N/A

Software prerequisites: https://www.es.paessler.com/prtg/requirements

Things to keep in mind:

  • I have tested on an AD environment, with a DNS properly setup and updated.
  • We might have a PRTG user with enough permissions to do the task, with the API passhash (See sources)

Results expected:

-When you reboot a machine it may pause and resume automatically itself in monitoring systems.

Knowledges required:

  • Basic Paessler PRTG
  • Average Powershell scripting level
  • Average AD and GPO

Versions used:    

  • PowerShell 4.0
  • Windows Server 2012R2
  • Paessler Free Edition above 15.x

Sources:

Extra Downloads: N/A

OBJECTIVE

It is always very disturbing that when you have to do any maintenance (or when others do it) alerting system is triggered without any needs, which may lead that people who shouldn´t be disturbed, is, and that statistics of SLA´s and availability on production systems are inaccurate, just due to these tiny details. It gives an image of “amateurism” in some way, which is highly undesirable.

Sometimes, is just a matter of cosmetics, and others is a matter of keeping your rates accurate for the year global results of availability, but in any case, if can be automated, is a waste of time thinking about it. We will show how we can avoid forgetting about this to prevent false positives and unneeded alerts. We will automate in PRTG the pausing and resuming of boxes when they are shut down or rebooted.

STEPS

  1. First of all is to define the variables with the monitoring service you may be wanting to connect to, and the device IP address (ideally you should be collecting it from the NIC within your box, so this script can be reused among all machines you need):

    It could be useful in case you have more than one environment to manage, or setup install.

  2. Next step is to collect all devices from PRTG connecting to the API and using our user passhash:

    In three steps:

  • We create the web client connection
  • Then we export via PRTG API the devices list. Can be very usefuyl the query builder which is included in the API itself to generate these acrtions
  • After that we import the file into memory with the format I have used, but others are available.

At this point we have all PRTG monitored devices in our script.

  1. Now is the moment to locate all the device IDs which are connected to our machine IP:

    In two steps we generate the array where we can collect all ID´s forom the csv we imported in memory.

    Then we filter existing ID´s to look for the one we need. It uses to happen that a device is attached to more than one sensor, or monitored using different IPs. If this is the case can be interesting to automate it with a ForEach statement when we action the pause or resume.

  2. Now it the moment to execute the task with all the information we have collected:

    At this point all IDs we might have passed onto the action have been paused in PRTG console.

    We have two options:

    1. Action=0 : This will mean we pause those ID´s
    2. Action=1 : We resume monitoring those ID´s

TESTING THE PROCEDURE

We can check the status of the machine in the ajaxs console just refresdhing and we will see:

That the status has changed from green to blue, meaning has been correctly paused. Doing the opposite whe should need to see it in its status at the moment of resuming.

And we are done!

Tanks for reading this, hope it can result helpful! And please post if you have doubts or if know a way better, I’m sure there’s always a possible improvement!

P.

Obtaining NICs usage metrics with Powershell from a Physical Box for a PRTG Sensor

Hardware prerequisites: https://www.es.paessler.com/prtg/requirements
Software prerequisites: https://www.paessler.com/prtg/download
Things to keep in mind:

  • This article applies to my setup, depending on yours the names of the sensors may change.
  • In this sample I used the counters for a Spanish version of the target OS, keep in mind this might change.
Results expected:

  • Monitor with a single sensor a single machine // obtaining data for physical NICs.
Knowledges required:

  • Basic Paessler PRTG
  • Average Powershell scripting level
Versions used

  • PowerShell 4.0
  • Windows Server 2012R2
  • Paessler Free Edition above 15.x
Sources:

  • Paessler Knowledge Base and Installation API documentation.
Extra Downloads: N/A
OBJECTIVE

Paessler PRTG is a great tool for enterprise monitoring, which includes many built in features which make it a really powerful piece of software. In this article will describe how to monitor Windows machines using a single sensor per box (let’s remember Paessler PRTG is licensed by number of sensors, which may be really useful for testing laboratories, or in small deployments, like my case).

The free edition is fully operational, with the only limitation to 100 sensors.

Weal already covered in a previous post how to monitor a machine with a single PRTG sensor, but we missed how to collect data from Physical boxes, which may have different names.

Doing so is the target of this one.

STEPS
  1. We might need in this case replace the code we used to have for this one, as in my case I know my Physical boxes, using locally as a local admin the cmdlet Get-Counter you will know what you may need. As usual, consistency in hardware will help us to achieve this, as will help us to make easier to find the proper counters:
$BytesTotalSec
= @(Get-counter
-ComputerName$Name-ListSet “Interfaz de Red”).PathsWithInstances |
Where-Object {($_
-Like “\\$Name\Interfaz de red(Q*\Total de bytes/s”)`

-or ($_
-Like “\\$Name\Interfaz de red(C*\Total de bytes/s”)`

-or ($_
-Like “\\$Name\Interfaz de red(R*\Total de bytes/s”)`

-or ($_
-Like “\\$Name\Interfaz de red(B*\Total de bytes/s”)
}
$BytesInSec
= @(Get-counter
-ComputerName$Name-ListSet “Interfaz de Red”).PathsWithInstances |
Where-Object {($_
-Like “\\$Name\Interfaz de red(Q*\bytes recibidos/s”)`

-or ($_
-Like “\\$Name\Interfaz de red(C*\bytes recibidos/s”)`


-or ($_
-Like “\\$Name\Interfaz de red(R*\bytes recibidos/s”)`


-or ($_
-Like “\\$Name\Interfaz de red(B*\bytes recibidos/s”)

}

$BytesOutSec
= @(Get-counter
-ComputerName$Name-ListSet “Interfaz de Red”).PathsWithInstances |
Where-Object {($_
-Like “\\$Name\Interfaz de red(Q*\bytes enviados/s”)`


-or ($_
-Like “\\$Name\Interfaz de red(C*\bytes enviados/s”)`


-or ($_
-Like “\\$Name\Interfaz de red(R*\bytes enviados/s”)`


-or ($_
-Like “\\$Name\Interfaz de red(B*\bytes enviados/s”)

}

In the sample we can see that, according to my setup, I want to gather Total Bytes, Received Bytes and Sent Bytes for any cards starting by C*, R* and B*.

TEST

Now using this, or your customized version of the script, according to your setup, you will be able to see the final status, as we could see in the previous post:

And again, we are done!! Now you can get all your NICs and machine data using a single sensor. Is a matter of playing with it, you can get all information you think can be useful for you.

Tanks for reading this, hope it can result helpful! And please post if you have doubts or if know a way better, I’m sure there’s always a possible improvement!

P.

Monitoring a Windows Server using PowerShell and Paessler PRTG using a single sensor

Hardware prerequisites: https://www.es.paessler.com/prtg/requirements
Software prerequisites: https://www.paessler.com/prtg/download
Things to keep in mind:

  • Using PowerShell sensors may increase the CPU consumption within the monitoring machine.
  • In this sample I used the counters for a Spanish version of the target OS, keep in mind this might change.
Results expected:

  • Monitor with a single sensor a single machine
Knowledges required:

  • Basic Paessler PRTG
  • Average Powershell scripting level
Versions used:

  • PowerShell 4.0
  • Windows Server 2012R2
  • Paessler Free Edition above 15.x
Sources:

Extra Downloads: N/A
OBJECTIVE

Paessler PRTG is a great tool for enterprise monitoring, which includes many built in features which make it a really powerful piece of software. In this article will describe how to monitor Windows machines using a single sensor per box (let’s remember Paessler PRTG is licensed by number of sensors, which may be really useful for testing laboratories, or in small deployments, like my case).

The free edition is fully operational, with the only limitation to 100 sensors.

STEPS
  1. First thing to know is what do we want to know from a Windows Server to know if is healthy or not? The most typical things which are common to all platforms and environments could be these:
    1. CPU
    2. Ram Usage
    3. Disk Usage
    4. Bandwidth usage
  2. We will use a parameter passed to the script, using the single IP we know of the box we are going to monitor:
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$IP
)
  1. Let’s get basic connectivity via PowerShell, with a simple ping:
$Ping
=
Test-Connection -ComputerName $IP -Count 1
  1. Let’s then get the CPU Usage
$CPU
= ([math]::round(((Get-counter
-Counter
“\Procesador(_Total)\% De tiempo de Procesador”
-SampleInterval
1
-ComputerName
$IP).CounterSamples.CookedValue),2))

You have to provide previously the $IP variable, and we get the round of the value in order to provide more clan values for PRTG sensor.

  1. Let’s get the Physical RAM
$PercPhysRam = (([math]::round(((Get-counter -Counter “\Memoria\Mbytes disponibles” -SampleInterval 1 -ComputerName $IP).Readings.SubString(46)),2))*100/`
([math]::round((((Get-WmiObject -Class Win32_ComputerSystem -ComputerName $IP | Select TotalPhysicalMemory).TotalPhysicalMemory)/1048576),2)))

This will provide us the percentage of total ram of the box, regardless the quantity installed or assigned via Hyper-Visor.

  1. Let’s get the Virtual RAM
$VRamTotal =
0
$VRamSize = @(Get-WmiObject
Win32_PageFileusage
-ComputerName $IP |
Select-Object
AllocatedBaseSize)
$VRamSize |
ForEach {$VRamTotal += $_.AllocatedBaseSize}

$VRamUsed =
0

$VRamUsedAmnt = @(Get-WmiObject
Win32_PageFileusage
-ComputerName $IP |
Select-Object
PeakUsage)

$VRamUsedAmnt |
ForEach {$VRamUsed += $_.PeakUsage}

Here we get the total virtual ram assigned, and the used one. Now we calculate the percentage used:

$PercVram = ([math]::round((($VRamUsed*100)/$VRamTotal),2))
  1. Let’s get the logical disks information
$Ldisks = Get-WmiObject -Class WIn32_Logicaldisk -ComputerName $Name | Where-Object {($_.DriveType -ne 5) -and ($_.DeviceId -ne “A:”)}

In this case what we are looking is for logical disks which are not removable (-ne 5) and which are not floppy drives (still even virtually could be applied in some older versions of boxes). This would provide us all information about any logical and fixed drive attached to the machine.

Having all the drives in one step helps us in case that drive assignment is different among our servers: if we have boxes with two and other with three or four, they should be automatically cached in the moment of the first data acquisition. But also has an issue, as data appears as channels within the same sensor, if the number of fixed drives changes to LESS we should need to read the sensor, losing the historical. We will see afterwards.

  1. Let’s get the bandwidth information we can get:
$BytesSecTotalNIC1
=
[math]::round((((Get-counter
-Counter “\interfaz de red(adaptador de red de microsoft hyper-v)\total de bytes/s” -SampleInterval
1
-ComputerName
$IP).CounterSamples.CookedValue)/1024),2)
$BytesSecInNIC1
=
[math]::round((((Get-counter
-Counter “\interfaz de red(adaptador de red de microsoft hyper-v)\bytes recibidos/s” -SampleInterval
1
-ComputerName
$IP).CounterSamples.CookedValue)/1024),2)
$BytesSecOutNIC1
=
[math]::round((((Get-counter
-Counter “\interfaz de red(adaptador de red de microsoft hyper-v)\bytes enviados/s” -SampleInterval
1
-ComputerName
$IP).CounterSamples.CookedValue)/1024),2)

For this one I have taken advantage of the three counters which include Bytes Total, Sent Bytes and Received Bytes. Of course, here is important consistency among boxes in hardware, or need an adaptation for physical boxes where the counter names also may change, depending on the hardware installed. We will cover this point in a separate post in the future.

  1. Now that we have this ( or other, is just a matter of different needs or requirements) information, and tidied up enough to be pretty and presentable to a PRTG sensor, is the moment of converting this data to the one expected by PRTG:
Write-Host ‘<?xml version=”1.0″ encoding=”Windows-1252″ ?>’
Write-Host ‘<prtg>’
Write-Host “<result>”

Write-Host ” <channel>Ping</channel>”

Write-Host ” <unit>Custom</unit>”

Write-Host ” <CustomUnit>Milisegundos</CustomUnit>”

Write-Host ” <mode>Absolute</mode>”

Write-Host ” <showChart>1</showChart>”

Write-Host ” <showTable>1</showTable>”

Write-Host ” <warning>100</warning>”

Write-Host ” <float>1</float>”

Write-Host ” <value>$($Ping.ResponseTime)</value>”

Write-Host ” <LimitMaxError>500</LimitMaxError>”

Write-Host ” <LimitMaxWarning>100</LimitMaxWarning>”

Write-Host ” <LimitWarningMsg>El Ping es alto para ser LAN</LimitWarningMsg>”

Write-Host ” <LimitErrorMsg>El Ping es alto para ser LAN, algo ocurre</LimitErrorMsg>”

Write-Host ” <LimitMode>1</LimitMode>”

Write-Host “</result>”

Write-Host ‘</prtg>’

In the sample above I show how the output is expected by PRTG to be able to parse it correctly.

Between the <prtg> and </prtg > XML tags, you can include each value generating a whole <result> set for each value you may want to use, for example the ones above we have used.

You can add as many as you want, but officically Paessler only supports up to 50 results or ‘channels’.

  1. Adding to PRTG
    1. First thing is to make sure permissions are granted:
      1. The PRTG PROBE (not the server) is the authentication which you may keep in mind, as is the one whoich will run the sensor at the end of the day.
      2. You must ensure the user running the Probe service has enough permissions on target boxes as local administrator to connect to them via PowerShell and to be able to run the script and gather the information.
    2. Second, we may place the script in the PRTG Folder for these ones:
[Paessler PRTG Installation Folder]\ Custom Sensors\EXEXML
  1. The after generating the PRTG device we need to add the custom sensor to our device ( See this)
  2. And we only have to make sure our script is correctly configured

Making sure we provide the %host value to our script (we set this to be passed as a parameter) to get it working fine. Be patient, as this can take up to one minute, depending on the load of the monitoring server, to retrieve valid data and show it.

TEST

If all has gone fine, now we should be able to see our device with a single sensor, which in this case has the name customized to System_Health

        

If we click on it, we will be able to see all the information, displayed as different channels in the gauges and graphs:

And we are done! The box is being basically monitored at a single sensor cost!

Tanks for reading this, hope it can result helpful! And please post if you have doubts or if know a way better, I’m sure there’s always a possible improvement!

P.