Error using Get-WebSite in PreDeploy because of FileNotFoundException

Simon Q's Avatar

Simon Q

25 Feb, 2013 01:20 PM

Hi Paul,

I have a Powershell error that only happens when I run a script through a Tentacle on one Windows 7 (64-bit) machine.

If I run (as an example):

Import-Module WebAdministration
Get-WebSite -Name "Default Web Site"
I get the following error:

ERROR: Get-WebSite : Could not load file or assembly 'Microsoft.IIS.PowerShell.Framework' or one of its dependencies. The system cannot find the file specified.

However, if I run the same through the Powershell.exe it works correctly. I assumed that there was something I needed to do to make the custom powershell host that the tentacle uses pick up the correct version.

Finally, out of desperation I tried some other commands and got to the point where I had the following (ridiculous) script:

Import-Module WebAdministration
Get-WebSite -Name "Default Web Site"
Get-WebSite -Name "Default Web Site" #yes, it is the same line - not a typo!

And, guess what?! The first "Get-WebSite" fails with the FileNotFoundException and the second one works fine! Is it a timing issue with loading the assemblies in the Powershell Host? And, do you have a workaround for this?

I am using version 1.3.5.1564.

Thanks,

  1. Support Staff 1 Posted by Paul Stovell on 26 Feb, 2013 04:04 AM

    Paul Stovell's Avatar

    Hi Simon,

    Thanks for the bug report. I'm glad you found a workaround and I'm sorry it was such a frustrating issue.

    Our custom PowerShell host does seem to have incompatabilities with PowerShell and unfortunately there's not a lot of guidance form Microsoft on how to create a real, 100% compatible PowerShell host. So in our next release, we're going to include a few different PowerShell invocation options:

    https://trello.com/card/alternative-powershell-execution-modes/4e90...

    Hopefully this will iron out these kinds of issues in future.

    Paul

  2. 2 Posted by Simon Q on 26 Feb, 2013 12:36 PM

    Simon Q's Avatar

    Hi Paul,

    I'll watch out for new feature. In the meantime I got around the issue with something like this:

    Import-Module WebAdministration
    Try{
        $site = Get-WebSite -Name "Default Web Site"
    } Catch [System.IO.FileNotFoundException]{
        $site = Get-WebSite -Name "Default Web Site" 
        Break
    } Finally {
        Write-Host $site.name # or do whatever meaningful thing we want to do...
    }
    

    In case that helps anyone else.

    Thanks,
    Simon

  3. 3 Posted by Debra on 13 Feb, 2014 12:07 PM

    Debra's Avatar

    Has this issue been resolved? I'm having the same problem as above.

  4. 4 Posted by Nicholas Blumha... on 17 Feb, 2014 10:59 PM

    Nicholas Blumhardt's Avatar

    Hi Debra,

    We've discovered subsequently that this issue occurs in PowerShell run outside of Octopus as well - it seems to be related to some broken error code checking in PowerShell itself.

    Most of the time it can be resolve by writing something to the console before running the problem line, e.g. :

    Write-Output "About to run tricky PowerShell"
    # Other PS command goes here
    

    Hope this helps,
    Nick

  5. 5 Posted by Debra on 19 Feb, 2014 09:22 AM

    Debra's Avatar

    Hi Nick,

    Thanks for the update. I also noticed that the error occurs outside of Octopus. It wasn't resolved by writing something to the console but the try catch solution seems to work.

    Thanks,
    Debra

  6. 6 Posted by Aaron Powell on 27 Feb, 2014 01:02 AM

    Aaron Powell's Avatar

    I just thought I'd drop a note here, I've been having the error too, but not within Octopus (we aren't using it here, you just came up as a top search hit :P).

    The problem that we hit was related to the IIS SnapIn. I have a script that was looking for the module and the snapin and then loading the appropriate one. It seemed that the get-pssnapin -Registered | select-string -pattern 'WebAdministration' command is the problem.

    Our solution was to look for the module with get-module, if it exists then import it, else look for the snapin.

  7. 7 Posted by Claus Topholt on 02 Apr, 2014 03:12 PM

    Claus Topholt's Avatar

    And I thought I would chime in as well: We have the same problem but with Powershell 3 itself (and I also found this post via search engine). Interestingly, in our scenario running the command mentioned by Aaron above actually seems to FIX it:

    get-pssnapin -Registered | select-string -pattern 'WebAdministration'
    get-website

    This makes it work 100% of the time. Completely strangely, if we omit the select-string, it WONT work. I don't even.

  8. 8 Posted by Mark Dasco on 11 Jul, 2014 12:55 AM

    Mark Dasco's Avatar

    Hi guys,

    We never had this problem - but now have it with the upgrade to 2.4.10.235

    This seems to point to the interaction between powershell and octopus.
    That module is definitely installed in our boxes and our previous version of octopus deploy worked.

    Can someone look into this?

    Dasco

  9. Support Staff 9 Posted by Paul Stovell on 17 Jul, 2014 03:13 AM

    Paul Stovell's Avatar

    Hi Mark,

    Thanks for getting in touch! Can you share some more information about the errors you are getting, the version of Windows you are using, whether 64 or 32-bit, and what your script looks like? This is a rather old thread.

    We've started doing this to load the IIS cmdlets regardless of the OS/powershell configuration:

    Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
    Import-Module WebAdministration -ErrorAction SilentlyContinue
    

    Paul

  10. 10 Posted by Mark Dasco on 17 Jul, 2014 05:11 AM

    Mark Dasco's Avatar

    Hi Paul,

    This is the only script in our whole stack which is called from PostDeploy.ps1 – in other post and predeploy scripts we invoke import-module WebAdminstration directly.

    On PostDeploy.ps1 we have:

    if(Test-Path .\Wng-UpdateCms.ps1)
    {
       .'.\Wng-UpdateCms.ps1'
    }
    

    And .\Wng-UpdateCms.ps1 contains:

    Write-Host "Updating CMS Website $cmsWebsiteIisName"
    
    $webAdminModuleTest = get-module -ListAvailable -Name WebAdministration
    
    if ($webAdminModuleTest)
    {
        # Web Administration module available.
       # SCRIPT FAILS HERE!!!!!!
        import-module WebAdministration
    }
    else
    {
        $webAdminSnapInTest = get-pssnapin -Registered | select-string -pattern 'WebAdministration'
        if ($webAdminSnapInTest)
        {
            # Web Administration snap-in available.
            add-pssnapin -name WebAdministration
        }
        else
        {
            Write-Error "Neither Web Administration module nor IIS Powershell snap-in are available." 
            Write-Verbose "In a powershell window type in: get-module -ListAvailable. "
            Write-Verbose "If you don't see Web Administration module listed, install the IIS Powershell snap-in."
            Write-Verbose "http://www.iis.net/download/powershell"
            return
        }
    }
    
    # Hack because import-module WebAdministration intermittently fails
    # starting from octopus 2.4.10.235
    
    $cmsWebsitePhysicalPath = $OctopusActionPackageCustomInstallationDirectory
    
    # Original code below
    #$cmsWebsite = Get-Website -Name "*$cmsWebsiteIisName*"
    #$cmsWebsitePhysicalPath = $cmsWebsite.physicalPath
    

    We are using Windows 2008 R2 64 Bit Powershell 3.0

    Another thing is that we run this same script from other octopus projects and it runs fine sometimes and gets past. But once in a while it doesn’t get past the above line.

    Thanks,
    Mark

  11. 11 Posted by Mark Dasco on 17 Jul, 2014 05:14 AM

    Mark Dasco's Avatar

    Hi Paul,

    This is the only script in our whole stack which is called from PostDeploy.ps1 – in other post and predeploy scripts we invoke import-module WebAdminstration directly.

    On PostDeploy.ps1 we have:

    if(Test-Path .\Wng-UpdateCms.ps1)
    {
      . '.\Wng-UpdateCms.ps1'
    }

    And .\Wng-UpdateCms.ps1 contains:

    Write-Host "Updating CMS Website $cmsWebsiteIisName"

        $webAdminModuleTest = get-module -ListAvailable -Name WebAdministration

        if ($webAdminModuleTest)
        {
            # Web Administration module available.
           # SCRIPT FAILS HERE!!!!!!
            import-module WebAdministration
        }
        else
        {
            $webAdminSnapInTest = get-pssnapin -Registered | select-string -pattern 'WebAdministration'
            if ($webAdminSnapInTest)
            {
                # Web Administration snap-in available.
                add-pssnapin -name WebAdministration
            }
            else
            {
                Write-Error "Neither Web Administration module nor IIS Powershell snap-in are available."
                Write-Verbose "In a powershell window type in: get-module -ListAvailable. "
                Write-Verbose "If you don't see Web Administration module listed, install the IIS Powershell snap-in."
                Write-Verbose "http://www.iis.net/download/powershell"
                return
            }
        }

        # Hack because import-module WebAdministration intermittently fails
        # starting from octopus 2.4.10.235

        $cmsWebsitePhysicalPath = $OctopusActionPackageCustomInstallationDirectory

        # Original code below
        #$cmsWebsite = Get-Website -Name "*$cmsWebsiteIisName*"
        #$cmsWebsitePhysicalPath = $cmsWebsite.physicalPath

    We are using Windows 2008 R2 64 Bit Powershell 3.0

    Another thing is that we run this same script from other octopus projects and it runs fine sometimes and gets past. But once in a while it doesn’t get past the above line.

    Thanks,
    Mark

  12. Support Staff 12 Posted by Paul Stovell on 18 Jul, 2014 01:55 AM

    Paul Stovell's Avatar

    Hi Mark,

    Just to confirm, what is the error message you receive when it hits that line?

    Paul

  13. 13 Posted by Mark Dasco on 18 Jul, 2014 01:58 AM

    Mark Dasco's Avatar

    Sorry Paul,

    The script actually fails on the Get-Website call.

    ERROR: Get-WebSite : Could not load file or assembly 'Microsoft.IIS.PowerShell.Framework' or one of its dependencies. The system cannot find the file specified.

    Mark

  14. Support Staff 14 Posted by Paul Stovell on 21 Jul, 2014 06:27 AM

    Paul Stovell's Avatar

    Hi Mark,

    Thanks for the reply. Looking around the web some people seem to have had success either stopping or removing the default web site in IIS, or putting a Write-Host call prior to Import-Module, but none of these seem to solve the problem 100%. You could also try a try/catch and retry the command since it only seems to be an intermittent problem. Sorry I can't be of more help.

    Paul

  15. 15 Posted by Harald Nielsen on 12 Aug, 2014 11:35 AM

    Harald Nielsen's Avatar

    Hi Paul,

    With the new update in octodeploy, I can deploy vdir websites, but Im getting this error on the server. Ive tried to update powershell to 4.0 and the problem are still there.

    Anyone has a solution for this, since its in a flow I havet control over how can I Write-Host or anything like that before the Get-Website call?

    Harald

  16. 16 Posted by Vanessa Love on 14 Aug, 2014 03:17 AM

    Vanessa Love's Avatar

    Hi Harald,

    Thanks for getting in touch! Are you able to please provide a screenshot of how your step is configured, this will help us get to the bottom of this.

    Thanks!
    Vanessa

  17. 17 Posted by hkn on 14 Aug, 2014 06:51 AM

    hkn's Avatar

    Hi Vanessa,

    I “fixed” the problem with some script, not beautiful:

    Write-Host "Getting web site $parentSite"
    try{
        $site = Get-WebSite -Name "Default Web Site"
    } catch [System.IO.FileNotFoundException]{
        Write-Host "Try again to get website"
        $site = Get-WebSite -Name "Default Web Site"
        Break
    } finally {
        Write-Host $site.name # or do whatever meaningful thing we want to do...
    }

    Its weird because the same flow on our “test” Environment worked when deploying, but then when I deployed to our “staging” environment the error happend. The windows version is the same, the powershell version is the same. Now ive updated the powershell version on staging to 4, but the problem still excised until I changed the script.

    There must be some kind of diff between our test and staging, since the error happened only on staging, ill happily fire off some scripts you send to gather some server information if you need it.

    //Harald

    Fra: Vanessa Love [mailto:[email blocked]]
    Sendt: 14. august 2014 05:17
    Til: Harald Kjær Nielsen
    Emne: Re: Error using Get-WebSite in PreDeploy because of FileNotFoundException [Problems #5172]

  18. 18 Posted by Claus Topholt (... on 14 Aug, 2014 11:37 AM

    Claus Topholt (XCTO)'s Avatar

    Hello,

    Sorry, what is this about? I’m just back from vacation now and have received a number of emails from your company?

    Med venlig hilsen | Best regards

    Claus Topholt |

  19. 19 Posted by Vanessa Love on 14 Aug, 2014 10:00 PM

    Vanessa Love's Avatar

    Hi Claus,

    4 months ago you responded to this thread on our support forums, recently someone has added to the thread. When you respond to a thread you become a watcher and receive emails for any correspondence on the thread.

    Vanessa

  20. 20 Posted by Claus Topholt (... on 15 Aug, 2014 06:48 AM

    Claus Topholt (XCTO)'s Avatar

    Ah! Sorry :-)

    Med venlig hilsen | Best regards

    Claus Topholt

  21. 21 Posted by Tiger Wang on 23 Mar, 2015 05:44 PM

    Tiger Wang's Avatar

    This line fixed our issue (PowerShell 4.0 + Octopus 2.5)

    try {Get-WebSite | Out-Null} catch {}

  22. 22 Posted by James Baker on 30 Apr, 2015 05:34 AM

    James Baker's Avatar

    We solved this is a different way; our Tentacles run as a deployment user so we added

    Import-module WebAdministration to the Powershell profile for that user

    A bit hacky but works very well for us

  23. 23 Posted by voropaev.viktor on 17 May, 2015 05:56 PM

    voropaev.viktor's Avatar

    As I understand, the problem is still not fixed? I'm getting the same error with IIS Virtual Directory — Create script.

     Failed: IIS Virtual Directory - Create
    
    20:27:35   Info     |         Running "IIS Virtual Directory - Create" on "DON"
    
                        |       
    
                        |         Failed: Tentacle script execution
    
    20:27:35   Verbose  |           Running PowerShell script: C:\Windows\system32\config\systemprofile\AppData\Local\Tentacle\Temp\e75262ec-69be-4fdc-bcb4-26fdd921c881.ps1
    
    20:27:37   Info     |           Virtual path: sitemaps
    
                        |           Physical path: C:\sitemaps
    
                        |           Parent site: Zakupki.Front
    
                        |           Getting web site Zakupki.Front
    
    20:27:38   Error    |           Get-Website : Could not load file or assembly 'Microsoft.IIS.PowerShell.Framewo
    
    20:27:38   Error    |           rk' or one of its dependencies. The system cannot find the file specified.
    
    20:27:38   Error    |           At C:\Windows\system32\config\systemprofile\AppData\Local\Tentacle\Temp\e75262e
    
    20:27:38   Error    |           c-69be-4fdc-bcb4-26fdd921c881.ps1:75 char:9
    
    20:27:38   Error    |           + $site = Get-Website -name $parentSite
    
    20:27:38   Error    |           +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    20:27:38   Error    |           + CategoryInfo          : NotSpecified: (:) [Get-Website], FileNotFoundExc
    
    20:27:38   Error    |           eption
    
    20:27:38   Error    |           + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.IIs.Po
    
    20:27:38   Error    |           werShell.Provider.GetWebsiteCommand
    
    20:27:38   Info     |           ==============================================
    
                        |           PowerShell exit code: 1
    
                        |           ==============================================
    
    20:27:38   Fatal    |           PowerShell script returned a non-zero exit code: 1
    
                        |           Tentacle version 2.6.5.1010
    
  24. Paul Stovell closed this discussion on 04 Jan, 2017 07:04 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac