powershelliis-8remote-execution

Set-ItemProperty physicalPath


Remote execution for setting physicalPath is erroring with the following message:

Cannot find drive. A drive with the name 'IIS' does not exist.

What is wrong with the following?

$site    = Read-Host "What is the name of the virtual?"
$newpath = Read-Host "What is the NEW PATH of the new site?"

$ScriptBlockContent = {
    $site = $args[0],
    $newpath = $args[0]
    (Set-ItemProperty -Path IIS:\\Sites\ABC_LIVE\$site -Name "physicalPath" -Value "$newpath")
}

# Add the IIS PowerShell Module
Import-Module WebAdministration 

Invoke-Command -ComputerName DEVSERVERNAME -ScriptBlock $ScriptBlockContent -ArgumentList $site,$newpath

Solution

  • You need to import the module inside the scriptblock (the module must be installed on the remote host). Also, both variables in the scriptblock are assigned the same argument ($args[0]), and the first assignment has a spurious trailing comma.

    Use a Param() block instead of individual variable assignments, and remove the pointless parentheses around Set-ItemProperty.

    $ScriptBlockContent = {
        Param($site, $newpath)
        Import-Module WebAdministration
        Set-ItemProperty -Path IIS:\\Sites\ABC_LIVE\$site -Name "physicalPath" -Value $newpath
    }