PowerCLIGoodies / DRSRule

PowerShell module with support for managing vSphere DRS Rules and Groups
29 stars 9 forks source link

Unable to find type [DRSRule_VMToVMRule] #21

Open Backstage24 opened 5 years ago

Backstage24 commented 5 years ago

Hi, I have a problem with the DRSRule module v2: Just a simple export script:

$vCenter = "servername"
$Exportpath = "C:\Scripts_chs"
$date = (Get-date -format yyyyMMdd)
Connect-VIServer $vCenter
Export-DrsRule -Path $Exportpath"\DrsRuleInfo_$date.json"

If I run the script .\bla.ps1 rise up the following error:

Unable to find type [DRSRule_VMToVMRule].
At C:\Program Files\WindowsPowerShell\Modules\DRSRule\2.0.0\DRSRule.psm1:271 char:15
+   [OutputType([DRSRule_VMToVMRule],[VMware.Vim.ClusterAffinityRuleSpe ...
+               ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (DRSRule_VMToVMRule:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

If I copy and paste the code in a fresh Powershell session, the export file will generate without a problem.

A little bit strange, isn't it? Do you have any ideas?

My modules:

ModuleType Version    Name
---------- -------    ----
Script     2.0.0      DRSRule
Manifest   3.1.0.0    Microsoft.PowerShell.Management
Manifest   3.1.0.0    Microsoft.PowerShell.Utility
Script     6.7.0.1... VMware.Vim
Script     11.0.0.... VMware.VimAutomation.Cis.Core
Script     11.0.0.... VMware.VimAutomation.Common
Script     11.0.0.... VMware.VimAutomation.Core
Script     11.0.0.... VMware.VimAutomation.Sdk

Thanks

mtboren commented 5 years ago

Hey, @Backstage24 -

Oh, man, that's no good. That is a bit strange, and definitely (obviously) not the desired behavior.

I was able to reproduce that behavior in a new PowerShell session. It seems to be something with the way that the module gets loaded in the scope of the running script.

In my testing, I was able to get the expected behavior (successful export of DRS Rule items into a JSON file) by loading the DRSRule module in a new PowerShell session and then running bla.ps1.

I am not sure how module auto-loading is coming into the picture here, if at all. But, to try to narrow down the cause, how does bla.ps1 behave if you import the VMware.VimAutomation.Core and DRSRule modules in your PowerShell session, and then invoke bla.ps1?

Backstage24 commented 5 years ago

Hi Matt

Thanks for your feedback. You'r right, if I load the modules in a fresh PowerShell session and invoke my script, all is fine. To load the modules in the script is not working also a staple script (1. Script load modules and call the 2nd script) is not working. I'll run the script with our Script Runner. I can configure the Script Runner Action to load some modules in the current session. And "bling" it works...! For me is all good, but there is still a little problem with the module.

Thanks again and regards Chris

mtboren commented 5 years ago

Greetings, Chris-

Ok, thanks for the update. So, a workaround for now, at least.

Yes, definitely still something to work out with the module, it seems.

And thanks for getting this Issue open. I've added a couple of labels -- it'll take some more digging to understand what the dealio.

Cheers

virtualGain commented 5 years ago

Just a heads up I have a little bit cleaner work around. In Powershell 5 in order to import a module with classes you cannot use the "Import-Module" function (which is what autoloading modules does as well). You have to utilize the slightly undocumented "using" command.

https://info.sapien.com/index.php/scripting/scripting-classes/import-powershell-classes-from-modules

The following line at the top of the script calling your drs cmdlets should allow you to run the script from anywhere including a task. Still baffled why i can import it from a command line and run the cmdlets but this is a decent workaround so im moving on haha.

must be top line in script: using module DRSRule

RKausS commented 5 years ago

Hi together

I have exact the same issue while exporting rules as "Backstage24" describes in the opening post of this thread:

Unable to find type [DRSRule_VMToVMRule].
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\DRSRule\DRSRule.psm1:271 char:15
+   [OutputType([DRSRule_VMToVMRule],[VMware.Vim.ClusterAffinityRuleSpe ...
+               ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (DRSRule_VMToVMRule:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Unfortunately, the work around from "virtualGain" seems not to work for me. Loading the module in advance and then starting my script (which uses DRSRule) in the same session works, but don't lets me schedule my script? My modules:

ModuleType Version    Name                                ExportedCommands                                                                                            
---------- -------    ----                                ----------------                                                                                            
Script     2.0.0      DRSRule                             {Export-DrsRule, Get-DrsVMGroup, Get-DrsVMHostGroup, Get-DrsVMToVMHostRule...}                              
Script     1.0.0.0    ISE                                 {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}                                                         
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                                          
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                                   
Script     6.7.0.1... VMware.Vim                                                                                                                                      
Script     11.2.0.... VMware.VimAutomation.Cis.Core       {Connect-CisServer, Disconnect-CisServer, Get-CisService}                                                   
Script     11.2.0.... VMware.VimAutomation.Common                                                                                                                     
Script     11.2.0.... VMware.VimAutomation.Core           {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostNtpServer...}        
Script     11.2.0.... VMware.VimAutomation.Sdk            {Get-ErrorReport, Get-InstallPath, Get-PSVersion}                                                           

Is there any progress or some advise available? Or can I do some more things to support solving the issue?

Regards Roman

lucdekens commented 5 years ago

Looks like a

using module .\DRSRule.init.ps1

in DRSRule.psm1 fixes the issue. I'll do some further testing, and when successful I'll submit a PR.

raoulschaffner commented 4 years ago

this did the trick. i manually edited C:\Program Files\WindowsPowerShell\Modules\DRSRule\2.0.0\ DRSRule.psm1 and inserted using module .\DRSRule.init.ps1 at the top. my task scheduler script works!