Last active
October 3, 2020 18:12
-
-
Save raulunzue/22423917c69465d44a8a2cf97edb75f5 to your computer and use it in GitHub Desktop.
FSLogix, script Citrix para comprobar tamaño de discos VHDX, mandando un email a los usuarios. Powershell: https://www.maquinasvirtuales.eu/powershell-script-para-comprobar-perfiles-vhdx-fslogix-en-citrix/
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
###################### | |
# FUNCION SABER LINEA | |
###################### | |
function linea | |
{ | |
$input | Select-String '.' | Select-Object -Property LineNumber, Line | |
} | |
############################### | |
# LISTAR DISCOS Y TAMAÑOS VHDX | |
############################### | |
$usuarios=Get-ChildItem -Path "c:\Users\runzue" -Filter "*.vhdx" -Recurse -File | Select-Object Name, @{n = 'SizeInGB'; e = {[math]::round($_.length/1GB,2)}} | |
# SEPARAMOS Y LIMPIAMOS VALORES EN DOS LISTADOS | |
#DISCOS-USUARIOS | |
$usuarios.Name | ForEach-Object { $_ -replace ".vhdx","" } > "c:\Users\runzue\vhdx.txt" | |
# Limpiamos los espacios en blanco y exportamos a TXT | |
(Get-Content c:\Users\runzue\vhdx.txt) | ? {$_.trim() -ne "" } | Set-Content c:\Users\runzue\vhdx.txt | |
#$vhdx=(Get-Content -Path "c:\Users\runzue\vhdx.txt") | |
#ESPACIO OCUPADO | |
$usuarios.SizeInGB > "c:\Users\runzue\gigas.txt" | |
# Limpiamos los espacios en blanco y exportamos a TXT | |
(Get-Content c:\Users\runzue\gigas.txt) | ? {$_.trim() -ne "" } | Set-Content c:\Users\runzue\gigas.txt | |
#$gigas=(Get-Content -Path "c:\Users\runzue\gigas.txt") | |
##################### | |
# TRATAMOS LOS DATOS | |
##################### | |
$dia=Get-date -Format "MM-dd-yyyy" | |
ForEach ($file in (Get-Content -Path "c:\Users\runzue\vhdx.txt")) | |
{ | |
#SABER LA LINEA EN EL LISTADO DEL VHDX | |
$linea= (Get-Content -Path "c:\Users\runzue\vhdx.txt") | sls -Pattern $file | linea | |
$linea=$linea.LineNumber | |
#PUNTO DE CONTROL | |
Write-Output "Fila $linea para valor $file" | |
#BUSCAMOS EL TAMAÑO DEL PERFIL | |
#HAGO UN FILTRO AL PRIMER VALOR PARA QUE NO LO CORTE | |
if ($linea -eq 1) { | |
$gigas=(Get-Content -Path "c:\Users\runzue\gigas.txt" -TotalCount $linea) | |
#PUNTO DE CONTROL | |
Write-Output "Para el disco VHDX $file el valor es $gigas GBs" | |
#################### | |
# HACEMOS UN FILTRO | |
#################### | |
$hora=Get-Date -Format "HH:mm:ss" | |
# SI EL TAMAÑO ES DE MAS DE 4GB | |
if ($gigas -ge 4) | |
{ | |
############### | |
# ENVIO CORREO | |
############### | |
# ENCRIPTAMOS USUARIO Y CONTRASEÑA | |
$EncodedTextMAIL = “ZQBsAGIAbABvAGcAZABlAG4AZQBnAHUAQABnAG0AYQBpAGwALgBjAG8AbQA=” | |
$DecodedTextMAIL = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedTextMAIL)) | |
$EncodedTextPW = “cAByAHUAZQBiAGEAcwBjAHIAaQBwAHQA” | |
$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 = "$file@tudominio.es" | |
$emailcc="otrousuario@tudominio.es" | |
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo ) | |
$emailMessage.cc.add($emailcc) | |
$emailMessage.Subject = "PERFIL DE TRABAJO A PUNTO DE LLENARSE" | |
$emailMessage.IsBodyHtml = $true #true or false depends | |
$emailMessage.Body = "<h2>¡Su perfil se está llenando!</h2>" | |
$emailMessage.Body += "Su perfil Windows tiene un límite de <FONT SIZE=4><strong>5GB</strong></font> y usted ha llegado a <FONT SIZE=4><strong>$gigas GB</strong></font>" | |
$emailMessage.Body += "<p>Por favor, mueva ficheros de sus carpetas personales (Escritorio, Descargas y Documentos), a sus unidades de red asignadas</p>" | |
$emailMessage.Body += "<p>Si no lo hace, puede provocar problemas en su sesión</p>" | |
$emailMessage.Body += "Gracias de antemano, le agradecemos su colaboración" | |
$emailMessage.Body += '<p><img src="https://www.maquinasvirtuales.eu/wp-content/uploads/2018/11/maquinas-virtuales-logo1.png"></p>' | |
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort ) | |
$SMTPClient.EnableSsl = $False | |
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass ); | |
$SMTPClient.Send( $emailMessage ) | |
########################## | |
# GENERA LOG DE ACCIONES | |
########################## | |
#PUNTO DE CONTROL | |
Write-Output "Perfil mayor de 4GB" | |
Echo "$hora - Enviado correo a $file@tudominio.es. Perfil de $file de $gigas GB y es mayor de 4GB" >> c:\Users\runzue\perfilesvhdx-$dia.log | |
} | |
else | |
{ | |
########################## | |
# GENERA LOG SIN ACCIONES | |
########################## | |
#PUNTO DE CONTROL | |
Write-Output "Perfil menor de 4GB" | |
Echo "$hora - Perfil de $file de $gigas GB. Menor de 4GB" >> c:\Users\runzue\perfilesvhdx-$dia.log | |
} | |
} | |
Else { | |
$gigas=(Get-Content -Path "c:\Users\runzue\gigas.txt" -TotalCount $linea)[-1] | |
#PUNTO DE CONTROL | |
Write-Output "Para el disco VHDX $file el valor es $gigas GBs" | |
#################### | |
# HACEMOS UN FILTRO | |
#################### | |
$hora=Get-Date -Format "HH:mm:ss" | |
# SI EL TAMAÑO ES DE MAS DE 4GB | |
if ($gigas -ge 4) | |
{ | |
############### | |
# ENVIO CORREO | |
############### | |
# ENCRIPTAMOS USUARIO Y CONTRASEÑA | |
$EncodedTextMAIL = “ZQBsAGIAbABvAGcAZABlAG4AZQBnAHUAQABnAG0AYQBpAGwALgBjAG8AbQA=” | |
$DecodedTextMAIL = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($EncodedTextMAIL)) | |
$EncodedTextPW = “cAByAHUAZQBiAGEAcwBjAHIAaQBwAHQA” | |
$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 = "$file@tudominio.es" | |
$emailcc="otrousuario@tudominio.es" | |
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo ) | |
$emailMessage.cc.add($emailcc) | |
$emailMessage.Subject = "PERFIL DE TRABAJO A PUNTO DE LLENARSE" | |
$emailMessage.IsBodyHtml = $true #true or false depends | |
$emailMessage.Body = "<h2>¡Su perfil se está llenando!</h2>" | |
$emailMessage.Body += "Su perfil Windows tiene un límite de <FONT SIZE=4><strong>5GB</strong></font> y usted ha llegado a <FONT SIZE=4><strong>$gigas GB</strong></font>" | |
$emailMessage.Body += "<p>Por favor, mueva ficheros de sus carpetas personales (Escritorio, Descargas y Documentos), a sus unidades de red asignadas</p>" | |
$emailMessage.Body += "<p>Si no lo hace, puede provocar problemas en su sesión</p>" | |
$emailMessage.Body += "Gracias de antemano, le agradecemos su colaboración" | |
$emailMessage.Body += '<p><img src="https://www.maquinasvirtuales.eu/wp-content/uploads/2018/11/maquinas-virtuales-logo1.png"></p>' | |
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort ) | |
$SMTPClient.EnableSsl = $False | |
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass ); | |
$SMTPClient.Send( $emailMessage ) | |
########################## | |
# GENERA LOG DE ACCIONES | |
########################## | |
#PUNTO DE CONTROL | |
Write-Output "Perfil mayor de 4GB" | |
Echo "$hora - Enviado correo a $file@tudominio.es. Perfil de $file de $gigas GB y es mayor de 4GB" >> c:\Users\runzue\perfilesvhdx-$dia.log | |
} | |
else | |
{ | |
########################## | |
# GENERA LOG SIN ACCIONES | |
########################## | |
#PUNTO DE CONTROL | |
Write-Output "Perfil menor de 4GB" | |
Echo "$hora - Perfil de $file de $gigas GB. Menor de 4GB" >> c:\Users\runzue\perfilesvhdx-$dia.log | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment