VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

script-structure_powercli

Hello Luc,

By mistake i deleted previous post for this query.

however below is your response.but i dont see any changes to what i posted .

also we are using RemoveAllSnapshots($true) so that means it shoud remove all snapshots .

my specific question is once snapshotdeletion started will script not pick the second task of traffic remediation.

$conn = $global:defaultviserver

if ($conn -cne $null) {

    write-output "there is already session to" $conn.name

    disconnect-viserver "*" -force -confirm:$true

}

$vc = read-host "provide the vcenter name"

connect-viserver -server $vc -username "administrator@vsphere.local" -password ""

$cluster = read-host "provide the cluster name"

foreach ($vm_snapshot in (Get-View -ViewType VirtualMachine -Property Name, snapshot -SearchRoot $cluster.MoRef|? {$_.snapshot -cne $null})) {

    $vm_snapshot.name

    if ($vm_snapshot -eq $null) {

        write-output "nothing to remediate for snapshot"

    }

    else {

        $rem = read-host "do yu want to remediate snapshot"

    }

    if ($rem -eq "yes") {

        write-output "deleting snapshot please wait"

        $vm_snapshot.RemoveAllSnapshots($true)

    }

}

#traffic separation

$vmhosts = get-vmhost -location $cluster

foreach ($hos in $vmhosts) {

    $vmkernel = Get-VMHostNetworkAdapter -VMKernel -VMHost $hos|Where-Object {$_.managementtrafficenabled -eq $true -and $_.vmotionenabled -eq $true}

    $vmkernel|select @{N = 'esxi name'; E = {$hos.name}}, @{N = 'vmkernel port'; E = {$_.name}}

    if ($vmkernel -eq $null) {

        Write-Output "nothing to remediate for traffic separation"

    }

    else {

        $rem1 = read-host " do yu want to remediate traffic separation"

    }

    if ($rem1 -eq "yes") {

        write-output "remediating traffic separation"

        $vmkernel|Set-VMHostNetworkAdapter -VMotionEnabled $false -confirm:$false

    }

}

#sshdisable

foreach ($esxi in $vmhosts) {

    $sshpolicy = Get-VMHostService -VMHost $esxi | Where {$_.Key -eq "TSM-SSH"}

    if ($sshpolicy.running -eq $false) {

        write-output "nothing to remediate for ssh service in $esxi"

    }

    else {

        $rem2 = read-host " do yu want to remediate ssh"

    }

    if ($rem2 -eq "yes") {

        write-output "remediating ssh"

        Stop-VMHostService $sshpolicy -Confirm:$true

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your original script didn't have the loop over all the VMs with a snapshot.

The RemoveAllSnapshots removes all snapshots of a specific VM.

And yes, the script should continue after the snapshot part.


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

View solution in original post

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

Your original script didn't have the loop over all the VMs with a snapshot.

The RemoveAllSnapshots removes all snapshots of a specific VM.

And yes, the script should continue after the snapshot part.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks luc.

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Hello Luc,

i have one additional question related to following which i discussed few days back regarding getting machines with snapshot and deleting them as per following code.

i am not able to write-output when there are no snapshopts.or when  $vm_snapshot.name   valus is $null.

foreach ($vm_snapshot in (Get-View -ViewType VirtualMachine -Property Name, snapshot -SearchRoot $cluster.MoRef|? {$_.snapshot -cne $null}))

{

$vm_snapshot.name

if($vm_snapshot.name -eq $null)

{

write-output "nothing to remediate for snapshot "

}

else{

$rem=read-host "do yu want to remediate snapshot"

}

if ($rem -eq "yes")

{

write-output "remediating snapshot"

$vm_snapshot.RemoveAllSnapshots($true)

}}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$vms = Get-View -ViewType VirtualMachine -Property Name, snapshot -SearchRoot $cluster.MoRef

if($vms -ne $null){

    foreach ($vm_snapshot in $vms)

    {

        if($vm_snapshot.snapshot -cne $null){

            $vm_snapshot.name

            if($vm_snapshot.name -eq $null)

            {

                write-output "nothing to remediate for snapshot "

            }

            else{

                $rem=read-host "do yu want to remediate snapshot"

            }

            if ($rem -eq "yes")

            {

                write-output "remediating snapshot"

                $vm_snapshot.RemoveAllSnapshots($true)

            }

        }

        else{

            Write-Output "No snapshot on $($vm_snapshot.name)"

        }

    }

}

else{

    Write-Output "No VMs found"

}


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thnaks Luc.

is there any way i can use -foregroundcolr and -backgroundcolor with write-output  .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, I'm afraid not, only with Write-Host


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

as per powershell comunity ITS CRIME TO USE write-host.Smiley Happy

but somehow i think for beginners in  powercli  interactive and colorful scripts motivate to learn more and do more .

i was thinking if we can include emoticons in powercli(powershell).is it possible. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look at Emojis in PowerShell? Yes!

And on the Write-Host have a read at Write-Host is not as bad in PowerShell 5


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks iam cheking it .so we can safely change write-output to write-host for this kind of scripts.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Only if you want pretty colours on the screen :smileygrin:


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

Reply
0 Kudos