Search code examples
powershelliispowershell-remoting

Powershell invoke-command not returning value of get-itemproperty


I'm writing a script to retrieve the pool recycling parameters from IIS Servers.

My problem is that locally the command works fine (I get the correct result in minutes) :

(Get-ItemProperty "IIS:\AppPools\MyPool" -Name "Recycling.Periodicrestart.Time.Value").totalminutes

But when launching remotely with invoke-command, nothing comes back, The $RecycleTimeInterval value is $null:

$PSSession = New-PSSession -ComputerName MyServer
Invoke-Command -Session $PSSession -ScriptBlock {Import-Module WebAdministration}
$PoolArray = Invoke-Command -Session $PSSession -ScriptBlock {Get-ChildItem -Path 'IIS:\AppPools' -Verbose}
ForEach($pool in $PoolArray) {
    $PoolName = $pool.Name
    $RecycleTimeInterval = Invoke-Command -Session $PSSession -ScriptBlock {(Get-ItemProperty IIS:\AppPools\$args[0] -Name 'Recycling.Periodicrestart.Time.Value').totalminutes} -ArgumentList $PoolName
    }
}

Note that the Get-ChildItem command to get the pool list works fine.


Solution

  • I've managed to get the value with :

    $PSSession = New-PSSession -ComputerName MyServer
    Invoke-Command -Session $PSSession -ScriptBlock {Import-Module WebAdministration}
    $PoolArray = Invoke-Command -Session $PSSession -ScriptBlock {Get-ChildItem -Path 'IIS:\AppPools' -Verbose}
    ForEach($pool in $PoolArray) {
        $PoolName = $pool.Name
        $RecycleTimeInterval = Invoke-Command -Session $PSSession -ScriptBlock {
            param($SBPoolName)
            (Get-Item IIS:\AppPools\$SBPoolName).Recycling.Periodicrestart.Time.TotalMinutes}
            -ArgumentList $PoolName
        }
    }