VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

log_script_structure_powercli

Hi Luc ,

could you check the line in orange color and suggest how different strings(regular expressions as you mentioned in last post) are provided using read-host .

?or it has to be hard coded like the way in put in script.

$entity = Read-Host "Which entity?"

$target = New-Item -ItemType Directory -Force -Path "c:\users\jvm\desktop\T_script\logs"

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 $target

    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

    get-log -VMHost $esxi_name -key $log_type | Select -ExpandProperty Entries|out-file -FilePath "$target\$entity.txt"

    $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_name -Bundle -DestinationPath "c:\users\jvm\desktop\T_script\logs"

    }

    if($res1 -eq 'n')

    {

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

    }

    $res2=read-host "do yu want to check specific strings in" $log_type" y/n?"

    if($res2 -eq 'y')

    {

     $match = ".*(?=.*2018-10-15)(?=.*device)(?=.*failed).*"

     get-log -VMHost $esxi_name  -key $log_type | Select -ExpandProperty Entries | where{$_ -match $match}|out-file $esxi_name-vmkernellog_match.txt

    }

    }

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with the attached version.


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

View solution in original post

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

It depends a bit on what you filter on.

The RegEx in your code looks for 3 substrings in each line that must be there.

$regEx1 = ".*(?=.*string1)(?=.*string2)(?=.*string3).*"

You can also do a RegEx where at least 1 of 3 substrings must appear.

$regEx2 = "string1|string2|string3"

You can script around this and generate the RegEx from the input the user gives to the script.

For example:

$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 = ($answer | %{"(?=.*$_)"}) -join ''

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

}   

else{

    $regexStr = $answer -join '|'

}

$regexStr


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Hi Luc,

Iam not able to get the desired result .

first of all this is one entry in vmkernel.log  and for testing  i want to  filter vmkernel on  three orange strings.

2018-10-17T02:17:11.891Z cpu13:66623)Migrate: 375: Remote machine is ESX 6.5 or newer. Encrypted vMotion.

below is how iam using it

Which entity?: esxi1

It's an ESXi

specify domain: .some.domain.com

specify log type: vmkernel

esxi1.some.domain.com

do yu need log bundle"y/n: n

.......

Should all substrings appear (y/n)?: y

Enter a substring (blank to end): 2018-10-17

Enter a substring (blank to end): remote

Enter a substring (blank to end): vmotion

Enter a substring (blank to end):

.*(?=.*).*

iam filtering vmkernel using $strings as all srtings are stored there.is that correct ??

attached is the script .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with the attached version.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks .iam checking this.

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

it works .just little modification of path .however if you could explain about try and catch.

also are we considering 2018-10-17 as string as we are giving this as one of the matching string but without ' '.

may be regular expression has taken it as string.

as i just checked what is stored and found below .

C:\users> $regexStr

.*(?=.*2018-10-17)(?=.*remote)(?=.*vmotion)(?=.*encrypted).*

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Try-Catch construct is a method to capture exceptions.

In this case we try to get the folder (Get-Item), and if the folder doesn't exist, the cmdlet will throw an error.

The exception is captured in the Catch block.

And in this case we use that block to create the folder.

The complete RegEx expression is a string, not need to place quotes around the date.

And yes, we are interpreting the date as a string.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks for the explanation.

i thought of checking this with following where sublogs directory is not present. 

$targetpath = "c:\users\logs\sublogs"

Try{

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

}

Catch{

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

}

as per above code it should throw error but create "sublogs" through catch block as exception is captured .however it did not throw error as expected .

then i checked variable drive

and found

pastedImage_0.png

do i need to change this .??

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That should work, and no, you don't need to change the preference variable for that, since the ErrorAction on the Get-Item overrides that.

You can do a test with something like this

$targetpath = "c:\users\logs\sublogs"

Remove-Item -Path $targetpath -ErrorAction SilentlyContinue

Try{

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

    Write-Host "try"

}

Catch{

    Write-Host "catch"

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

}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

its writing catch only .its the same result .

it would have written try if errors were thrown when it did not find the target path.but it didnot work.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's what I would expect to happen.

In the Try block you try to "get" a non-existing folder.

This causes an exception, and thus the code jumps into the Catch block.
Where you create the missing folder.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

oh i thought when it dot find the folder it will throw error or write try in try block and then proceed to catch to create new folder .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, when an instruction in the Try block causes an exception, the code execution will immediately jump to the Catch block.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks.

Reply
0 Kudos