Advertisement
Guest User

script.ps1

a guest
Feb 9th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $PATH_PSCP = "pscp.exe"
  2. $PATH_PSSERVICE = "c:\PsTools\psservice.exe"
  3. $PATH_PSEXEC = "c:\PsTools\psexec.exe"
  4. $SLEEP_SECS = "5"
  5. $MAX_STATUS_CHECK_ITER = "2"
  6.  
  7. Function WaitForServiceTo ($address, $service, $status) {
  8.     [Array]$serv_query_args = "\\$address", "-u", "$REMOTE_USER", "-p", "$REMOTE_USER_PWD", "query", "$service";
  9.     $iteration = 1
  10.     do {
  11.         if ($iteration -gt $MAX_STATUS_CHECK_ITER) {
  12.             write-host "No se consiguió que el servicio $service llegue al estado $status en el equipo $address" -foreground "red"
  13.             return $false
  14.         }
  15.  
  16.         write-host "Esperamos $SLEEP_SECS segundos antes de comprobar el estado del servicio" -foreground "yellow"
  17.         Start-Sleep -s $SLEEP_SECS
  18.         write-host "Solicitud de estado para el servicio $service - Iteración $iteration" -foreground "yellow"
  19.         write-host "$PATH_PSSERVICE $serv_query_args" -foreground "yellow"
  20.         $result = & $PATH_PSSERVICE $serv_query_args
  21.         if ($result -eq $null) {
  22.             break
  23.         }
  24.        
  25.         $str_result = [string]$result
  26.         $res = $str_result.Contains($status)
  27.         $iteration = $iteration + 1
  28.     } while ($res -ne $true)
  29.    
  30.     return $true
  31. }
  32.  
  33. Function StopService ($address, $service) {
  34.     [Array]$serv_stop_args = "\\$address", "-u", "$REMOTE_USER", "-p", "$REMOTE_USER_PWD", "stop", "$service";
  35.     write-host "Stopping service $service" -foreground "yellow"
  36.     write-host "$PATH_PSSERVICE $serv_stop_args" -foreground "yellow"
  37.     $result = & $PATH_PSSERVICE $serv_stop_args
  38.     $str_result = [string]$result
  39.     $RES = $str_result.Contains("1  STOPPED")
  40.     if ($result -ne $null -and $res -ne $true) {
  41.         write-host "Servicio $service no ha parado todavía..." -foreground "yellow"
  42.         $wait_result = WaitForServiceTo $address $service "1  STOPPED"
  43.     } else {
  44.         $wait_result = $true
  45.     }
  46.    
  47.     if ($wait_result -eq $true) {
  48.         write-host "Servicio $service parado" -foreground "green"
  49.     }
  50.    
  51.     return $wait_result
  52. }
  53.  
  54. Function StartService ($address, $service) {
  55.     [Array]$serv_start_args = "\\$address", "-u", "$REMOTE_USER", "-p", "$REMOTE_USER_PWD", "start", "$service";
  56.     write-host "Starting service $service" -foreground "yellow"
  57.     write-host "$PATH_PSSERVICE $serv_start_args" -foreground "yellow"
  58.     $result = & $PATH_PSSERVICE $serv_start_args
  59.     $str_result = [string]$result
  60.     $RES = $str_result.Contains("4  RUNNING")
  61.     if ($result -ne $null -and $res -ne $true) {
  62.         write-host "Servicio $service no se ha iniciado todavía..." -foreground "yellow"
  63.         $wait_result = WaitForServiceTo $address $service "4  RUNNING"
  64.     } else {
  65.         $wait_result = $true
  66.     }
  67.    
  68.     if ($wait_result -eq $true) {
  69.         write-host "Servicio $service iniciado" -foreground "green"
  70.     }
  71.    
  72.     return $wait_result
  73. }
  74.  
  75. & cls
  76.  
  77. if ($args.Length -ne 4) {
  78.     write-host "Wrong number of parameters. usage: kiosk_installer_script.ps1 REMOTE_USERNAME REMOTE_USERNAME_PASSWORD INSTALLATION_FOLDER_NAME KIOSK_TYPE" -foreground "red"
  79.     exit
  80. }
  81.  
  82. $REMOTE_USER = $args[0]
  83. $REMOTE_USER_PWD = $args[1]
  84. $INSTALLER_FOLDER = $args[2]
  85. $KIOSK_TYPE = $args[3]
  86.  
  87. if ($KIOSK_TYPE -eq "TPVA") {
  88.     $MSI_FILE = "TPVAInstaller.msi"
  89.     $SERVICE_NAME = "IonIDESellingPointManager"
  90.     $PRODUCT_CODE = "{3A273578-9191-441E-B82A-6F70933CA4CD}"
  91.     $SOURCE_KIOSK_LIST = "$Pwd\tpva_kiosks"
  92.     $KIOSK_INSTALL_ERROR = "$Pwd\failed_tpva_kiosks.txt"
  93. } elseif ($KIOSK_TYPE -eq "KION") {
  94.     $MSI_FILE = "KIONInstaller.msi"
  95.     $SERVICE_NAME = "IonIDESellingPointManager"
  96.     $PRODUCT_CODE = "{9DF487A0-4B4F-45D1-B78B-ACACA69131BA}"
  97.     $SOURCE_KIOSK_LIST = "$Pwd\kion_kiosks"
  98.     $KIOSK_INSTALL_ERROR = "$Pwd\failed_kion_kiosks.txt"
  99. } elseif ($KIOSK_TYPE -eq "IK100") {
  100.     $MSI_FILE = "IK100Installer.msi"
  101.     $SERVICE_NAME = "IonIDESellingPointManager"
  102.     $PRODUCT_CODE = "{0577C7A7-9A7D-42AD-838E-D85C47487A46}"
  103.     $SOURCE_KIOSK_LIST = "$Pwd\ik100_kiosks"
  104.     $KIOSK_INSTALL_ERROR = "$Pwd\failed_ik100_kiosks.txt"
  105. } else {
  106.     write-host "Kiosk type must be either TPVA or KION or IK100" -foreground "red"
  107.     exit
  108. }
  109.  
  110. if ($INSTALLER_FOLDER.StartsWith(".\") -eq $True) {
  111.     $INSTALLER_FOLDER = $INSTALLER_FOLDER.TrimStart(".\")
  112. }
  113.  
  114. if (Test-Path ("$KIOSK_INSTALL_ERROR")) {
  115.     & del "$KIOSK_INSTALL_ERROR"
  116. }
  117.  
  118. $kiosk_list = [System.IO.File]::OpenText("$SOURCE_KIOSK_LIST")
  119.  
  120. try {
  121.     for(;;) {
  122.         $kiosk_address = $kiosk_list.ReadLine()
  123.         if ($kiosk_address -eq $null) {
  124.             break
  125.         }
  126.  
  127.         write-host "------ Copiando directorio de instalación $INSTALLER_FOLDER a máquina $kiosk_address ------" -foreground "magenta"
  128.         ""
  129.         [Array]$sftp_args = "-sftp", "-v", "-r", "-l", "$REMOTE_USER", "-pw", "$REMOTE_USER_PWD", "$Pwd\$INSTALLER_FOLDER", "$kiosk_address`:";
  130.         write-host "$PATH_PSCP $sftp_args" -foreground "yellow"
  131.         & $PATH_PSCP $sftp_args
  132.  
  133.         if ($LASTEXITCODE -ne 0) {
  134.             write-host "Could not copy installation folder to kiosk $kiosk_address" -foreground "red"
  135.             "$kiosk_address $LASTEXITCODE" >> "$KIOSK_INSTALL_ERROR"
  136.             continue
  137.             }
  138.        
  139.         if ($KIOSK_TYPE -eq "TPVA" -Or $KIOSK_TYPE -eq "KION") {
  140.             ""
  141.             write-host "------ Parando servicio IonIDEKillExplorer ------"  -foreground "magenta"
  142.             ""
  143.             $serv_res = StopService $kiosk_address "IonIDEKillExplorer"
  144.             if ($serv_res -eq $false) {
  145.                 write-host "Error parando el servicio" -foreground "red"
  146.                 "$kiosk_address" >> "$KIOSK_INSTALL_ERROR"
  147.                 continue
  148.             }
  149.  
  150.             ""
  151.             write-host "------ Parando servicio IonIDEFirefoxService ------" -foreground "magenta"
  152.             ""
  153.             $serv_res = StopService $kiosk_address "IonIDEFirefoxService"
  154.             if ($serv_res -eq $false) {
  155.                 write-host "Error parando el servicio" -foreground "red"
  156.                 "$kiosk_address" >> "$KIOSK_INSTALL_ERROR"
  157.                 continue
  158.             }
  159.         }
  160.  
  161.         ""
  162.         write-host "------ Parando servicio $SERVICE_NAME ------" -foreground "magenta"
  163.         ""
  164.         $serv_res = StopService $kiosk_address $SERVICE_NAME
  165.         if ($serv_res -eq $false) {
  166.             $dialog_screen = new-object -comobject wscript.shell
  167.             $int_answer = $dialog_screen.popup("Continuar instalación en este kiosco?", 0 , "Error parando servicio $SERVICE_NAME", 4)
  168.             If ($int_answer -eq 6) {
  169.                 write-host "Continuando la instalación en este kiosco" -foreground "green"
  170.             } else {
  171.                 write-host "Error parando el servicio" -foreground "red"
  172.                 "$kiosk_address" >> "$KIOSK_INSTALL_ERROR"
  173.                 continue
  174.             }
  175.         }
  176.  
  177.         ""
  178.         write-host "------ Desinstalando versión anterior de $SERVICE_NAME ------" -foreground "magenta"
  179.         ""
  180.         [Array]$serv_uninstall_args = "\\$kiosk_address", "-u", "$REMOTE_USER", "-p", "$REMOTE_USER_PWD", "cmd", "/c", "`"msiexec.exe /x $PRODUCT_CODE /qn`"";
  181.         write-host "$PATH_PSEXEC $serv_uninstall_args" -foreground "yellow"
  182.         cmd /c $PATH_PSEXEC $serv_uninstall_args
  183.  
  184.         ""
  185.         if ($LASTEXITCODE -eq 0) {
  186.             write-host "Desinstalación remota completada con éxito" -foreground "green"
  187.         } elseif ($LASTEXITCODE -eq 1605) {
  188.             write-host "Aplicación ya desinstalada. Continuando el proceso..." -foreground "yellow"
  189.         } else {
  190.             write-host "Error en la desinstalación remota - Error $LASTEXITCODE" -foreground "red"
  191.             "$kiosk_address $LASTEXITCODE" >> "$KIOSK_INSTALL_ERROR"
  192.             continue
  193.         }
  194.  
  195.         ""
  196.         write-host "------ Instalando nueva versión de $SERVICE_NAME ------" -foreground "magenta"
  197.         ""
  198.         [Array]$serv_install_args = "\\$kiosk_address", "-u", "$REMOTE_USER", "-p", "$REMOTE_USER_PWD", "cmd", "/c", "`"msiexec.exe /i C:\temp\$INSTALLER_FOLDER\$MSI_FILE /qn /l*v c:\temp\$INSTALLER_FOLDER\install.log user=$REMOTE_USER pass=$REMOTE_USER_PWD`"";
  199.         write-host "$PATH_PSEXEC $serv_install_args" -foreground "yellow"
  200.         cmd /c $PATH_PSEXEC $serv_install_args
  201.  
  202.         ""
  203.         if ($LASTEXITCODE -eq 0) {
  204.             write-host "Instalación remota completada con éxito" -foreground "green"
  205.         } else {
  206.             write-host "Error en la instalación remota - Error $LASTEXITCODE" -foreground "red"
  207.             "$kiosk_address $LASTEXITCODE" >> "$KIOSK_INSTALL_ERROR"
  208.             continue
  209.         }
  210.  
  211.         ""
  212.         write-host "------ Iniciando servicio $SERVICE_NAME ------" -foreground "magenta"
  213.         ""
  214.         $serv_res = StartService $kiosk_address $SERVICE_NAME
  215.         if ($serv_res -eq $false) {
  216.             write-host "Error iniciando el servicio" -foreground "red"
  217.             "$kiosk_address" >> "$KIOSK_INSTALL_ERROR"
  218.             continue
  219.         }
  220.     }
  221. }
  222. finally {
  223.     $kiosk_list.Close();
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement