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.

Roise
  • 364
  • 5
  • 22
  • 1
    Have you tried the [PowerShell IIS module](http://www.iis.net/download/powershell)? Check out [Stop-Website](http://technet.microsoft.com/en-us/library/ee790607.aspx). – Andy Arismendi May 11 '12 at 07:38
  • the powershell iis module you are linking to is a snap-in for iss 7.0, and im using 7.5 which uses modules, and isnt that iis module the same as webadmin module ? – Roise May 11 '12 at 08:14
  • It also applies to IIS 7.5, http://learn.iis.net/page.aspx/429/installing-the-iis-powershell-snap-in/ – Lex Li May 11 '12 at 08:24
  • Tried to install the snap-in but it said it was not supported on the current operating system – Roise May 11 '12 at 08:43

2 Answers2

10
Import-Module WebAdministration
Stop-WebSite 'Default Web Site'
Start-WebSite 'Default Web Site'
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • 1
    I have problems with the "Import-Module WebAdministration" if i run it it says thats its already imported: [link](http://technet.microsoft.com/en-us/library/ee790599.aspx), but if i run: "Import-Module -Name webadministration" i get the not digitally signed error. And Stop-WebSite is not recognized as a command – Roise May 11 '12 at 08:39
  • What is your execution policy (Get-ExecutionPolicy)? You should probably set it to the appropriate level before calling import-module. – David Brabant May 11 '12 at 09:11
  • My execution policy is AllSigned – Roise May 11 '12 at 09:45
  • Try "Set-ExecutionPolicy Unrestricted", then try to import the webadministration module. – David Brabant May 11 '12 at 09:51
  • I tested with RemoteSigned now, and the the message was that its default imported as stated in my first comment to this answer. So that rules out my webadministration package theory... – Roise May 11 '12 at 09:51
  • @Roise After set executionpolicy as remotesigned you need to close and re-open powershell console (run as administrator for working with webadministrator module), then `import-module webadministration`. – CB. May 11 '12 at 09:59
  • @Christian When i do this i get no message after i import the module, but i still get the same error when running my own script, so it still seems like i cant fetch any of my websites from IIS. The Stop-WebSite worked though... – Roise May 11 '12 at 10:22
  • Your script is not affected by webadministration module. Change your script for using `stop-website`! – CB. May 11 '12 at 10:42
  • Also, if it's no available you need to run the following to INSTALL the WebAdministration Modules. Add-WindowsFeature Web-Scripting-Tools see this link: https://stackoverflow.com/a/69137154/738895 – m1m1k Aug 20 '22 at 00:10
7

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://learn.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

Amir
  • 1,722
  • 22
  • 20