Search code examples
powershelliiswinrm

Remote Powershell return value


I want to run a powershell with WinRM on serveral servers. I am using Invoke-Command with a script block.

I am reading from IIS and want to return a AppPool-Object, but I cannot access its properties - always empty.

#--imagine this code in a foreach block
$result = Invoke-Command -ComputerName $line.Servername -ScriptBlock {

        Import-Module WebAdministration
        $remotePoolName = Get-Item "IIS:\Sites\LeSite" #| Select-Object applictionPool 

        $pool = dir IIS:\AppPools | Where-Object { $_.Name -eq $remotePoolName.applicationPool }
           return $pool

}
write-host $result.managedRuntimeVersion <- empty

Do I have to access it on the remote machine and return it as string ?


Solution

  • The problem here is, that you are referring to a property including get and set functions.

    Using these functions outside of your server area results in nothing since the object is no longer in your server environment.

    Using these functions inside your script block will work, because you use them on your server directly.

    Greetz