Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Find-Service #31

Merged
merged 4 commits into from
Oct 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 59 additions & 70 deletions Public/Find-Service.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Function Find-Service {
function Find-Service {
<#
.SYNOPSIS
Find a specified service on remote endpoints
.PARAMETER Computername
.PARAMETER ComputerName
The remote endpoint(s) to query
.PARAMETER ServiceDisplayName
The Display Name of the service to query
Expand All @@ -15,89 +15,78 @@ Function Find-Service {
.EXAMPLE
(Get-ADComputer -Filter * -Searchbase $ou).Name | Find-Service -Service 'Windows Audio'
#>
[cmdletBinding()]
Param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipeLineByPropertyName)]
[Alias('Name')]
[string[]]
$ComputerName,

[Parameter(Mandatory,Position=1)]
[Alias('Service')]
[string]
$ServiceDisplayName
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory, ValueFromPipelineByPropertyName)]
[Alias('PSComputerName', 'PC')]
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName,

[Parameter(Mandatory, Position = 1, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('Name', 'ServiceName')]
[ValidateNotNullOrEmpty()]
[string[]]
$ServiceDisplayName
)

Process {

$Computername | ForEach-Object {

If(!(Test-Connection $psitem -Count 1 -Quiet)){
$offlineError = [pscustomobject]@{
Computername = $psitem
TargetOffline = $true
Message = "Ping failed to this endpoint"
process {
$ComputerName | ForEach-Object {
if (-not (Test-Connection -ComputerName $_ -Count 1 -Quiet)) {
Write-Error [PSCustomObject]@{
Computername = $_
TargetOffline = $true
WSManAvailable = $null
Service = $null
Message = 'Ping Failed'
}

Return $offlineError
return
}



Else {

Try {

Test-WSMan $psitem -ErrorAction Stop | Out-Null

}

Catch {

$WSManError = [pscustomobject]@{
Computername = $tryitem
WSManError = $true
Message = "Unable to remote to endpoint"
else {
try {
Test-WSMan -ComputerName $_ -ErrorAction Stop > $null
}

return $WSManError

}

Invoke-Command -ComputerName $psitem -ArgumentList $ServiceDisplayName -ScriptBlock {
Param($Params)
$Service = Get-Service -DisplayName $Params
If($Service){

$ServiceData = [pscustomobject]@{
Computername = $env:COMPUTERNAME
Service = $Service.DisplayName
StartupType = $Service.StartType
Status = $Service.Status
catch {
Write-Error [PSCustomObject]@{
Computername = $_
TargetOffline = $false
WSManAvailable = $false
Service = $null
Message = "WSMan Unresponsive"
}

return $ServiceData
return
}

}#inner if
Invoke-Command -ComputerName $_ -ArgumentList $ServiceDisplayName -ScriptBlock {
param($Params)

Else {
$Service = Get-Service -DisplayName $Params

$ServiceData = [pscustomobject]@{
Computername = $env:COMPUTERNAME
Service = $Params
Error = "$Params not found on this system"
if ($Service) {
return [PSCustomObject]@{
Computername = $env:COMPUTERNAME
Service = $Service.DisplayName -join ', '
StartupType = $Service.StartType
Status = $Service.Status
}
}

return $ServiceData

}#inner else

}#invoke
else {
Write-Error [PSCustomObject]@{
Computername = $env:COMPUTERNAME
TargetOffline = $false
WSManAvailable = $true
Service = $Params -join ', '
Message = "$Params not found on this system"
}
}#inner else

}#invoke

}#outer else

}#Foreach

}#process

}#Function
}#function