LucaS
LucaS

Reputation: 287

Powershell Invoke-Command fails, Locally it works

I want to get Informations about IIS Application Pools from another Server.

This should be possible with Invoke-Command. But theres a strange behavior..

Part of the information i want to get with this command:

$s = "chvmes01"
$command = {Get-ItemProperty IIS:\AppPools\DefaultAppPool | Select *}
$General = Invoke-Command -ComputerName $s -ScriptBlock $command

But this gives me the following error:

Cannot find drive. A drive with the name 'IIS' does not exist.

When i execute this command on the local machine its working. Why? EDIT: The solution was to add Import-Module WebAdministration to the command Variable, now its working perfectly..

The second part of the Information i want to get with the following commands:

$b = Invoke-Command -ComputerName $s -ScriptBlock {(Get-WebConfiguration "$appPoolPath/cpu/@limit").Value}
$a = Invoke-Command -ComputerName $s -ScriptBlock {Get-WebConfiguration "$appPoolPath/cpu/@action"}

Here i dont get any information nore a failure! Its the same with this command: when i execute it on my local machine it works.

Do i misunterstand Invoke-Command?

Best regards

Upvotes: 0

Views: 702

Answers (1)

Kirill Pashkov
Kirill Pashkov

Reputation: 3236

For the first part of your problem. Make sure you have WebAdministration module installed on remote computer. IIS:\ drive is provided by the WebAdministration module, so you need to install/import that module first.

For the second part of your problem. You need to pass arguments for ScriptBlock otherwise remote computer would not know variable values.

$b = Invoke-Command -ComputerName $s -ScriptBlock {param($appPoolPath)(Get-WebConfiguration "$appPoolPath/cpu/@limit").Value} -ArgumentList $appPoolPath
$a = Invoke-Command -ComputerName $s -ScriptBlock {param($appPoolPath) Get-WebConfiguration "$appPoolPath/cpu/@action"} -ArgumentList $appPoolPath

Upvotes: 2

Related Questions