PS-Windows-Client.ps1 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. $logfile = "C:\Windows\SRM\SRM.log"
  2. $BaseURL = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\SRM -Name MasterURL
  3. $Secret = Get-ItemPropertyValue -Path HKLM:\SOFTWARE\SRM -Name AssetToken
  4. $Hostname = hostname
  5. $fullurl = "$BaseURL/inform/poll/?hostname=$Hostname"
  6. $webClient = New-Object System.Net.WebClient
  7. $webClient.Headers.add('Authorization',$Secret)
  8. $pollanswer = $webClient.DownloadString($fullurl)
  9. if($pollanswer -eq 0){
  10. $now = (get-date).tostring("yyyy-MM-dd HH:mm:ss")
  11. Add-Content $logfile "$now - Master has no tasks"
  12. }else{
  13. $fullurl = "$BaseURL/inform/run/?hostname=$Hostname&schedid=$pollanswer"
  14. $webClient = New-Object System.Net.WebClient
  15. $webClient.Headers.add('Authorization',$Secret)
  16. $rawscript = $webClient.DownloadString($fullurl)
  17. $shell = ($rawscript -split '\:' ,2)[0]
  18. $script = ($rawscript -split '\:' ,2)[1]
  19. if($shell -eq "cmd"){
  20. set-content -Path C:\Windows\SRM\run.cmd -Value $script
  21. $output = & C:\Windows\SRM\run.cmd
  22. }elseif($shell -eq "powershell"){
  23. set-content -Path C:\Windows\SRM\run.ps1 -Value $script
  24. $output = & C:\Windows\SRM\run.ps1
  25. }else{
  26. $now = (get-date).tostring("yyyy-MM-dd HH:mm:ss")
  27. $output = "Cannot run $shell script"
  28. Add-Content -Path $logfile "$now - $output"
  29. }
  30. $URL = "$BaseURL/inform/return/?hostname=$Hostname&schedid=$pollanswer"
  31. $wc = new-object net.WebClient
  32. $wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
  33. $wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727)")
  34. $wc.Headers.Add("Authorization", $Secret)
  35. $wc.UseDefaultCredentials = $true
  36. $NVC = New-Object System.Collections.Specialized.NameValueCollection
  37. $NVC.Add("__VIEWSTATE", $viewstate);
  38. $NVC.Add("__EVENTVALIDATION", $eventvalidation);
  39. $NVC.Add("status", "complete");
  40. $NVC.Add("output", $output);
  41. $NVC.Add("ctl00`$MainContent`$Submit", "Submit");
  42. $wc.QueryString = $NVC
  43. $Result = $WC.UploadValues($URL,"POST", $NVC)
  44. $readableoutput = [System.Text.Encoding]::UTF8.GetString($Result)
  45. $WC.Dispose();
  46. }