Automation

 View Only
  • 1.  Need help writing script to check vmtools timesync settings

    Posted Jul 18, 2024 05:15 PM
    Edited by fscked Jul 18, 2024 05:22 PM

    Having an issue figuring out a ps script to just read vm names from a csv and grab the timesync settings. Anybody have one laying around I might be able to get?

    Here is the working I have so far...

    # Define the path to your CSV file
    $csvPath = "C:\Path\to\import.csv"

    # Connect to the vCenter server
    $vCenterServer = "server.name.com"
    Connect-VIServer -Server $vCenterServer

    # Import VM names from the CSV file
    $vmList = Import-Csv -Path $csvPath

    # Initialize an array to hold the results
    $results = @()

    foreach ($vm in $vmList) {
        $vmName = $vm.VMName
        
        try {
            # Get the VM object
            $vmObject = Get-VM -Name $vmName

            # Get the VM Tools object
            $vmTools = $vmObject.ExtensionData

            # Check if VMware Tools is installed
            if ($vmTools.Config.Tools -gt 0) {
                # Retrieve the time sync settings from VMware Tools
                $toolsTimeSyncEnabled = $vmTools.Config.Tools.SyncTimeWithHost
                $results += [PSCustomObject]@{
                    VMName = $vmName
                    ToolsVersion = $vmTools.Config.Tools
                    TimeSyncEnabled = $toolsTimeSyncEnabled
                }
            } else {
                $results += [PSCustomObject]@{
                    VMName = $vmName
                    ToolsVersion = "Not Installed"
                    TimeSyncEnabled = "N/A"
                }
            }
        } catch {
            Write-Warning "Failed to process VM: $vmName. Error: $_"
            $results += [PSCustomObject]@{
                VMName = $vmName
                ToolsVersion = "Error"
                TimeSyncEnabled = "Error"
            }
        }
    }

    # Export results to a CSV file
    $outputPath = "C:\path\to\export\vm_tools_time_sync_settings.csv"
    $results | Export-Csv -Path $outputPath -NoTypeInformation

    # Disconnect from the vCenter server
    Disconnect-VIServer -Server $vCenterServer -Confirm:$false

    Write-Host "Time sync settings check complete. Results saved to $outputPath"



  • 2.  RE: Need help writing script to check vmtools timesync settings

    Posted Jul 19, 2024 12:57 AM

    What is wrong or missing from the code you have?



    ------------------------------


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


    ------------------------------



  • 3.  RE: Need help writing script to check vmtools timesync settings

    Posted Jul 19, 2024 08:43 AM
    Edited by LucD Jul 22, 2024 06:34 AM

    Hello,

    That PowerShell script you got there is pretty sweet! It reads VM names from a CSV file, connects to vCenter, checks if VMware Tools are installed, and then grabs the time sync settings for those VMs. Finally, it saves everything to a new CSV file.

    Here's a simplified breakdown:

    1. Grabs VM Names: Reads VM names from a CSV you specify.
    2. Connects to vCenter: Establishes a hpinstantink connection to your vCenter server.
    3. Loops through VMs: One by one, it goes through each VM name in the list.
    4. Checks VM Tools: Sees if VMware Tools are installed on the current VM.
    5. Gets Time Sync Settings (if installed): If VM Tools are there, it retrieves the time sync settings.
    6. Saves Results: Stores all the info (VM name, Tools version, time sync status) in a new CSV file.
    7. Disconnects from vCenter: Closes the connection to the vCenter server.

    Hps.




  • 4.  RE: Need help writing script to check vmtools timesync settings

    Posted Jul 19, 2024 09:28 AM

    Your section of code below is not giving you any info

            # Get the VM Tools object
            $vmTools = $vmObject.ExtensionData

    I tried the code with this and now see info when I get to the rest of your code.

            # Get the VM Tools object
            $vmTools = Get-VM -Name $vm | Get-View 

    Lot of info there so you will have to see how you want to handle it.