Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# tseanlaws.ps1
- Conditions:
- 1. Have the Test-Connection function send an email if the computer was offline, then terminate the script.
- 2. If online, run executable.exe
- 3. Then, loop thorugh the processes until none are running (officeclicktorun = 2), then force a reboot.
- VBScipt Sytax:
- Set objShell = CreateObject("Wscript.Shell")
- objShell.run("powershell.exe -noexit -file .\tseanlaws.ps1") &Input
- #>
- $Computername = 'Computer01'
- #Test-MyConnection -ComputerName $Computername
- <# origional
- param (
- [Parameter(Mandatory = $true,
- ValueFromPipeline = $true)]
- [ValidateNotNull()]
- [ValidateNotNullOrEmpty()]
- [string]$ComputerName
- )
- #>
- function Send-Email {
- # param (
- [Parameter(Mandatory=$true)]
- [String]$Body,
- [String]$ComputerName
- # )
- Send-MailMessage -From “admin@domain.com" -To “tseanlaws@domain.com" -SMTPServer mail.domain.com -Subject “$($computername) is OFFLINE " # -Body $Body
- }
- Function Send-Email2 {
- # param (
- [Parameter(Mandatory=$true)]
- [String]$Body,
- [String]$ComputerName
- # )
- Send-MailMessage -From “admin@domain.com" -To “tseanlaws@domain.com" -SMTPServer mail.domain.com -Subject “$($computername) is OFFLINE " # -Body $Body
- }
- function Test-Running {
- param (
- [Parameter(Mandatory=$true)]
- [System.String]$ComputerName
- )
- $processList = @'
- "Name", "Expected", "Running"
- "cmd", "1", "0"
- "powershell", "1", "0"
- "OfficeClickToRun", "2", "0"
- '@ | ConvertFrom-Csv | ForEach-Object {$_.Expected = [int]$_.Expected; $_}
- $splat = @{}
- $splat['ComputerName'] = $computerName
- Write-Host "Testing processes on $($computerName)" -ForegroundColor Yellow
- Do {
- $processList | ForEach-Object {
- $_.Running = @(Get-Process $_.Name @splat -ErrorAction SilentlyContinue).Count
- }
- ($processList | Format-Table -AutoSize | Out-String).Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries) | Write-Host
- If ($running = @($processList | Where-Object {$_.Running -ge $_.Expected}).Count) {
- Start-Sleep -Seconds 5
- }
- } Until (-not $running)
- #/////////////////////////////////////////////////////
- #Restart-Computer -Computer $Computername -Force
- #/////////////////////////////////////////////////////
- return 0
- }
- function Test-MyConnection {
- param (
- [Parameter(Mandatory=$true)]
- [System.String]
- $ComputerName
- )
- if (Test-Connection $computername -Quiet -Count 2) {
- Write-Host "Ping successful on $($computerName)" -ForegroundColor Green
- Test-Running -ComputerName $ComputerName
- #/////////////////////////////////////////////////////
- # Invoke-Command -ComputerName $computername { Start-Process -FilePath "c:\executable.exe"
- #/////////////////////////////////////////////////////
- }
- else {
- send-email $computername -ComputerName $computername -body $processlistOP
- Write-Host $($computerName) "is Offline." -ForegroundColor Red
- }
- }
- # } # rem out IF remming out RESTART or invoke to executable
- Test-MyConnection -ComputerName $computername
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement