VMware Cloud Community
MakeItWork
Enthusiast
Enthusiast

Cannot run imported powershell module from Invoke a Powershell script

Greatings,

I'm trying to run an imported powershell module (http://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc) using the default Invoke a Powershell Script workflow.

The host has been added, and I can run the default powershell cmdlets (Get-Service), but cannot run one of the imported modules (Get-WUList, from the package above). The host was added using the domain admin account.

The external package was imported from a powershell window opened 'Run as administrator'.  The cmdlet runs fine from the command line.

Here is the error message I get;

PowerShellInvocationError: Errors found while executing script
System.Management.Automation.CommandNotFoundException: The term 'get-WUlist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
   at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandOrigin commandOrigin)
   at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
   at System.Management.Automation.ExecutionContext.CreateCommand(String command)
   at System.Management.Automation.CommandNode.CreateCommandProcessor(Int32& index, ExecutionContext context)
   at System.Management.Automation.CommandNode.AddToPipeline(PipelineProcessor pipeline, ExecutionContext context)
   at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
   at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
(Dynamic Script Module name : invokeScript#14)

I wasn't able to find anything in the documentation on how to do this.

Your feedback and help are greatly appreciated in advance.

Visit us at http://www.cloudnutz.com and twitter @cloudnutz
0 Kudos
3 Replies
igaydajiev
VMware Employee
VMware Employee

Each time  "Invoke a PowerShell script" workflow is invoked it opens  new session to powershell engine (This is the same as opening a new powershell console). Initially in this session powershell engine loads only system cmdlet's. To use functionality available from third party powershell modules/snapins you need to import them .

What this means is that you need to prepend the  ' Import-Module PSWindowsUpdate' command  in you script.

So your script shoould look like :

     Import-Module PSWindowsUpdate

     get-WUlist

MakeItWork
Enthusiast
Enthusiast

Thanks for the feedback, this got me on the right track. It is now working.

The steps taken for the group

1. Install the Powershell plugin (VCO)

2. Create or modify a windows VM for WinRM. I used a CD installed W2K8R2 and did not apply any patches.

3. Add a powershell host (VCO client -> Library -> PowerShell -> Add a PowerShell Host)

4. Run one of the canned PowerShell samples (VCO client -> Library -> PowerShell -> Samples -> List directory content)

5. (On WinRM host). Download the .Zip file. Right click, select properties and 'Unblock"

6. Unzip the file

7. Copy the files to the following

  • %USERPROFILE%\Documents\WindowsPowerShell\Modules
  • %WINDIR%\System32\WindowsPowerShell\v1.0\Module

8. Open a PowerShell prompt on the WinRM host

9. Verify you can import the modules without the execution policy nag

  • Import-module PSWindowUpdate

10. (In VCO client) Run Get-WUList (Gives you a list of available updates)

  • Library -> PowerShell -> Invoke a PowerShell script
  • Select your WinRM host (Step 3)
  • Enter the following in the Script box
    • Import-Module PSWindowsUpdate
    • Get-WUList

11. If everything works right you should get an output like this in the Schema Logs window


[2012-02-24 09:08:03.302] [D] Invoke command in session d5d5d355-69f7-445c-8b5b-32952ba64fe6
[2012-02-24 09:08:16.799] [I] Connecting to Windows Update. Please wait...
Found 104 updates to install.

Status              LastDeploymentChang KBArticleID         Title             
                    eTime                                                     
------              ------------------- -----------         -----             
------              10/13/2009 12:00... KB974431            Update for Windo...
------              10/13/2009 12:00... KB975467            Security Update ...
------              10/13/2009 12:00... KB974571            Security Update ...
------              1/12/2010 12:00:... KB972270            Security Update ...

Visit us at http://www.cloudnutz.com and twitter @cloudnutz
0 Kudos
igaydajiev
VMware Employee
VMware Employee

Glad  I was able to help.

This is very nice example how you can reuse PowerShell functionality to extend the power of vCO. Thanks for sharing it...

0 Kudos