Stopping website with powerShell script

2

Ive got a script for stopping a site:

param($HostName = "localhost", $SiteName)

$server = $HostName
$siteName = $SiteName
$iis = [ADSI]"IIS://$server/W3SVC"
$site = $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $siteName}
$site.start()

# SIG # Begin signature block ...

But when i run the script on a server with high security policies i get this error:

The following exception was thrown when trying to enumerate the collection: "Unknown error (0x80005000)".
At D:\DeploymentScripts\Common\StopSite.ps1:6 char:8
+ $site = <<<<  $iis.psbase.children | where { $_.keyType -eq "IIsWebServer" -AND $_.ServerComment -eq $siteName}
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : ExceptionInGetEnumerator

From what I have read this might occur if i don't have access to the IIS , but im running the script as admin, should that not grant me the access i need ?

Does the webadministration module give more access rights ? I don't import this since i didn't have to do this on the other servers, if i need to import the webadministration there is another problem, when i try i get a error saying the WebAdministrationAliases.ps1 is not digitally signed...

I have tested the script on other servers with no problem, but this one got more strict policies as mentioned above, and its not a option to change the policies.

Im running this on a Windows server 2008 R2, with IIS 7.5.

iis
powershell
windows-server-2008
asked on Stack Overflow May 11, 2012 by Roise

2 Answers

8
Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'
answered on Stack Overflow May 11, 2012 by David Brabant
2

Based on David's Answer:

In recent version of Windows (10, 2016, and later), you need to import IISAdministration instead of WebAdministration

Import-Module IISAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'

For older version of Windows and IIS, you need to do

Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'

Read More:

1- https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-10/iisadministration-powershell-cmdlets

2- https://blogs.iis.net/iisteam/introducing-iisadministration-in-the-powershell-gallery

answered on Stack Overflow Nov 19, 2018 by Amir

User contributions licensed under CC BY-SA 3.0