Search code examples
powershelliispowershell-remoting

Powershell remote returning false positive on IIS:\AppPools lookup


Whatever value entered for $appPoolName always returns true for Test-Path when sent to the remote machine, even when no such pool exists. When run locally in powershell on those machines, the proper result is returned. PSRemote is verified to be enabled on the target machines.

$appPoolName = 'Abc123'
$scriptBlock = {
    Import-Module WebAdministration
    if (Test-Path IIS:\AppPools\$appPoolName) {
        Write-Host "Already installed."
    } else {
        Write-Host "Installing..."
        $appPool = New-Item –Path IIS:\AppPools\$using:appPoolName
        $appPool | Set-ItemProperty -Name managedRuntimeVersion -Value 'v4.0'
    }
}
Invoke-Command -ComputerName LT-CODE8 -ScriptBlock $scriptBlock

Why is this reporting true, or what steps can I take to further diagnose?


Solution

  • I think you missed the $using: on the first call of $appPoolname:

    if (Test-Path IIS:\AppPools\$using:appPoolName) {