disassemble-number-5
disassemble-number-5

Reputation: 995

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?

Upvotes: 1

Views: 688

Answers (3)

Petter Ivarsson
Petter Ivarsson

Reputation: 703

I had this problem when I was running Powershell in VS Code. In ISE it always worked as expected.

Upvotes: 0

BenH
BenH

Reputation: 10034

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

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

Upvotes: 1

Related Questions