Skip to content

Instantly share code, notes, and snippets.

@raulunzue
Last active October 3, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raulunzue/e14bd899e1d5f5640cb38d48d49f617e to your computer and use it in GitHub Desktop.
Save raulunzue/e14bd899e1d5f5640cb38d48d49f617e to your computer and use it in GitHub Desktop.
FSLogix comprimir discos VHDX de Citrix mediante Powershell: https://www.maquinasvirtuales.eu/powershell-comprimir-perfiles-fslogix/
######################
# FUNCION SABER LINEA
######################
function linea
{
$input | Select-String '.' | Select-Object -Property LineNumber, Line
}
###############################
# LISTAR DISCOS Y TAMAÑOS VHDX
###############################
$usuarios=Get-ChildItem -Path "C:\Scripts\Invoke-FslShrinkDisk-master" -Filter "*.vhdx" -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:\SCRIPTS\vhdx.txt"
# Limpiamos los espacios en blanco y exportamos a TXT
(Get-Content C:\SCRIPTS\vhdx.txt) | ? {$_.trim() -ne "" } | Set-Content C:\SCRIPTS\vhdx.txt
#$vhdx=(Get-Content -Path "C:\SCRIPTS\vhdx.txt")
#ESPACIO OCUPADO
$usuarios.SizeInGB > "C:\SCRIPTS\gigas.txt"
# Limpiamos los espacios en blanco y exportamos a TXT
(Get-Content C:\SCRIPTS\gigas.txt) | ? {$_.trim() -ne "" } | Set-Content C:\SCRIPTS\gigas.txt
#$gigas=(Get-Content -Path "C:\SCRIPTS\gigas.txt")
#####################
# TRATAMOS LOS DATOS
#####################
$dia=Get-date -Format "MM-dd-yyyy"
ForEach ($file in (Get-Content -Path "C:\SCRIPTS\vhdx.txt"))
{
#SABER LA LINEA EN EL LISTADO DEL VHDX
$linea= (Get-Content -Path "C:\SCRIPTS\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:\SCRIPTS\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 3GB
if ($gigas -ge 3)
{
#################
# COMPRIMIR VHDX
#################
cd "C:\Scripts\Invoke-FslShrinkDisk-master"
.\Invoke-FslShrinkDisk.ps1 "$file.vhdx" -Recurse -PassThru
##########################
# GENERA LOG DE ACCIONES
##########################
#PUNTO DE CONTROL
Write-Output "Perfil mayor de 3GB"
Echo "$hora - Perfil VHDX de $file de $gigas GB, se comprime" >> C:\SCRIPTS\LOGS\comprimirvhdx-$dia.log
}
else
{
##########################
# GENERA LOG SIN ACCIONES
##########################
#PUNTO DE CONTROL
Write-Output "Perfil menor de 3GB"
Echo "$hora - Perfil VHDX de $file de $gigas GB. No se comprime" >> C:\SCRIPTS\LOGS\comprimirvhdx-$dia.log
}
}
Else {
$gigas=(Get-Content -Path "C:\SCRIPTS\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 3GB
if ($gigas -ge 3)
{
#################
# COMPRIMIR VHDX
#################
cd "C:\Scripts\Invoke-FslShrinkDisk-master"
.\Invoke-FslShrinkDisk.ps1 "$file.vhdx" -Recurse -PassThru
##########################
# GENERA LOG DE ACCIONES
##########################
#PUNTO DE CONTROL
Write-Output "Perfil mayor de 3GB"
Echo "$hora - Perfil VHDX de $file de $gigas GB, se comprime" >> C:\SCRIPTS\LOGS\comprimirvhdx-$dia.log
}
else
{
##########################
# GENERA LOG SIN ACCIONES
##########################
#PUNTO DE CONTROL
Write-Output "Perfil menor de 3GB"
Echo "$hora - Perfil VHDX de $file de $gigas GB. No se comprime" >> C:\SCRIPTS\LOGS\comprimirvhdx-$dia.log
}
}
}
# ELIMINAMOS FICHERO TEMPORAL
Remove-Item C:\SCRIPTS\vhdx.txt
Remove-Item C:\SCRIPTS\gigas.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment