6

Every time I execute this command

invoke-command -computername REMOTEPC -scriptblock { import-module WebAdministration; new-item "$env:systemdrive\inetpub\testsite" -type directory; New-WebSite -Name TestSite -Port 81 -PhysicalPath "$env:systemdrive\inetpub\testsite" }

I get the following error

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetChildItemCommand

The website is created successfully as far as I can see.

The following command gives the same error when enumerating the testsite

Invoke-Command -computername REMOTEPC { import-module webadministration; dir -path IIS:\Sites\ }

Name             ID   State     Physical Path     Bindings      PSComputerName
Default Web Site 1    Started                     http *:80:    REMOTEPC

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
    + CategoryInfo          : NotSpecified: (:) [Get-ChildItem], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetChildItemCo
   mmand

Any suggestions would be appreciated

3
  • @David: does the computer you are running the command on have the webadministration module installed and imported?
    – MattB
    Mar 30, 2010 at 14:34
  • @MattB: You do not have to have the module loaded locally. As per the ScriptBlock parameter details it, this will be run on the remote computer.
    – Marco Shaw
    Mar 31, 2010 at 18:50
  • @Marco Shaw: where I was going with that is - I'm wondering if the remote call is passing back a property with a type from the webadministration module that the calling machine can't parse properly, but since he isn't using that property it doesn't affect his output. (I haven't been able to test this theory yet...)
    – MattB
    Mar 31, 2010 at 19:10

2 Answers 2

4

This is a serialization problem. New-WebSite and top-level WebSite information from the IIS:\Sites\{sitename} protocol both return objects of type Microsoft.IIs.PowerShell.Framework.Configuration. That object contains a property ftpServer; and ftpServer isn't serializable.

New-WebApplication and Sub-WebApplication information from the IIS:\Sites\{siteName}\{subappname} protocol do not have an ftpServer property. They won't have the same serialization error.

@cad's solution is great, because it prevents the remote script from returning any objects.

But, if you want to return the website information from the remote machine, you can use:

$scriptBlock = {
    Import-Module WebAdministration

    $site = Get-Item IIS:\Sites\www.sitename.com

    $site.ftpServer = $null   # this won't affect the real server value

    return $site
}

Invoke-Command -ComputerName REMOTEPC -ScriptBlock $scriptBlock
3

I had same problem as you. I was in an iteration trying to create 16 sites. First one was being created and had same exception... exiting from iteration and leaving me with only one site created. I didn't fix it, but as a workaround I added

| out-null

at the end of the line. This suppresed output so exception was ignored and my iteration was happily continuing.

Something like:

invoke-command -computername REMOTEPC -scriptblock { import-module WebAdministration; new-item "$env:systemdrive\inetpub\testsite" -type directory; New-WebSite -Name TestSite -Port 81 -PhysicalPath "$env:systemdrive\inetpub\testsite" | out-null}

(Notice that | out-null at the end of the code snippet)

You must log in to answer this question.