How do I start and stop several IIS Applicationpools at once with a PowerShell script - powershell

I would like to create a PowerShell script that can Start and Stop several IIS Applicationpools at once.
I already found a similar article about this: How to start and stop application pool in IIS using powershell script
But I would like to create a PowerShell script where I can define more than one IIS Application to stop them all at once through that script.
Thank you in advance for the help

The link you provided already has a solution very close to what you need; look for "Stop all application pools script".
If you want to start/stop only some AppPools, put them in an array: replace $AppPools=Get-ChildItem IIS:\AppPools | Where {$_.State -eq "Started"} with $AppPools=#('server1', 'server2', 'server3'):
$AppPools=#('server1', 'server2', 'server3')
ForEach($AppPool in $AppPools) {
Stop-WebAppPool -name $AppPool.name
}

Related

Powershell script check the status of the stopped services and send mail

I am pretty new to Powershell scripting and i have a requirement to check the status of the services, if the services are stopped ,capture the status into a separate text file and send the email status to the users that services got stopped. In my environment services runs on two different machines . How to achieve it using Get -service
Please help me.
Since it looks like you haven't started, I'll give you a few things to kick you off.
Get-Service | Where-Object {$_.status -eq "stopped"}
This is a way to retrieve all the stopped services. Get-Service gets all the services, Where-Object is like an if statement in programming, and the part in parenthesis is the condition to meet.
Here's a link where you can read more about that: https://technet.microsoft.com/en-us/library/ee176858.aspx
This is a line of code that will write processes to a text file:
Get-Process | Out-File c:\scripts\test.txt
You can see how combining different commands will get you the result you want. Search around the internet if you get stuck. PowerShell is pretty well documented.
Here's a link for some more research on writing to a file: https://technet.microsoft.com/en-us/library/ee176924.aspx

PowerShell wait application to launch

I have the following script to launch an application:
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
$args = "arguments"
$proc = Start-Process -PassThru "path" -ArgumentList $args
start-sleep -Seconds 5
[Microsoft.VisualBasic.Interaction]::AppActivate($proc.Id)
[System.Windows.Forms.SendKeys]::SendWait("~")
I use this script to launch several windows forms applications that need user interaction to start (i.e. push a start button). So far I've been sleeping the script 5 seconds to allow the application to launch. I tried to run this script in a variety of computers with different processing capabilities, but did not work in all computers, some launch the application slower than others, when the app took more than 5 seconds to launch the app the script fails, because the AppActivate could not find the PID.
I don't want to try different sleeping times for each computer, because I have to run this script in more than 100 computers at boot time.
I would like to know if there is a way to wait for the application to launch in a event driven way.
UPDATE:
The WaitForInputIdle does not work in all applications I tried to start. It returns immediately (successfully because it's returning true) and the AppActive method throws an exception.
I suggest you to avoid using "Sleep" to synchronize system objects (it's never a good solution). As far as you are starting Windows Forms application I suggest you to use Process.WaitForInputIdle Method.
$p = [diagnostics.process]::start("notepad.exe", "D:\temp\1mbfile.txt")
$p.WaitForInputIdle(5000);
using it in a loop you can test alternatively test if the input is idle (application is started and waiting) or if the application is stopped ($p.HasExited -eq $true).
do
{
if ($p.HasExited -eq $true)
{
break
}
} while ($p.WaitForInputIdle(5000) -ne $true)
So, I finally wrote this workaround since WaitForInputIdle does not work with all my applications and I was not able to figure out why.
do{
if ($proc.MainWindowHandle -ne 0)
{
break
}
$proc.Refresh()
} while ($true)
I am not sure if this is the best approach; anyway, I wanted to share this.
Neither of the other answers waited long enough for my script, so I came up with this, which waits until the process stops using the cpu.
do{
$ProcCpu = $Process.CPU
Start-Sleep -Milliseconds 100
} until($ProcCpu -eq $Process.CPU)

Is there a powershell replacement for Repadmin /syncall

Currently we are using the command repadmin /syncall /e [our dn] and repadmin /syncall /eP [our dn] to force replication betwen domain controllers. I am wanting to use powershell to sync the domain controllers but everything I see online indicates that I would have to simply call repadmin from within powershell, which to me seems hokey and like duct taping something instead of doing it right. Is there any PURE powershell equivelant of repadmin /syncall?
I wrote this pure powershell function/script back last year to do exactly this and it looks like maybe that's where the other posted PS snippet answer here came from (I'll take as compliment). The other post saying it is not possible is absolutely incorrect this ADSI call and my script does in fact force a full sync just like a Repadmin /syncall simply test it and you will see - I use it quite a bit. It also does debug output and proper error checking. Here's the link to the Pure Powershell script on the MSDN site:
http://bit.ly/SyncADDomain
and the github repo where I even have the pure powershell script packaged into a MSI installer for simple deployment as well:
https://github.com/CollinChaffin/SyncADDomain
If you find it helpful please mark as answer. Thanks!
AFIAK there's not a full replacement for repadmin. Sync-ADObject will let you replicate a single object, but won't let you do a full sync. Also, that cmdlet is Windows 8.1/Windows Server 2012 R2 only. I would expect more comprehensive AD replication support in Windows Server vNext.
Give this script a try:
[CmdletBinding()]
Param([switch]$AllPartitions)
$myDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain();
ForEach ($dc in $myDomain.DomainControllers) {
$dcName = $dc.Name;
$partitions = #();
if ($AllPartitions) {
$partitions += $dc.Partitions;
} else {
$partitions += ([ADSI]"").distinguishedName;
}
ForEach ($part in $partitions) {
Write-Host "$dcName - Syncing replicas from all servers for partition '$part'"
$dc.SyncReplicaFromAllServers($part, 'CrossSite')
}
}

How to find all VM/Hyper-V and then reboot them on Window Server 2012 with the use of Power-shell

I would like to ask how to find all VM/Hyper-V on Window Server 2012 with the use of power-shell and then restart/reboot them with the use of power-shell script?
The next thing is:
I cannot even find any get command for it:
==================
Added in 2014-08-15
have find a reference link that help me a lot, just share here
http://technet.microsoft.com/en-us/library/hh848479.aspx
Run Powershell elevated (!) and execute following:
ForEach ($VM in Hyper-V\Get-VM | Where {$_.State -ne "Off") {
Restart-Computer -ComputerName $VM.Name
}
Non-elevated Powershell will not return any results.
Martin

Start/Stop App Pool IIS6.0 with Powershell or command line

I'm using IIS 6.0 and looking for a way to stop/start the app pool. I know there is a stop-appPool for powershell in 7.0 but using 6.0. :-( So does anyone have a powershell script or another command line exe that will stop/start the app pool?
Thanks.
Ok here it is, I just add a switch to stop the app pool else it starts since no harm in starting an app pool that is already started:
param([string]$appPoolName, [switch]$stop)
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | where-object {$_.Name -eq "W3SVC/AppPools/$appPoolName"}
if($appPool)
{
if($stop)
{
$appPool.Stop()
}
else
{
$appPool.Start()
}
}
If anybody is looking for a purely command-line tool that does not require Powershell, I have created such a thing based on the information contained in these other answers. Since the original question is specifically looking for possible command-line alternatives, I thought I would share it here.
Usage is quite simple:
IIS6AppPool Start DefaultAppPool
IIS6AppPool Stop AppPool #1
IIS6AppPool Recycle Some other app pool
Source and binaries are available on bitbucket. May this save somebody else a few minutes of head scratching.
You might be interested in this Powershell library I started maintaining:
psDeploy : http://rprieto.github.com/psDeploy/
Among other things it has lots of cmdlets for IIS6 automation, for example Start-IIS6AppPool, New-IIS6Website...
I hope it helps!
If on Windows Server 2003 it is simpler to use the supplied script iisapp.vbs
CScript.exe C:\WINDOWS\system32\iisapp.vbs /?
CScript.exe C:\WINDOWS\system32\iisapp.vbs /a MyApp /r
Or depending on your setup (default to Cscript not WScript), simply
iisapp /a MyApp /r
And of course it is different in IIS7
If you wish to do this remotely, and / or on a machine without powershell you can modify the script posted here.
It uses WMI to access and recycle the app pool, from VBScript. It's a trivial change to make it stop / start pools instead of recycling them, you just need to call .Stop or .Start on the app pool in question.
The meat of the script is paraphrased below:
strServer = "LocalHost" 'Server name goes here
strAppPoolName = "MyAppPool" 'App pool name goes here
'Connect to the specified server using WMI
set Locator = CreateObject("WbemScripting.SWbemLocator")
Locator.Security_.AuthenticationLevel = 6
set Service = locator.connectserver(strServer,"root/MicrosoftIISv2")
'Get a collection of WMI apppools
set APCollection = Service.InstancesOf("IISApplicationPool")
For each APInstance in APCollection
If UCase(ApInstance.Name) = UCase("W3SVC/AppPools/" & strAppPoolName) Then
WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
' You can do any of these things depending you what you want to do.
APInstance.Recycle
APInstance.Stop
APInstance.Start
End If
Next
If you have some kind of command line / batch toolchain which you want to integrate this into, you can execute a VBScript file in command line mode by calling:
CScript.exe \NoLogo MyScriptFile.vbs
The \NoLogo switch removes the VBScript interpreter startup messages and running it with CScript.exe means that calls to WScript.Echo go to the command line rather than a popup window.
You could create a function to stop or start the application pool remotely as below:
function StopOrStartAppPool($RemoteServerName, $AppPoolName, $commandWebPool)
{
if ($commandWebPool -eq "Stop")
{
$wmiprocess = [wmiclass]"\\$RemoteServerName\root\cimv2:win32_process"
$wmiprocess.create("cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs STOP_SERVER W3SVC/AppPools/$AppPoolName -s:$RemoteServerName")
}
else
{
$wmiprocess = [wmiclass] "\\$RemoteServerName\root\cimv2:win32_process"
$wmiprocess.create("cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs START_SERVER W3SVC/AppPools/$AppPoolName -s:$RemoteServerName")
}
}