简体   繁体   中英

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.

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM