Powershell to query specific Service in enterprise? - powershell

Good day good people.
Would someone please help me out I am trying to PS our enterprise servers to find all assets with a particular service on it and having no luck.
I tried
$servicename = "SERVICE_NAME"
$list = get-content "c:\security\comp_list.txt"
foreach ($server in $list) {
if (Get-Service $servicename -computername $server -ErrorAction 'SilentlyContinue'){
Write-Host "$servicename exists on $server
}
Any suggestions would be greatly appreciated. I'm still fairly new to PS.

My accepted answer to why Get-Service -ComputerName fails can be found in the link below and the resolution to the issue.
Powershell Results to Slack via Webhook question - Remote Server results
Summary
Differences between Windows PowerShell 5.1 and PowerShell 7.x
Please Note In Windows 7.2 the Get-Service command made use of DCOM and such functionality like '-ComputerName' is removed.
https://learn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell?view=powershell-7#remove--computername-from--service-cmdlets-5090
$server = $env:Computername
Invoke-Command -Computername $server -Scriptblock {Get-Service | where status -eq 'started;}
Get-Service cmdlet
The cmdlet gets the members, the properties and methods, of objects
get-service | get-member | sort Name
Names of services that start / contain 'App'
$Results = Get-Service -Name App | Select Name

Related

Is there a way to query Server Manager using PowerShell

Part of our daily process is to RDP to a remote machine and check the services are running. We have to check “File and Storage Services”, “IIS”, “Local Server”, and “All Servers” (see image).
Can I do this remotely through PowerShell? I have a script (Get-Service -ComputerName [remote computer name]), but which services do I list to check these 4 main areas are running?
Server Manager
Tried:
get-service -Name "LanmanServer", "LanmanWorkstation" -ComputerName [computername]
get-service -ComputerName [computername] | Where-Object {$.Status -ne "Running"}
get-service -ComputerName [computername] | Where-Object {$.Status -eq "Running"}
So I can list the services, and if they're running or not, but this doesn't tell me what I need

How do i get power shell to show a few services from my servers and not them all

this is my code i want it to show a few services from my servers but it keeps showing all of them. i tried using -Name but power shell 7 keeps saying that doesn't exist please help
$offlineServices = (Invoke-Command -ComputerName $server.Name {Get-service [string]$server.Value | `
Where-Object{$_.status -eq 'Stopped'}} ).Name
Get-Service can be used directly against remote servers, like this:
Get-Service -Name $server.Value -ComputerName $server.Name |
Where-Object Status -eq 'Stopped'
If you want to stick with your original remoting technique, you need to use the using modifier:
$offlineServices = (Invoke-Command -ComputerName $server.Name -Script {Get-service $using:server.Value |
Where-Object Status -eq 'Stopped'}).Name
NOTE: you should also remove the backtick before Where-Object as it isn't needed and might cause you issue later when modifying/debugging the code.

Stopping Windows WMI Services

im trying to stop some Windows Services remotely on different servers but only those with a defined logon name. I already collecting the Services like this. Could you please help?
Next Step should be to stop the collected Services. Thank you!!
$Servers = #('server1','server2')
foreach ($i in $Servers){
Get-WmiObject win32_service -ComputerName $i | where {($_.startname -eq "Username") -or ($_.startname -eq "Username")} | select #{n='Hostname';e={$i}} ,DisplayName,Name,Startname |Out-File C:\Temp\Out.txt -Append
}

Get Service from remote machines in a domain

Trying to get list of all machines in a Domain with a certain service
tried via all posts in here, helped per one machine, but if i use a text file with multiple machines, it failes
$computers = Get-Content c:\script\computers.txt
$service = "*crystal*"
foreach ($computer in $computers) {
$servicestatus = Get-Service -ComputerName $computer -Name $service
}
$Data = $servicestatus | Select-Object Name,Machinename | Format-Table -AutoSize
Write($Data) | Out-File c:\script\output.txt -Append
Expected list of machines with service in table, instead got error:
This operation might require other privileges
same script, but with a direct machine name, works like a charm.
Any clue what is wrong?
Why not use:
Invoke-Command -ComputerName $computers -ScriptBlock {Get-Service -Name *crystal*}
Eventually you may store the result from invoke into a variable and work with it.
The benefit of using Invoke-Command, insted of foreach is that Invoke works in parallel, while foreach is serial ...
Hope it helps!
Best regards,
Ivan

Powershell Script - Search AD and then restart service

I have created the below powershell script which looks at a local active directory, and searches for a particular string within the description field.
Once the script has found the particular computer name from the above search it starts the service in question.
$FullName = "*Username*"
$LookUp = Get-ADComputer -Filter {Description -Like $FullName} -Properties Description | Select DNSHostName
$ComputerName = $LookUp
$ServiceName = "Service Name"
Get-Service -Name $ServiceName -ComputerName $ComputerName | Set-Service -Status Running
The issue I am having is that the Powershell script returns a 'service not found' message when I know (have manually checked) that the service exists?
Any help would be greatly appreciated.
Thanks
Does the service start if you run only that line on the target machine?
ADDITION----------------------
I think you need to do this:
$ComputerName = $LookUp.DNSHostname