Quantcast
Channel: VMware Communities : All Content - VMware PowerCLI
Viewing all articles
Browse latest Browse all 16717

Checking if VM exist from CSV

$
0
0

Hi,

 

Thank you LucD for the help and guidance with setting up scheduled task time/date base don the local time zone(work nicely see below).  I am now trying to add an exist at the beginning to let us know if the VM exist in VMware and if not just put a message on screen

 

#connect-viserver xxx

$TargetTime = Get-Date '24/07/20 23:30:00'

$snapName = 'Snaphot'

$snapDescription = 'Scheduled snapshot'

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'xxx'

$fileName = '.\serverlist.csv'

 

 

Import-Csv -Path $fileName -UseCulture | %{

    foreach ($vm in $fileName){

    $not = get-vm -name $_.VMName -ErrorAction SilentlyContinue

    If ($not){

              Write "Server $vm exists VMware, will create Scheduled Task"

    }

    Else {

           Write "Server $vm does not exist, try Nutanix or different Vcentre"

    }

}

    $vm = Get-VM -Name $_.VMName

    If ($vm.Name -match "^NYC*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(-$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^PHILI*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(-$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^MINNI*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time") 

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(-$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^UK*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("UTC")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(-$timezone.BaseUtcOffset.TotalHours)

    }

    elseif ($vm.Name -match "^SERB*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Europe Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^DE*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Europe Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^FR*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Europe Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^SW*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Europe Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^IN*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("India Standard Time")

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^HK*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("China Standard Time") 

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    elseif ($vm.Name -match "^AUS*") {

    # Get the target timezone

    $timezone = [System.TimeZoneInfo]::FindSystemTimeZoneById("AUS Eastern Standard Time") 

    # Get the UTC time for the target timezone

    $timezone = $TargetTime.AddHours(+$timezone.BaseUtcOffset.TotalHours)                            

    }

    $si = get-view ServiceInstance

    $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

    $spec = New-Object VMware.Vim.ScheduledTaskSpec

    $spec.Name = "Patching Snapshot",$vm.Name -join ' '

    $spec.Description = "Take a snapshot of $($vm.Name)"

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler

    $spec.Scheduler.runat = $timezone

    $spec.Action = New-Object VMware.Vim.MethodAction

    $spec.Action.Name = "CreateSnapshot_Task"

    @($snapName,$snapDescription,$snapMemory,$snapQuiesce) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

    $scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec)

}

disconnect-viserver -confirm:$false

 

The error i get it that the exist statement is returning the filename, i just missing something and help appreciated

 

Also for server not found i get

 


Viewing all articles
Browse latest Browse all 16717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>