We had to find a way to install package (hotfix or msi ) on our servers juste before the Citrix scheduled reboot, as we try to limit the effects on our users. We also want the script to be the most multi-purpose, then we decided to put all applicable actions in an xml file (action_list.xml) with the following syntax :
<actions>
<action name="action1">
<file>\\server\share\file1.msi</file>
<script>c:\windows\system32\msiexec.exe /i c:\tmp\file1.msi /l*v c:\tmp\file1.log /quiet /norestart</script>
<clean>file1.log</clean>
<clean>=== Verbose logging stopped:</clean>
</action>
(...)
<action name="actionx">
<file>\\server\share\filex.exe</file>
<script>c:\tmp\filex.exe log:c:\tmp\filex.log /norestart</script>
<clean>filex.log</clean>
<clean>KBXXXXXXXX</clean>
</action>
</actions>
In a CSV file, we store Citrix folders and the required actions
Folder;Action
Servers/MyInfra;action1
Servers/MyApp1;actionX
Finally, we prefered to make the choice of a dynamically declared variables to store actions rather than insert them into an hashtable, the script is more brief and lisible.
A variable is initialized with the cmdlet New-Variable
PS>New-Variable -Name Toto -Value titi
PS>$toto
titi
We can access to its content with the cmdlet get-variable
PS > get-variable -Name toto
Name Value
---- -----
toto titi
PS > get-variable -Name toto -valueonly
titi
Here come the full script
#########################################################
# FileName RemoteInstall.ps1 #
# Version 0.3 #
# Author Nicolas #
#########################################################
# ChangeLog #
# #
# Version 0.3 #
# Author Nicolas #
# Content Utilisation d'un fichier XML
# pour stocker les actions #
# #
# Version 0.2 #
# Author Nicolas #
# Content Ajout d'un système de multi #
# installation de composant "Actions List" #
# #
# Version 0.1 #
# Author Nicolas #
# Content Initial Realase #
#########################################################
# TODO #
# WSUS Forced Install #
#########################################################
#Chargement des Actions depuis le fichier action_list.xml
([xml]( gc .\action_list.xml)).actions.action |% { new-variable -Name $_.name -value $_ }
#Déploiement
#Ajout des composant Citrix si non chargé
If ((Get-PSSnapin -Name "Citrix*" -ErrorAction SilentlyContinue | Measure-Object).count -eq 0) {Add-PSSnapin -Name "Citrix*"}
Write-host Installation
import-csv .\Folders.txt -delim ';' |% {
#Récupération de la liste des serveurs qui vont redemarrer dans 1h
$servers = get-xaserver -FolderPath $_.Folder |Select ServerNAme,@{N="Last reboot";E={(get-date).AddSeconds( - (gwmi -ComputerName $_.ServerNAme Win32_PerfFormattedData_PerfOS_System).systemuptime) }} |? { $_.'Last Reboot'.dayOfweek -eq [DateTime]::Now.AddDays(-7).DayOfWeek }
$action = $_.action
if( $servers -and (Get-Variable |? { $_.NAme -eq $action })){#L'action existe ?
$myAction = get-variable $action -valueonly
$servers |% {
Write-host $_.ServerName
#copie des fichiers nécessaire
if( -not (test-path \\$($_.serverName)\c$\tmp )){ new-item \\$($_.serverName)\c$\tmp -type directory}
if( $myAction.file ){
cpi $myAction.file \\$($_.serverName)\c$\tmp
}
#lancement de l'installation
$process = [WMICLASS]"\\$($_.ServerName)\ROOT\CIMV2:win32_process"
$result = $process.Create($myAction.script)
}
#temporisation
Start-Sleep -second 120
Write-host suppression
$servers |% {
Write-host $_.ServerName
if( $myAction.clean ){
#Attente de la fin d'installation
while( -not( Select-String \\$($_.ServerNAme)\c$\tmp\$($myAction.clean[0]) -pattern ($myAction.clean[1]) -quiet )){
Start-Sleep -second 120
}
#Suppression des fichiers copiés
if( (get-variable $action -valueonly).file ){
remove-item \\$($_.ServerNAme)\c$\tmp\$( Split-PAth -Leaf $myAction.file )
}
}
}
}
}
Did you like this article? Share it with your friends!
Leave a Reply