mikelieman

drop-sxs-collect.ps1

Mar 7th, 2021 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. #
  3. #
  4.  
  5.  
  6. param ($computer)
  7. if ($computer -eq $null)  {
  8.     $computer = read-host -Prompt "Please enter a computer name (e.g.: bgc-kevin, bgc-mike, bgc-dean,...)"
  9. }
  10.  
  11. function cleanup-remote {
  12.  
  13.     param ($RemovableFiles)
  14.  
  15.     ForEach ($RemovableFile in $RemovableFiles) {
  16.    
  17.         $retval = Invoke-Command -Session $rs -ScriptBlock {
  18.             Param($RemovableFile)
  19.             Try {
  20.                 Remove-Item -LiteralPath $RemovableFile -ErrorAction Stop
  21.                 return "Removed $RemovableFile"
  22.             }
  23.             Catch {
  24.                 return $_
  25.             }
  26.         } -ArgumentList $RemovableFile
  27.        
  28.         # note, $retval now has any output from the try/catch block
  29.         Write-Host "<d> $computer - $retval"
  30.        
  31.     }  # ForEach
  32. }
  33.  
  34. #
  35. # Define paths
  36. #
  37. $LogFileSource  = "C:\Windows\Logs\WindowsUpdate\WinSxS-stats.txt"
  38. $LogFileTarget  = "C:\Users\administrator.CORP\stats-$computer.txt"
  39.  
  40. $PayloadSource  = "C:\Users\administrator.CORP\code\collect-sxs-info.ps1"
  41. $PayloadTarget  = "C:\Users\Administrator\collect-sxs-info.ps1"
  42.  
  43. $DuFileSource   = "C:\Users\administrator.CORP\code\du64.exe"
  44. $DuFileTarget   = "C:\Users\Administrator\du64.exe"
  45.  
  46. [string[]]$RemovableFiles = $LogFileSource, $PayloadTarget, $DuFileTarget
  47.  
  48. #
  49. # Connect  
  50. #
  51. $rs = New-PSSession $computer
  52. Write-Host "<o> $computer - Opened connection $rs"
  53.  
  54. Cleanup-Remote($RemovableFiles)
  55.  
  56. #
  57. # Drop files
  58. #
  59. Copy-Item -ToSession $rs $PayloadSource -Destination $PayloadTarget
  60. Write-Host "--> $computer - Delivered $PayloadTarget"
  61.  
  62. Copy-Item -ToSession $rs $DuFileSource -Destination $DuFileTarget
  63. Write-Host "--> $computer - Delivered $DuFileTarget"
  64.  
  65.  
  66. #
  67. # Run
  68. #
  69. Write-Host -nonewline "<-> $computer - Running collect-sxs-info.ps1...  "
  70. Invoke-Command -Session $rs -ScriptBlock { C:\Users\Administrator\collect-sxs-info.ps1 }
  71. Write-Host "Done"
  72.  
  73. #
  74. # Check data
  75. #
  76. # Invoke-Command -Session $rs -ScriptBlock { Param($path); Get-ChildItem -Path $path } -ArgumentList $LogFileSource
  77. #
  78. Copy-Item -FromSession $rs $LogFileSource -Destination $LogFileTarget
  79. Write-Host "<-- $computer - Received $LogFileTarget"
  80.  
  81.  
  82. #
  83. # Cleanup
  84. #
  85. Cleanup-Remote($RemovableFiles)
  86. Remove-PSSession -session $rs
  87. write-host "<c> $computer - Closed connection $rs"
  88.  
  89.  
Add Comment
Please, Sign In to add comment