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.
Related
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
}
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()
I am trying to remotely invoke a command on a server and change it's IP, amongst other settings. I got the following line of code:
Invoke-Command -ComputerName $currentIP {`
$wmi = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "ipenabled = 'true'";`
$wmi.SetDNSServerSearchOrder($DNSServers);`
$wmi.SetGateways($Defaultgateway);`
$wmi.EnableStatic($newIP,"255.255.255.0")} -Credential $cred
Every setting is applied but at the end of the script it starts going:
The network connection to ###.##.###.## has been interrupted. Attempting to reconnect for up to 4 minutes...
And continues to fail the connection, obviously because the IP has changed. But am I issuing no more commands after the IP is changed. So why does this occur and how can I stop it?
What you are seeing is expected behaviour as WinRM has no idea that the IP is being changed and during the ScriptBlock execution the IP changes which results in connection interrupted
For smoother connection closing, you can run Invoke-Command using -AsJob parameter so that it won't be interrupted eg:
invoke-command -ComputerName 192.168.56.103 -Credential administrator -AsJob -ScriptBlock {$wmi = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "ipenabled = 'true'";$wmi.SetDNSServerSearchOrder("8.8.8.8");$wmi.SetGateways("1.1.1.1");$wmi.EnableStatic("192.168.56.104","255.255.255.0")}
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.
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'