Invoke-Command script needs imported module on remote end

rated by 0 users
This post has 4 Replies | 2 Followers

Top 75 Contributor
Posts 18
Delton Posted: 12-02-2011 5:36 PM

I'm needing the cmdlet New-Website on the remote computers for the script I'm sending them via Invoke-Command.  The thing is, the WebAdministration module containing New-Website exists on all the remote machines I'm targeting, yet the script reports it does not recognize New-Website, I'm guessing because the remote machines need to first import the module.

I tried adding Import-Module WebAdministration at the beginning of my script block, and this seemed to work, but I got an error.  What is the best way to get the remote computers to access their installed module packs when sending them commands via Invoke-Command?

Here is how I was trying to make a website on a remote computer:

Invoke-Command -ComputerName SERVER -command {Import-module webadministration; New-Website -Name "testing site" -Port 80 -HostHeader "testing site" -PhysicalPath c:\TS}

When I do this, it does create the website, but the website is not started, and it gives me the following error:

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))
    + CategoryInfo          : NotSpecified: (:) [New-Website], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.NewWebsite
   Command

Thank you for your help!

-Delton

  • Filed under:
Top 10 Contributor
Posts 597
Microsoft MVP
Top Contributor

when you run code remotely, it cannot use your credentials implicitly, so you may run into access denied scenarios.

There are a couple of workarounds. If the command you are calling allows submission of credentials, explicitly submit the credentials to use. You can also set up the target machine to use CredSSP mode. In this mode, credentials can be passed to remote targets. Finally, you could also enable "Trusted by delegation" in AD for the target machine and the account. This however is considered a security vulnerability in most scenarios.

I am not sure whether you ran into an access denied-issue. The error message isn't very explicit ;-)

Top 75 Contributor
Posts 18

Thanks Tobias.  I enabled CredSSP on all the targer servers, but I'm still getting the same error.  Am I using the correct method of importing the modules from the WebAdministration pack by using the import-module command in my code block for Invoke-Command, or is there a better method?  I guess for now this gets me a little closer to my goal, because it does create the website on the remote targets, it's just that it creates the website in a stopped state, and gives me that Invalid Class String error exception. 

Also, is there a better method to create a website in IIS on a remote computer than how I'm doing it with my script block in Invoke-Command?

Thanks!

Top 10 Contributor
Posts 597
Microsoft MVP
Top Contributor

Importing the module is alright or else you wouldn't be able to call the cmdlets from it.

Regarding the website creation, this beats me. You may want to post your question in a more specific forum. I haven't used PS in this scenario yet. ;-)

Not Ranked
Posts 1

Hi Delton,

Were you able to resolve the issue? I have run into the same problem.. Please let me know how you resolved it.

 

Thanks

 

Page 1 of 1 (5 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.