proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
542 stars 87 forks source link

Issue with -ModulesToImport and the VMWare module. #117

Closed hematic closed 6 years ago

hematic commented 7 years ago

Bug or Possible Lack of Documentation

When passing the 'VMware.VimAutomation.Core' module using the '-ModulesToImport' switch none of the VMWare commands are available in the job.

I get continuous errors like :

The term 'Get-VM' 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.

The term 'Connect-VIServer' 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.

The term 'Set-PowerCLIConfiguration' 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.

Commands from other modules do seem to work fine.

hematic commented 7 years ago

Its possible that this module requires something else to work on the remote system and passing it is not enough. But i have no idea whats up.

proxb commented 7 years ago

Can you provide the code that you are using?

hematic commented 7 years ago

Absolutely!

https://gist.github.com/hematic/c487d59ff85630d415af2f92a556b46c

hematic commented 7 years ago

To expand on this as i have done a bunch more testing, i think something weird is going on and its not related to the VMware module.

The code i gave you in the Gist above pipes $Servernames (an array of server names) to Start-RSJOB. For fun i picked 5 of them that were failing and put them in their own array and piped it to the same code.

The first server worked and the next 4 failed. So i then removed the successful server and did the remaining 4.

The first server worked and the next 3 failed. This happend till i was out of servers.

It seems like -ModulestoImport is only importing the modules on the first server in the array and not the consecutive ones. All of the ones after fail saying that the command Get-VM is not known because that module doesn't go through.

If i instead do the following :

Foreach($Item in $Missingservers){ $Item | Start-RSjob -Name {"$($_)"} -throttle 100 -ScriptBlock $Scriptblock -ModulesToImport Serverdata, VMware.VimAutomation.Core }

Every. Single. Server. Works.

MVKozlov commented 7 years ago

Just tested - Have no problem with PowerCLI 6.5 R1 may be you use early version ? Its not fully moved from Snapins to Modules. may be there is a problem. and about your code: if you use -ModuletoImport do not use Import-Module inside of your scriptblock

this works fine $servers | start-rsjob { param($server) connect-viserver vcenter | out-null; get-vm $server } -modulestoimport VMware.VimAutomation.Core and this approach get bunch of errors C:\Users\MKozlov> $servers | start-rsjob { param($server) import-module VMware.VimAutomation.Core; connect-viserver vcenter | out -null; get-vm $server }

errors like

WriteStream : The specified mount name 'vis' is already in use.
At D:\Users\Max\Documents\WindowsPowerShell\Modules\PoshRSJob\Public\Receive-RSJob.ps1:105 char:18
+             $_ | WriteStream
+                  ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,WriteStream
hematic commented 7 years ago

I am using version 6.3

I went ahead and removed the Import-Module portion from the code and tried to add some better error handling with Try-Catches. But its thanksgiving so i wont be testing till tomorrow. Will reply back with my findings though!

hematic commented 7 years ago

Removing the import module changed nothing on my testing.

If i do the servers one by one. Every single one works as expected.

Pipelining the entire array to Start-RSJob however gets me a high number of failures.

Its unfortunate because i have used this module religiously since i found it but i think this may cause me to have to abandon it if i cant get it sorted out.

@MVKozlov @proxb Im perfectly willing to accept this its something i'm doing wrong on my end or an environmental issue if that's what you guys think. I have just never run into this before with this module and have spent dozens of hours trying to figure out whats going on.

Here is the most current version of code i have for the main script https://gist.github.com/hematic/44fc0e7621b4bf845404c8edaed8adc9

Get-VM is failing on a regular basis on line 89 inside the try catch. This causes the names of the failed servers to get dumped to a log for me to track. It also puts the error into a log file per machine.

Every single machine says

"The term 'Get-VM' 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."

EDIT** tracing this farther back im actually getting an error before the Get-VM command.

The term 'Connect-VIServer' 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.

This doesn't change anything but just wanted to be as exact as possible. This command is called from my personal module which gets passed just fine.

EDIT 2** Inside of each job i am having it pipe out a list of modules using get-module -listavailable. It does include the VM modules. Im not sure if that command is job specific though.

Im not the only one with this problem it seems. https://communities.vmware.com/thread/513253?start=0&tstart=0

MVKozlov commented 7 years ago

It's not RSJob bug, it's PowerCli problem and last post line on your link have solution :) It was really hard to find PowerCli 6.3 in my environment but I find it :) and Yes, it works! just change

$Servernames | Start-RSjob -Name {"$($_)"} -ScriptBlock $Scriptblock -ModulesToImport Serverdata, VMware.VimAutomation.Core

to

$Servernames | Start-RSjob -Name {"$($_)"} -ScriptBlock $Scriptblock -ModulesToImport Serverdata -PSSnapinsToImport VMware.VimAutomation.Core

but I strongly recommend to upgrade PowerCli to 6.5 R1 and run away from snapins :)

codykonior commented 7 years ago

@hematic Did this fix it for you?