VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

disk_info_function_powercli

hi Luc,

could you suggest about below scenerio.

following is the function to get disk info of virtual machine.it works fine but finally i thought of converting it to module and saved as module1.psm1

i am not sure how to run this .do i need to import this ??

also functions are not persistent but modules are .is this correct?

function disk_info

{

[cmdletbinding()]

param (

[parameter(mandatory = $true,

           valuefrompipeline = $true,

           valuefrompipelinebypropertyname = $true)]

           [string[]]$vmname

)

$vm = get-vm $vmname

$disksinfo=$vm.Guest.Disks

$disksinfo

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Module autoloading was introduced in PowerShell v3, so that is the 1st requirement.

Autoloading is the concept that the first time you use a cmdlet from a module, the PS engine will automatically load the module.

And PS "knows" to which module a cmdlet belongs, because it keeps a cache of the cmdlet-module mapping.

The folder you used is not a standard PowerShell folder, it is the location where older versions of PowerCLI installed.

Those installations were MSI based, and they added that folder to $env:PSModulePath.

If you install your PowerCLI modules from the PSGallery (Install-Module), that folder will not be used.

The common PS module folders are $(env:USERPROFILE)\Documents\WindowsPowerShell\Modules (scope=user) and C:\Program Files\WindowsPowerShell\Modules (scope=allusers).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

That is correct.

A module consists (mostly) of one or more functions that are packaged in a .psm1 file.

Normally you also add a manifest file (.psd1) that contains information about the module.

Those files are stored in a folder, and the foldername is the name with which you address the module (listing, importing...).

These folders can be stored anywhere, but if you store them in one of the folders listed in $env:PSModulePath, you will have the advantage of module autoloading.

You can find more elaborate info about modules in Understanding a Windows PowerShell Module and in the help page in about_modules.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

this is one of the paths given by  $env:PSModulePath.

C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules

so i created one directory same as as module_test and stored my file module_test.psm1 there .

pastedImage_0.png

and exit powershell but it does not load automatically .i need to go to this directory and import this .

iam validating this by going to function: drive

it does not load module automatically and its not persisting also .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Module autoloading was introduced in PowerShell v3, so that is the 1st requirement.

Autoloading is the concept that the first time you use a cmdlet from a module, the PS engine will automatically load the module.

And PS "knows" to which module a cmdlet belongs, because it keeps a cache of the cmdlet-module mapping.

The folder you used is not a standard PowerShell folder, it is the location where older versions of PowerCLI installed.

Those installations were MSI based, and they added that folder to $env:PSModulePath.

If you install your PowerCLI modules from the PSGallery (Install-Module), that folder will not be used.

The common PS module folders are $(env:USERPROFILE)\Documents\WindowsPowerShell\Modules (scope=user) and C:\Program Files\WindowsPowerShell\Modules (scope=allusers).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

powershell version is 5.1

i stored one of e the .psm1 file below

$modules_path="C:\Program Files\WindowsPowerShell\Modules\module_diskinfo"

so if i close the powershell session and open again do i expect it to load as i have to manualyy import again .

are modules for functions only??

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, but it works a bit different.

Modules are not autoloaded at the start of your PowerShell session.

A module is autoloaded when you use the 1st cmdlet from that module.

You can do a test to verify

Write-Host "Before"

Get-Module

Get-PowerCLIConfiguration | Out-Null

Write-Host "After"

Get-Module


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnks iam checking this .

could you suggest why below is not working in orange line.i thought of creating function for partitions and corresponding hard drives.for windows vm.

function get-diskinfo

{

[cmdletbinding()]

param (

[parameter(mandatory = $true,

           valuefrompipeline = $true,

           valuefrompipelinebypropertyname = $true)]

           [string[]]$vmname

)

$vm = get-vm $vmname

#$disksinfo=$vm.Guest.Disks

#$disksinfo

#$disks=Get-HardDisk -VM $vm|select name

$vm|select name,@{N='path';E={($_.guest.disks).path}};@{N='harddisk';E={($_|Get-HardDisk).name}}

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Because you are using a semi-column (which is a line separator) between the two calculated properties.

That should be a comma.

function get-diskinfo

{

    [cmdletbinding()]

    param (

    [parameter(mandatory = $true,

               valuefrompipeline = $true,

               valuefrompipelinebypropertyname = $true)]

               [string[]]$vmname

    )

   

    $vm = Get-VM $vmname

    #$disksinfo=$vm.Guest.Disks

    #$disksinfo

    #$disks=Get-HardDisk -VM $vm|select name

    $vm | select Name,@{N='Path';E={($_.guest.disks).path}},@{N='harddisk';E={($_|Get-HardDisk).name}}

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks .time to clean laptop screen:smileylaugh:

Reply
0 Kudos