Powershell import-module webadministration

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

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)