Powershell get-service status not working as scheduled task - powershell

I'm running this code below as a function to iterate through a list of servers to check if services are running. This works when run manually. When run from a scheduled task the code $arrService.Status translates to the service name. I even tried something like Get-Service -Computer $Server -display $ServiceName | Select -expandproperty Status but that returns empty string or no value.
Any idea why the status property would not work when run from a scheduled task?
function CheckService{
param($Server, $ServiceName)
$arrService = Get-Service -Computer $Server -display $ServiceName
if ($arrService.Status -ne "Running"){
Start-Service -InputObject $ServerService
}
}

Thanks to #djs I was able to find a solution, albeit not a fantastic solution. Adding the account to the admin group of each server allows the commands to execute. In our case this isn't a problem so we are going to go with this solution.

Related

PowerShell Stopping and Starting Windows Services on Remote Computer

I have a very basic PowerShell that accepts a list of Windows Server and Services on them and then stops the Services
Get-Service -ComputerName machine1 -Name service1 -ErrorAction Stop | Stop-Service -WarningAction SilentlyContinue
Although I had explicitly mentioned -ErrorAction as Stop, in a case if one of the Server is not reachable, the Powershell doesn't come back. How do I change this behaviour at least in a way to stop processing after a certain amount of time as "n" secs which again can be passed thru parameter?
Incorporating both comments, we end up with following
Enclose the current script in a Test-Connection that allows you to first verify if a computer is actually reachable:
if (Test-Connection -ComputerName machine1 -Quiet)
{
all your current code
}

Unable to stop Remote Service using Stop-Service

Get-Service -ComputerName "SERVERNAME" -Name "SERVICENAME" | Stop-Service -ErrorAction Inquire
I am trying to stop a windows service remotely, but it returns an error:
Stop-Service : Cannot find any service with service name 'SERVICENAME.exe'
Even though I specify the correct service name, it seems to add the exe part to the name.
When I Run Get-Service it works perfectly and gets the Service details, but the moment I add | Stop-Service it returns the error above.
Get-Service -ComputerName "SERVERNAME" -Name "SERVICENAME"
I have also tried Get-WmiObject but with no luck.
Everything else seems to work, all commands return the results required except the stop or start service commands where I get this error.
You probably need to pass a -computer name argument to the Stop-Service command. Feels like you may be getting the service from remote computer, then trying to stop the service on local computer.
Try (Get-Service -computername remotepc -name servicename).Stop()

How To Start IIS

When I run the following:
$iis = get-wmiobject Win32_Service -ComputerName $env:computername -Filter "name='IISADMIN'"
I get nothing.
How do I verify that IIS is running in Powershell?
Second, how do I start it, if I determine that it's not running?
Because the above returns nothing, I can only assume this means it isn't running, but I have run IISReset and received the output:
Attempting stop...
Internet services successfully stopped
Attempting start...
Internet services successfully restarted
This implies that it is running, yet the other script indicates that it's not (other script's source is here, and I get a message of "It is not running").
Assuming that it is not running, how do I start IIS with Powershell?
IIS is installed.
First check the service if stopped,then start it if stopped:
$service = Get-WmiObject Win32_Service -ComputerName 'myserver' -Filter "Name='IISAdmin'"
IF($service.State -eq 'Stopped')
{
Get-WmiObject Win32_Service -ComputerName 'myserver' -Filter "Name='IISAdmin'" | Start-Service
"Service started successfully"
}
elseif($service.State -eq 'Running')
{
"Service is already in running state"
}
Apart from this, you can check with Web Server (IIS) Administration Cmdlets.
Hope it helps.

Restart server and poll for started service

If I was to create a script to restart a server and wait for a started service SERVICE123 - NODE1 to come back up, how would I do this?
I have the following:
Restart-Computer -ComputerName COMPUTER123456 wait -For SERVICE123 - NODE1 -Timeout 400 -Delay 5
If not, is there a script I could use to poll for a started service only? I would like it to sit for 20 minutes or so until this service node is up and running.
To see if a Service is running you can use the invoke Command. And execute the
cmdlet get-service on the machine.
$service = Invoke-Command -ComputerName COMPUTER123456 -ScriptBlock { Get-Service -Name $args[0] } -ArgumentList "Service123"
via $service.Status you can see if the Service is Running or Stopped.
Normaly it should work with your code and the restart-Computer cmdlet,
what is the error, is there any error given. Why does your code not work?
I have an idea, you have to put the hyphen to the parameter 'wait' = -Wait
Restart-Computer -ComputerName COMPUTER123456 -Wait -For "SERVICE123 - NODE1" -Timeout 400 -Delay 5
Have a look at MSTechnet
In example 6 you see exactly what you need.
Greets Eldo.Ob

How to remotely check the status of a web application pool with PowerShell?

I see a lot of scripts for recycling application pools on a web server running IIS7 but is there a way to check, with PowerShell, that the web application pool is running or stopped? I can't seem to figure out a way to remotely have Get-WebAppPoolState return the status on the Application Pool, and my Google-fu has not been able to come up with a replacement. I can remotely get gwmi to work and recycle or start my app pools but ideally I only want to have to run this if the app pool is actually stopped.
Would I need to work this out with PSExec or is there an alternative I can use similar to gwmi and have a one line command to call the app pool on the IIS7 server and give me the status?
You can use Invoke-Command to invoke the Get-WebAppPoolState cmdlet on the remote machine.
$appPoolStatus = Invoke-Command -ComputerName RemoteHostName {Import-Module WebAdministration; Get-WebAppPoolState DefaultAppPool}
$appPoolStatus.Value
Note that if you are going to use variables defined locally on the calling machine, you will have to treat them according to the rules. This link is an excellent post explaining them:
http://blogs.msdn.com/b/powershell/archive/2009/12/29/arguments-for-remote-commands.aspx
Example:
$appPoolName = "SomeAppPoolName"
$appPoolStatus = Invoke-Command -ComputerName RemoteHostName { param($apn) Import-Module WebAdministration; Get-WebAppPoolState $apn} -Args $appPoolName
You can use this if you want pretty out:
Invoke-Command -ComputerName RemoteHostName -ScriptBlock { Get-WebAppPoolState | % { return #{($_.itemxpath -split ("'"))[1]="$($_.value)" } }}
For older version of IIS (before version 8):
$items = get-wmiobject -namespace 'root\MicrosoftIISv2' -computername $server -class 'IIsApplicationPoolSetting'