Is there a way to query Server Manager using PowerShell - 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

Related

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.

How to stop Windows Services via Powershell with AutomaticDelayed start type?

I was able to write a Powershell 2.0 script that that stops certain running services, which I use in an Ansible script. The Ansible script reboots the VM first, then runs the script.
# Get a list of running XYZ_* services and store them in an array.
$runningExaServices = Get-Service | Where-Object {$_.Name -like "*XYZ_*"} | Where-Object {$_.Status -eq "Running"}
# Iterate throgh the services and stop all of them except EXA_Web,
# EXA_Web_APIs, EXA_Nginx, EXA_Redis. We'll stop those separately
Foreach($service in $runningExaServices) {
Write-Host "Stopping: "$service.name
Stop-Service -Name $service.name -Force
$svc = Get-Service | Where-Object {$_.Name -eq $service.name}
Write-Host $svc.name: $svc.status
}
The XYZZ_* services will indeed stop. However, the services with Automatic (Delayed Start) would start running again. What's the secret to keeping them stopped? TIA
Since Get-Service doesn't expose the "Automatic (Delayed start)" configuration, you'll have to rely on just the Automatic value for StartType. If you want them to stay stopped, switch the starttype value to "Manual":
# Get a list of running XYZ_* services and store them in an array.
$runningExaServices = Get-Service -Name "*XYZ_*" | Where-Object {$_.Status -eq "Running"} #| Stop-Service -PassThru
Foreach($service in $runningExaServices) {
#Write-Host "Stopping: $($service.name)"
if ($service.StartType -eq "Automatic") {
$Serice | Set-Service -StartupType 'Manual'
}
Stop-Service -Name $service.name -Force -PassThru
# Write-Host "$($svc.name): $($svc.status)"
}
I'm not too sure when these cmdlets were introduced but, I will go ahead and take a guess that Set-Service was released at the same time as Get-Service; i.e. PowerShell 2.0.
With that said, a simple if condition/statement can check for the starttype value and proceed accordingly by setting it to "Manual". Then, stopping the service(s).
Couple of side notes:
You don't need the loop if you pipe directly to Set-Service, and Stop-Service given the following circumstances:
You don't care about setting the starttype to "Manual"
You don't care about the iteration process, and just having them all stop at once with a -PassThru switch provided; which in turn outputs the object with the new status of "Stopped".
Lastly, - referring back to the 2nd sub-bullet point above - -PassThru is what I would suggest rather than stating what current service object you're on and re-querying the same object for the new status.

Use PowerShell to install Windows Updates in vSphere

I have a cluster of vSphere windows clients approximately 100 that I want to remotely automate windows updates on weekly. I have listed all the windows machine out in text file on my desktop. I have run the PSWindowsUpdate module on my local windows10 machine with command:
Install-Module -Name PSWindowsUpdate and then executed the below script successful for my local machine to run windows updates.
#Import-Module PSWindowsUpdate
#Remove-Item -Path C:\Scripts\status.tx
#Start-Transcript -Path C:\Scripts\status.txt
#$Updates = "Critical Updates", "Security Updates"
#Get-WUInstall -AcceptALL- Verbose -IgnoreReboot -Category $Updates
#Write-Host "Done"
#Stop-Transcript
#Start-Sleep -s 120
#Restart-Computer -Force -Confirm:$false
-- after pc restarts run as PS As Administrator
#Get-WindowsUpdate
However, I am not a expert at PowerShell so, I do not know what to additionally script to accomplish the task of remotely updating 100 or so windows clients in vSphere.
Any suggestion would be appreciated.
You can try with the invoke-command. You can create a server list from a DC:
$Servers = Get-ADObject -Filter * -Properties Name,OperatingSystem | Where-Object OperatingSystem -like '*Server*'
And use this list with a loop like this
ForEach($_ in $Servers)
{
Invoke-Command -ScriptBlock {Get-WUInstall -AcceptALL- Verbose -IgnoreReboot -Category $Updates } -ComputerName $_.Name -ErrorAction SilentlyContinue
}

Powershell to query specific Service in enterprise?

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

How to get process not responding on remote server with Powershell

I don't understand, responding is empty on remote serveur.
Get-Process explorer -ComputerName vcass1 | select name,id,responding
Name Id Responding
---- -- ----------
explorer 1204
explorer 3020
but responding i real empty :
Get-Process explorer -ComputerName vcass1 | Where-Object {$_.Responding -eq $true}
>> No result
Get-Process explorer -ComputerName vcass1 | Where-Object {$_.Responding -eq $false}
>> No result
when i try with : -ComputerName localhost i have the same problem, but without -ComputerName i have value $True for each process !
According to the help on MSDN you should get a NotSupportedException error indictaing that:
You are attempting to access the Responding property for a process
that is running on a remote computer. This property is available only
for processes that are running on the local computer.
If the remote server is on Windows 2012 you can read the Responding status remotely.