Advertisement
tseanlaws

tseanlaws.ps1

Oct 13th, 2019
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. <# tseanlaws.ps1
  2. Conditions:
  3. 1. Have the Test-Connection function send an email if the computer was offline, then terminate the script.
  4. 2. If online, run executable.exe
  5. 3. Then, loop thorugh the processes until none are running (officeclicktorun = 2), then force a reboot.
  6.  
  7. VBScipt Sytax:
  8. Set objShell = CreateObject("Wscript.Shell")
  9. objShell.run("powershell.exe -noexit -file .\tseanlaws.ps1") &Input
  10.  
  11. #>
  12.  
  13. $Computername = 'Computer01'
  14.  
  15. #Test-MyConnection -ComputerName $Computername
  16.  
  17. <# origional
  18. param (
  19. [Parameter(Mandatory = $true,
  20. ValueFromPipeline = $true)]
  21. [ValidateNotNull()]
  22. [ValidateNotNullOrEmpty()]
  23. [string]$ComputerName
  24. )
  25. #>
  26.  
  27. function Send-Email {
  28. # param (
  29. [Parameter(Mandatory=$true)]
  30. [String]$Body,
  31. [String]$ComputerName
  32. # )
  33. Send-MailMessage -From “admin@domain.com" -To “tseanlaws@domain.com" -SMTPServer mail.domain.com -Subject “$($computername) is OFFLINE " # -Body $Body
  34.  
  35. }
  36.  
  37. Function Send-Email2 {
  38. # param (
  39. [Parameter(Mandatory=$true)]
  40. [String]$Body,
  41. [String]$ComputerName
  42. # )
  43. Send-MailMessage -From “admin@domain.com" -To “tseanlaws@domain.com" -SMTPServer mail.domain.com -Subject “$($computername) is OFFLINE " # -Body $Body
  44. }
  45.  
  46. function Test-Running {
  47. param (
  48. [Parameter(Mandatory=$true)]
  49. [System.String]$ComputerName
  50. )
  51. $processList = @'
  52. "Name", "Expected", "Running"
  53. "cmd", "1", "0"
  54. "powershell", "1", "0"
  55. "OfficeClickToRun", "2", "0"
  56. '@ | ConvertFrom-Csv | ForEach-Object {$_.Expected = [int]$_.Expected; $_}
  57. $splat = @{}
  58. $splat['ComputerName'] = $computerName
  59. Write-Host "Testing processes on $($computerName)" -ForegroundColor Yellow
  60. Do {
  61. $processList | ForEach-Object {
  62. $_.Running = @(Get-Process $_.Name @splat -ErrorAction SilentlyContinue).Count
  63. }
  64. ($processList | Format-Table -AutoSize | Out-String).Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries) | Write-Host
  65. If ($running = @($processList | Where-Object {$_.Running -ge $_.Expected}).Count) {
  66. Start-Sleep -Seconds 5
  67. }
  68. } Until (-not $running)
  69.  
  70. #/////////////////////////////////////////////////////
  71. #Restart-Computer -Computer $Computername -Force
  72. #/////////////////////////////////////////////////////
  73.  
  74. return 0
  75. }
  76.  
  77.  
  78. function Test-MyConnection {
  79. param (
  80. [Parameter(Mandatory=$true)]
  81. [System.String]
  82. $ComputerName
  83. )
  84. if (Test-Connection $computername -Quiet -Count 2) {
  85. Write-Host "Ping successful on $($computerName)" -ForegroundColor Green
  86. Test-Running -ComputerName $ComputerName
  87.  
  88. #/////////////////////////////////////////////////////
  89. # Invoke-Command -ComputerName $computername { Start-Process -FilePath "c:\executable.exe"
  90. #/////////////////////////////////////////////////////
  91.  
  92. }
  93. else {
  94. send-email $computername -ComputerName $computername -body $processlistOP
  95. Write-Host $($computerName) "is Offline." -ForegroundColor Red
  96. }
  97. }
  98. # } # rem out IF remming out RESTART or invoke to executable
  99.  
  100. Test-MyConnection -ComputerName $computername
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement