Last active
August 4, 2022 17:45
-
-
Save raulunzue/652d4686a3bcf87910961faa38ced7f2 to your computer and use it in GitHub Desktop.
Report servidores o pcs. Powershell con envío de report HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################### | |
## Raúl Unzué - El Blog de Negu | |
## https://www.maquinasvirtuales.eu/ | |
## Reporte HTML de Ordenadores / Servidores | |
########################################### | |
#Definimos los códigos de estilos CSS | |
$header = @" | |
<style> | |
h1 { | |
font-family: Arial, Helvetica, sans-serif; | |
color: #44A1FE; | |
font-size: 28px; | |
} | |
h2 { | |
font-family: Arial, Helvetica, sans-serif; | |
color: #000099; | |
font-size: 16px; | |
} | |
table { | |
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; | |
border-collapse:collapse; | |
overflow:scroll; | |
} | |
td { | |
font-size:1em; | |
border:1px solid #2087db; | |
padding:5px 5px 5px 5px; | |
text-align: center; | |
} | |
th { | |
font-size:1.1em; | |
text-align:center; | |
padding-top:5px; | |
padding-bottom:5px; | |
padding-right:7px; | |
padding-left:7px; | |
background-color:#2087db; | |
color:#ffffff; | |
text-align: center; | |
} | |
tbody tr:nth-child(even) { | |
background: #f0f0f2; | |
} | |
#CreationDate { | |
font-family: Arial, Helvetica, sans-serif; | |
color: #ff3300; | |
font-size: 12px; | |
} | |
.StopStatus { | |
color: #ff0000; | |
} | |
.RunningStatus { | |
color: #008000; | |
} | |
</style> | |
"@ | |
#Obtenemos el nombre del ordenador o servidor | |
$ComputerName = "<h1>Nombre PC / Server: $env:computername</h1>" | |
#El siguiente comando obtendrá la información del sistema operativo, convertirá el resultado a código HTML como una tabla y lo almacenará en una variable | |
$OSinfo = Get-CimInstance -Class Win32_OperatingSystem | ConvertTo-Html -As List -Property Version,Caption,BuildNumber,Manufacturer -Fragment -PreContent "<h2>Información Sistema Operativo</h2>" | |
#El siguiente comando obtendrá la información del procesador, convertirá el resultado a código HTML como una tabla y lo almacenará en una variable | |
$ProcessInfo = Get-CimInstance -ClassName Win32_Processor | ConvertTo-Html -As List -Property DeviceID,Name,Caption,MaxClockSpeed,SocketDesignation,Manufacturer -Fragment -PreContent "<h2>Información Procesador</h2>" | |
#El siguiente comando obtendrá la información del BIOS, convertirá el resultado a código HTML como tabla y lo almacenará en una variable | |
$BiosInfo = Get-CimInstance -ClassName Win32_BIOS | ConvertTo-Html -As List -Property SMBIOSBIOSVersion,Manufacturer,Name,SerialNumber -Fragment -PreContent "<h2>Información BIOS</h2>" | |
#El siguiente comando obtendrá los detalles de Disk, convertirá el resultado a código HTML como tabla y lo almacenará en una variable | |
$DiscInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID,DriveType,VolumeName, @{N="DiskSizeGB";e={[math]::Round($($_.Size) / 1073741824,0)}}, @{N="FreeSpaceGB";e={[math]::Round($($_.FreeSpace) / 1073741824,0)}}, @{N="FreePorcentaje";e={[math]::Round($($_.FreeSpace) / $_.Size * 100,1)}} | ConvertTo-Html -As List -Property DeviceID,DriveType,VolumeName,DiskSizeGB,FreeSpaceGB,FreePorcentaje -Fragment -PreContent "<h2>Disk Information</h2>" | |
#El siguiente comando obtendrá la información de los primeros 10 servicios, convertirá el resultado a código HTML como una tabla y lo almacenará en una variable | |
$ServicesInfo = Get-CimInstance -ClassName Win32_Service | Select-Object -First 10 | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "<h2>Información Servicios</h2>" | |
#$ServicesInfo = Get-CimInstance -ClassName Win32_Service | ConvertTo-Html -Property Name,DisplayName,State -Fragment -PreContent "<h2>Información Servicios</h2>" | |
$ServicesInfo = $ServicesInfo -replace '<td>Running</td>','<td class="RunningStatus">Running</td>' | |
$ServicesInfo = $ServicesInfo -replace '<td>Stopped</td>','<td class="StopStatus">Stopped</td>' | |
#El siguiente comando combinará toda la información recopilada en un solo informe HTML | |
$Report = ConvertTo-HTML -Body "$ComputerName $OSinfo $ProcessInfo $BiosInfo $DiscInfo $ServicesInfo" -Head $header -Title "Computer Information Report" -PostContent "<p id='CreationDate'>Creado el día: $(Get-Date)</p>" | |
#El siguiente comando generará el informe en un archivo HTML | |
$Report | Out-File C:\Users\runzue\Desktop\UniversalDashboard\Basic-Computer-Information-Report-$env:computername.html | |
#Envío de resultados | |
############### | |
# ENVIO CORREO | |
############### | |
# ENCRIPTAMOS USUARIO Y CONTRASEÑA | |
$EncodedTextMAIL = “ZQBsAGIAbABvAGcAZABlAG4AZQBnAHUA” | |
$DecodedTextMAIL = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedTextMAIL)) | |
$EncodedTextPW = “ZQBsAGIAbABvAGcAZABlAG4AZQBnAHUA” | |
$DecodedTextPW = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedTextPW)) | |
$emailSmtpServer = "smtp.tuserver.es" | |
$emailSmtpServerPort = "587" | |
$emailSmtpUser = "$DecodedTextMAIL" | |
$emailSmtpPass = "$DecodedTextPW" | |
$emailFrom = "cuentacorreo@tudominio.es" | |
$emailTo = "cuentaadmin@tudominio.es" | |
$emailcc="otrousuario@tudominio.es" | |
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo ) | |
$emailMessage.cc.add($emailcc) | |
$emailMessage.Subject = "Datos de PC/Server: $env:computername" | |
$emailMessage.IsBodyHtml = $true #true or false depends | |
$emailMessage.Body = $body | |
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort ) | |
$SMTPClient.EnableSsl = $False | |
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass ); | |
$SMTPClient.Send( $emailMessage ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment