Automation

 View Only
  • 1.  Disable (Hyper-V) PowerShell Modules

    Posted Oct 25, 2013 01:13 AM

    I have a utility server that I use to administer our vSphere environment. I use PowerCLI pretty heavily. We also have a Hyper-V environment and are starting to increase our scripting and automating of that as well.

    The problem is that I'm getting cmdlet conflicts.

    > gcm -type cmdlet,function,alias | group name | where { $_.count -gt 1 }

    (shows about 20 conflicting commands between Hyper-V and PowerCLI (New-VM, Start-VM, Stop-VM, Get-VM, Export-VM, Get-VMHost, etc).

    I don't necessarily want to completely eliminate the Hyper-V module from the server as we are starting to use it and that looks to increase. If there was a way to disable it whenever I am starting PowerCLI or running a PowerCLI script, that would probably be the best option.

    I have tried things like:

    > (Get-Module -Name Hyper-V) | Remove-Module -Force

    But while I get no error, it doesn't seem to remove the cmdlets. I get the same results with the gcm command and I continue to get errors due to conflicts.

    Any help would be appreciated.

    Thank you



  • 2.  RE: Disable (Hyper-V) PowerShell Modules

    Posted Oct 25, 2013 05:16 AM

    In PowerShell v3 and higher modules that are located in any of the directories defined in the $PSModulePath variable are autoloaded.

    See paragraph 2 in this document.

    You can disable this behavior by using the following setting

    $PSMmoduleAutoloadingPreference="none" 

    You could do something like this in your scripts

    $defaultAutoLoad = $PSMmoduleAutoloadingPreference
    $PSMmoduleAutoloadingPreference = "none"
    Remove-Module -Name Hyper-V

    # Do your PowerCLI stuff


    # End of the PowerCLI stuff

    $PSMmoduleAutoloadingPreference = $defaultAutoLoad

    The drawback of this method is that you will have to load other modules, you would require, explicitly in the script with the Import-Module cmdlet.