Last active
March 6, 2019 09:20
-
-
Save raulunzue/2a0ec8ebb6fc1c4c3b072cdaa0ec9a41 to your computer and use it in GitHub Desktop.
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
############################################################## | |
#SCRIPT CIERRE SESIONES USUARIOS EN CITRIX (TAREA PROGRAMADA)# | |
############# Raúl Unzué - 27/02/2019 ####################### | |
#################### EL BLOG DE NEGU ######################### | |
# https://www.maquinasvirtuales.eu/cerrar-sesion-de-usuarios-citrix-con-tarea-programada-y-powershell/# | |
############################################################## | |
################################## | |
# SOBRE EL CONTROLADOR DE DOMINIO | |
################################## | |
# Abrir una sesión persistente y la guardamos en una variable $ConAD | |
$ConAD= New-PSSession -ComputerName neguad01.negu.local | |
# Extraemos los usuarios del grupo a un fichero | |
$UsuariosAD=Invoke-Command -Session $ConAD {Get-ADGroupMember “CITRIXKILL” -recursive | Select-Object -Property name | Format-Wide -Column 1 | Out-String} | |
# Creamos un fichero de texto con el contenido de la variable | |
Out-File -InputObject $UsuariosAD -FilePath C:\Users\runzue\Desktop\UsuariosAD.txt | |
# Limpiamos los espacios en blanco | |
(gc C:\Users\runzue\Desktop\UsuariosAD.txt) | ? {$_.trim() -ne "" } | set-content C:\Users\runzue\Desktop\UsuariosAD.txt | |
# Agregamos el dominio en cada línea de usuario para poder comparar los datos exportados | |
$Dominio="NEGU\" | |
$PathAD= 'C:\Users\runzue\Desktop\UsuariosAD.txt' | |
(get-content $PathAD) | foreach { $Dominio + $_ } | set-content $PathAD | |
# Re-Cargamos la variable con el fichero limpio | |
#$UsuariosAD=@(Get-Content $PathAD | Where-Object { ![string]::IsNullOrWhiteSpace($_) }) | |
################################# | |
# SOBRE EL DELLIVERY CONTROLLER | |
################################# | |
# Abrir una sesión persistente y la guardamos en una variable $ConDC | |
$ConDC= New-PSSession -ComputerName ctxroles01.negu.local | |
#$ConDC= Enter-PSSession -ComputerName localhost | |
#Lanzamos sobre la sesión persistente la carga de los Snapin de Citrix | |
Invoke-Command -Session $ConDC {Add-PSSnapin -Name Citrix*} | |
# Extraemos los usuarios del Delivery Controller y los guardamos en una variable | |
$UsuariosDC=Invoke-Command -Session $ConDC {Get-BrokerSession | Select-Object -Property UserName | Format-Wide -Column 1 | Out-String} | |
#Eliminamos el dominio | |
#$UsuariosDC= $UsuariosDC -replace "GRUPOIMQ\\","" | |
# Creamos un fichero de texto con el contenido de la variable y limpiamos los espacios | |
Out-File -InputObject $UsuariosDC -FilePath C:\Users\runzue\Desktop\UsuariosDC.txt | |
# Limpiamos los espacios en blanco | |
(gc C:\Users\runzue\Desktop\UsuariosDC.txt) | ? {$_.trim() -ne "" } | Set-Content C:\Users\runzue\Desktop\UsuariosDC.txt | |
# Re-Cargamos la variable con el fichero limpio | |
#$PathDC= 'C:\Users\runzue\Desktop\UsuariosDC.txt' | |
#$UsuariosDC=@(Get-Content $PathDC | Where-Object { ![string]::IsNullOrWhiteSpace($_) }) | |
################################ | |
# COMPARAMOS LOS FICHEROS | |
################################ | |
# Comparamos los dos ficheros TXT, para extraer los que están en el grupo de AD | |
$AD = Get-Content C:\Users\runzue\Desktop\UsuariosAD.txt | ForEach-Object { $_.Trim() } | |
$DC = Get-Content C:\Users\runzue\Desktop\UsuariosDC.txt | ForEach-Object { $_.Trim() } | |
$comp=Compare-Object $AD $DC -IncludeEqual -ExcludeDifferent | Select-Object -Property InputObject | Format-Wide -Column 1 | Out-String | |
Out-File -InputObject $comp -FilePath C:\Users\runzue\Desktop\UsuariosDiff.txt | |
# Limpiamos los espacios en blanco | |
(gc C:\Users\runzue\Desktop\UsuariosDiff.txt) | ? {$_.trim() -ne "" } | Set-Content C:\Users\runzue\Desktop\UsuariosDiff.txt | |
# Cargamos Array. Cargamos la variable con el fichero limpio | |
#$PathDiff= 'C:\Users\runzue\Desktop\UsuariosDiff.txt' | |
#$UsuariosDiff=@(Get-Content $PathDiff | Where-Object { ![string]::IsNullOrWhiteSpace($_) }) | |
######################################################################## | |
# CERRAMOS LA SESION DE LOS QUE NO ESTEN EN EL GRUPO DE ACTIVE DIRECTORY | |
######################################################################## | |
#Cerramos todas las sesiones de los usuarios | |
ForEach ($user in $(Get-Content C:\Users\runzue\Desktop\UsuariosDiff.txt)) | |
{ | |
Invoke-Command -Session $ConDC {param($user);Get-BrokerSession -Username $user | Stop-BrokerSession} -ArgumentList $user | |
# Invoke-Command -Session $ConDC {param($user);Get-BrokerSession -Username $user -Filter { SessionState -eq 'Disconnected' } | Stop-BrokerSession} -ArgumentList $user | |
# Generamos log | |
$date= Get-Date | |
$log = Write-Output "$date, Forzamos cierre sesion $user" | |
Add-Content $log -Path "C:\Users\runzue\Desktop\matar-sesiones.log" | |
} | |
################################# | |
# LIBERAMOS SESIONES EN SERVIDORES | |
################################# | |
# Obtenemos las sesiones remotas generados y las cerramos | |
Get-PSSession | Remove-PSSession |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment