VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

function_structure

Hi Luc,

could you check following  when i am running as ps1 i get all three parameters in commnd

like below

pastedImage_0.png

however when i convert it to module and then run i dont get all parmeters option only entity is avaliable from command .

function Get-logs

{

    [cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

        [string]$vcenter,

        [string]$entity,

        [string]$targetPath

       

       

    )

   

$cred = Get-Credential

connect-viserver -Server $vcenter  -Credential $cred | Out-Null

#$targetPath = read-host "please provide path"

# "c:\users\in0079d6\desktop\Technicolor_script\logs"

Try{

    $target = Get-Item -Path $targetPath -ErrorAction stop

}

Catch{

    $target = New-Item -ItemType Directory -Force -Path $targetPath

}

if((Get-VM -ErrorAction SilentlyContinue).Name -contains $entity){

    Write-Host "It's a VM" -ForegroundColor Yellow

    $vm = Get-VM -Name $entity

    $ds = Get-Datastore -RelatedObject $vm

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' | Out-Null

    #Get-ChildItem -Path DS:\

    Set-Location DS:\

    cd $vm

    #Get-ChildItem vmware.log

    Copy-DatastoreItem -Item vmware.log -Destination $targetpath

    Remove-PSDrive -Name DS -Confirm:$false

}

elseif(((Get-VMHost).Name | %{$_.Split('.')[0]}) -contains $entity){

    Write-Host "It's an ESXi" -ForegroundColor Green

    #specify domain starting with "."

    $domain=Read-Host "specify domain"

    $log_type=Read-Host "specify log type"

    $esxi_name = "$($entity).$($domain)"

    $esxi_name

    $esxi=get-vmhost $esxi_name

    $res1=Read-Host "do yu need log bundle""y/n"

    if($res1 -eq 'y')

    {

        Write-Host "generating log bundle" -ForegroundColor DarkCyan

        Get-Log -VMHost $esxi -Bundle -DestinationPath $target.FullName

    }

    elseif($res1 -eq 'n')

    {

        Write-Host "......."

        $allCondition = Read-Host -Prompt "Should all substrings appear (y/n)?"

        $strings = @()

        do {

            $answer = Read-Host -Prompt "Enter a substring (blank to end)"

            if($answer){

                $strings += $answer

            }

        } until ($answer -eq '')

        if($allCondition -eq 'y'){

            $regexStr = ($strings | %{"(?=.*$_)"}) -join ''

            $regexStr = ".*$($RegexStr).*"

        }  

        else{

            $regexStr = $strings -join '|'

        }

        Get-Log -VMHost $esxi  -key $log_type | Select -ExpandProperty Entries |

            where{$_ -match $regexStr} |

            #Out-File -FilePath "$($target.FullName)\$($esxi_name)-vmkernellog_match.txt"

            out-file -FilePath $targetPath\$esxi_name-vmkernellog_match.txt

    }

}

$res2=read-host "do yu need full log file without mataching expresions"

if($res2 -eq 'y')

{

write-host "generating" $log_type "log file"

get-log -VMHost $esxi -Key $log_type | select -ExpandProperty entries|out-file $targetPath\$esxi_name_$log_type.txt

}

Disconnect-VIServer -Server "*" -Force -Confirm:$false

}

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Are you sure the function in the module (I assume you named the module MyModule) is defined with 3 parameters?

Do a

Get-Command -Name Get-Logs -Module MyModule -Syntax

When you change the code in a module, you need to add the Force switch on the Import-Module cmdlet to force PS to pick up the latest version.
Import-Module -Name MyModule -Force


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

View solution in original post

0 Kudos
4 Replies
jpsider
Expert
Expert
Jump to solution

When I copy your function and import it into a console, I am able to tab complete the parameters:

PS H:\> function Get-logs{

>>     [cmdletbinding()]

>>     param (

>>         [parameter(mandatory = $true,

>>             valuefrompipeline = $true,

>>             valuefrompipelinebypropertyname = $true)]

>>         [string]$vcenter,

>>         [string]$entity,

>>         [string]$targetPath

>>     )

>>

>>     write-Output $vcenter

>>     Write-Output $entity

>>     write-output $targetPath

>> }

>>

PS H:\> Get-logs -vcenter -entity -targetPath

What do you mean when you say you convert it to a Module?

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

I saved it as .psm1 to convert to module .saved it under default modules path for powershell and then imported this module.

when i try to run this its all parameters are not displayed.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure the function in the module (I assume you named the module MyModule) is defined with 3 parameters?

Do a

Get-Command -Name Get-Logs -Module MyModule -Syntax

When you change the code in a module, you need to add the Force switch on the Import-Module cmdlet to force PS to pick up the latest version.
Import-Module -Name MyModule -Force


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

pastedImage_0.png

it is available now for some reasons(not sure) it was not displayed earlier.

however i am going to add more functions in utilities module so -force switch will help to pick the latest version .this is helpful as it will avoid creating separate directory under modules

each time code changes .

0 Kudos