VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

Script not performing Task for all objects using the Get-Content cmdlet

Hi,

For some reason when I run the following code the log bundles are only being dumped for the first host in the hosts.txt file. I do have a script that gets the log bundles for all the hosts in a cluster and renames the cryptic file name to reflect the host name thanks to Luc Dekens. I could just use the script to get the bundles for all the hosts in the cluster but there are times when I need just one or two hosts from the cluster. Can someone tell me what is wrong with the code below?

$VC = Read-Host "Enter vCenter Name"
$varHost = Get-Content "S:\Get-Logs\hosts.txt"

##Connect to VCenter
Connect-VIServer $VC -WarningAction SilentlyContinue
#----------------------------------------------------


foreach ($ESX in $varHost) {

    $destPath = "S:\Get-Logs\Logs\$($ESX)"
   
   
   If ((New-Item -Path $destPath -ItemType directory -ErrorAction SilentlyContinue) -eq $null)
{New-Item -Path $destPath -ItemType directory -ErrorAction SilentlyContinue}

    Get-Log -VMHost $ESX -Bundle -DestinationPath $destPath

   
    }

             
##Disconnect-VIServer
Disconnect-VIServer * -Confirm:$false
##End of Script

Although it would be ideal for the log files to be renamed at this point I would be happy if the script would create a folder for each host listed in the text file and place the log file in each folder.

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The variable $ESX contains just a string, not a property Name.

So referencing $ESX.Name will return nothing


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

I just tested this script again, and for me it is working as designed.

I strongly suspect it might be related to the hosts.txt file you are using.

In my test file I have 2 lines, with on each line the FQDN os an ESXi node.

Can you check if the following returns a VMHost object for each line your .txt file ?

$varHost = Get-Content "S:\Get-Logs\hosts.txt"

foreach ($ESX in $varHost) {

    Get-VMHost -Name $ESX

}


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

The text file that I am using also contains the fqdn of 2 hosts with one on each line but I will test the output.

Thanks,

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

OK after running the following code the output is 2 lines with the fqdn for both hosts listed in the text file.

$varHost = Get-Content "S:\Get-Logs\hosts.txt"

foreach ($ESX in $varHost) {

    Get-VMHost $ESX | Select Name

}

What is happening when I run the full script is that a folder is created for each individual host. The log bundle code appears to run for the first host listed in the text file but it seems to finish prematurely and then proceeds to dump the log bundle for the second host listed in the text file with the result being that the log bundle exports successfully for the second host but nothing for the first host.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that the production of the log for the 1st host in the list has an issue.

Try do that separately to check.

Get-VMHost -Name <1sthost> | Get-Log -Bundle -DestinationPath C:\Temp


Does this produce a dump ?


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

I thought that might be the case but I ran the script again against that host only the second time with success. I will run the script against two different hosts when I get a chance.

Thanks

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I ran the script again and this time a folder was created for each host and the logs were dumped for both hosts so I'm leaning towards the issue being with the hosts.txt file?

I've added back the code to rename the bundle file but for some reason it renames the file with just the date and not the host name.

CODE:

--------

$VC = Read-Host "Enter vCenter Name"


$varHost = Get-Content "S:\Get-Logs\hosts.txt"

##Connect to VCenter
Connect-VIServer $VC -WarningAction SilentlyContinue
#----------------------------------------------------

foreach ($ESX in $varHost) {

    $destPath = "S:\Get-Logs\Logs\$($ESX)"
   
   
  
If ((New-Item -Path $destPath -ItemType directory -ErrorAction SilentlyContinue) -eq $null)
{New-Item -Path $destPath -ItemType directory -ErrorAction SilentlyContinue}

  
    Get-Log -VMHost $ESX -Bundle -DestinationPath $destPath
   
    $logName = Get-Log -VMHost $ESX -Bundle -DestinationPath $destPath

    if($logName)
   

    {

        Rename-Item -Path $logName -NewName "$($destPath)\$($ESX.Name)-$((Get-Date).ToString('yyyy-MM-dd-hh-mm')).tgz"


   
    }

}
#-----------------------------------------------------------------------------------

##Disconnect-VIServer
Disconnect-VIServer * -Confirm:$false
##End of Script

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The variable $ESX contains just a string, not a property Name.

So referencing $ESX.Name will return nothing


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

That worked. It now renames the log bundle correctly. Question: is the original cryptic file supposed to remain after the file is renamed? I'm seeing 2 files now. The cryptic named one and the host named one.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, since it is a rename, there should only be one file.

Isn't that 2nd file one that was left behind after previous attempts ?


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

That's what I thought but I deleted the old file to make sure  before I ran the script a second time. Tell me, have you ever encountered weird behavior of code like I'm encountering?...LOL

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just noticed, in that last script you included, you seem to have Get-Log -Bundle twice in there.

That would explain the two files.

This version should work

$VC = Read-Host "Enter vCenter Name"

$varHost = Get-Content "S:\Get-Logs\hosts.txt"

##Connect to VCenter

Connect-VIServer $VC -WarningAction SilentlyContinue

#----------------------------------------------------

foreach ($ESX in $varHost) {

    $destPath = "S:\Get-Logs\Logs\$($ESX)"

    Try{

        Get-Item -Path $destPath -ErrorAction Stop

    }

    Catch{

        New-Item -Path $destPath -ItemType Directory

    }

 

    $logName = Get-Log -VMHost $ESX -Bundle -DestinationPath $destPath

    if($logName)

    {

        Rename-Item -Path $logName -NewName "$($destPath)\$($ESX)-$((Get-Date).ToString('yyyy-MM-dd-hh-mm')).tgz"

    }

}

#-----------------------------------------------------------------------------------

##Disconnect-VIServer

Disconnect-VIServer * -Confirm:$false

##End of Script


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

Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

I haven't had a chance to take a look at the code again...That certainly explains why there are two files...DOH!...LOL

At least the resolution is an easy one. Smiley Happy

Reply
0 Kudos