Skip to content

Instantly share code, notes, and snippets.

@Alcha Alcha/JBHunt-2.ps1
Last active Jan 11, 2019

Embed
What would you like to do?
<#
===========================================================================
Created on: 1/11/2019 10:28
Created by: jisodl0
Organization: J.B. Hunt
Filename: Artemis.psm1
-------------------------------------------------------------------------
Module Name: Artemis
===========================================================================
#>
<#
.SYNOPSIS
Restart a service used by J.B. Hunt.
.DESCRIPTION
Restarts the given service on the given computer so long as you have the necessary permissions. This is done by retrieving the service from the given computer name, stopping that service, then starting it back up. Each step requires confirmation before continuing to the next to avoid any issues.
.PARAMETER ServiceName
A description of the ServiceName parameter.
.PARAMETER ComputerName
The name of the computer/server to restart the given service on. Defaults to the local machine if no value is provided.
.EXAMPLE
PS C:\> Restart-JBService -ServiceName 'Value1'
.NOTES
Additional information about the function.
#>
function Restart-JBService {
[CmdletBinding(PositionalBinding = $true)]
param (
[Parameter(Mandatory = $true,
Position = 0)]
[Alias('Name', 'Service')]
[System.String]
$ServiceName,
[Parameter(Position = 1,
HelpMessage = 'Which computer would you like to reset the service on?')]
[Alias('Computer', 'ServerName', 'Server')]
[System.String]
$ComputerName = $env:COMPUTERNAME
)
$Service = Get-Service -Name $ServiceName -ComputerName $env:COMPUTERNAME
if ($Service.Length -gt 1) {
$Services = @()
for ($x = 0; $x -lt $Service.Length; $x++) {
[hashtable]$Data = @{}
$Data.Add('Index', $x)
$Data.Add('Status', $Service[$x].Status)
$Data.Add('Name', $Service[$x].Name)
$DataTable = New-Object -TypeName System.Management.Automation.PSObject -Property $Data
$Services += $DataTable
# $Temp = @{
# 'Index' = $x;
# 'Status' = $Service[$x].Status;
# 'Name' = $Service[$x].Name;
# }
# $Temp = New-Object -TypeName System.Management.Automation.PSObject
# $Temp | Add-Member -MemberType NoteProperty -Name 'Index' -Value $x
# $Temp | Add-Member -MemberType NoteProperty -Name 'Status' -Value $Service[$x].Status
# $Temp | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Service[$x].Name
# $Services.Add()
}
Write-Output 'Returned services:'
Write-Output $Services
$Index = Get-Index $Services $x
} else {
$CorrS = Get-CorrectService
if ($CorrS.StartsWith('y') -or ($CorrS.Length -eq 0)) {
Stop-Service $Service
$ContFlag = Get-ContinueFlag
if ($ContFlag.StartsWith('y') -or ($ContFlag.Length -eq 0)) {
Start-Service $Service
}
}
}
}
function Get-Index {
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[System.Management.Automation.PSObject]
$Services,
[Parameter(Mandatory = $true,
Position = 1)]
[int]
$x
)
return Read-Host "Which service would you like to restart? (0 - $x)"
}
function Get-CorrectService {
$CorrectService = Read-Host -Prompt 'Is this the correct service? (Y/N)'
if ($CorrectService.Length -gt 0) {
return $CorrectService.ToLower()
} else {
return $CorrectService
}
}
function Get-ContinueFlag {
$Continue = Read-Host -Prompt 'Continue with restart? (Y/N)'
if ($Continue.Length -gt 0) {
return $Continue.ToLower()
} else {
return $Continue
}
}
Restart-JBService -ServiceName 'wi*'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.