powershell, vmware

pramod1
pramod1 used Ask the Experts™
on
####################################################################################
$vcenter1 = 'testw'
$vcenter2 = 'testdr2w'
$names = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist.txt'
#$names2 = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist2.txt'
$snapoutput = '\\management server name\C$\Admin\Scripts\snap-results.txt'
$snapreport  = '\\management server name\C$\Admin\Scripts\output\SnapReport.csv'

####################################################################################

send-mailmessage -From $mailfrom -To $mailto -Subject "Snapshot Cleanup - START" -SmtpServer $smtpServer

if (Test-Path $snapoutput -ErrorAction SilentlyContinue)
{
Remove-Item $snapoutput
}

Import-Module vmware.vimautomation.core

Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope session -Confirm:$false

Connect-VIServer $vcenter1, $vcenter2 -InformationAction SilentlyContinue


@(
foreach ($name in $names)
{
Get-Snapshot "$name" | Remove-Snapshot -Confirm:$false
#Write-Host "A snapshot was deleted from $name"
Write-Output "A snapshot was deleted from $name"
}
) | Out-File -FilePath $snapoutput

get-vm | get-snapshot | select VM,Name,Created,@{Name="SizeGB";Expression={[math]::Round($_.SizeGB,2)}} | Export-Csv -NoTypeInformation -UseCulture -Path $snapreport




i have 2 questions on above snap creation

1) $names = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist.txt'
#$names2 = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist2.txt'

the above script is being run from a management server- so there is hashtag against the second line -
#$names2 = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist2.txt' so this wont run?

2) i believe i need to update aplist .txt first with this command-get-vm | get-snapshot | select VM,Name,Created,@{Name="SizeGB";Expression={[math]::Round($_.SizeGB,2)}}


but when i run it in cli- it says get-vm is not recognized as commndlet?

any thoughts?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sebastian TalmonSystem Engineer Datacenter Solutions

Commented:
Do you have PowerCLI installed?
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
when you start powershell, are you sure that

Import-Module vmware.vimautomation.core

Open in new window


is executed.. it would seem like it's not loaded, and so any VMware related cmdlet, is not going to be recognised.

and when you type

get-vm, does it show in yellow ?

Author

Commented:
Yes I opened in power cli only

Can you help me in explaining what 2 questions I have asked
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
Answer 1.

yes # (hash) is a remark, at the beginning of a line is seen as a remark, and will not be executed.

ignored.

normally used for comments

# comments

but can also be used to temporary/permanently stop lines from being executed


Answer 2.

if you type

Import-Module vmware.vimautomation.core

Open in new window


in your Windows Powershell console prompt do you get an error ?

Author

Commented:
Let me try again

Author

Commented:
it says you are not connected to any server ?

Author

Commented:
please connect first using a connect commndlet

Author

Commented:
I ran this: Import-Module vmware.vimautomation.core

and then commnd get-vm | get-snapshot | select VM,Name,Created,@{Name="SizeGB";Expression={[math]::Round($_.SizeGB,2)}}  

then I got not connected to any server?

Author

Commented:
looks to me it is asking me to connect to $vcenter1 = 'testw'

Author

Commented:
what command should I use
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
You are best running commands manually, in powershell to test them before writing a script.

Connect-VIServer

Open in new window

Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
Checked your script here with some modifications so I could get it to run, adding variables, and it works fine.

I'm using Windows 10 - 1809.

VMware PowerCLI 6.5 Release 1 4624819.

PowerShell as supplied with Windows 10.

Author

Commented:
I just ran your command and it worked

Import-Module vmware.vimautomation.core
Connect-VIServer
get-vm | get-snapshot | select VM,Name,Created,@{Name="SizeGB";Expression={[math]::Round($_.SizeGB,2)}}  

I could get the result of which servers have snapshots .

server1 -3gb
server2 -4gb
server 3-1gb

now my last question :

the whole script name is cleanup.ps1

but before I run this script I need to populate "$names = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist.txt'"

as you see originally put in question ( aplist.txt) because this.ps1 script will clean the snapshots from this txt as per commands shown

so should I manually put the names in that aplist.txt or is there any command where I can export it to this list


but before I run this cleanup.ps1 script I
Andrew Hancock (VMware vExpert / EE Fellow)VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017

Commented:
what do you want the script to do, just clean ALL snapshots or be specific as to what VMs Snapshots you remove ?

Author

Commented:
above is the content of the script i have been given which is a .ps1 script

now when i run the command-get-vm | get-snapshot | select VM,Name,Created,@{Name="SizeGB";Expression={[math]::Round($_.SizeGB,2)}}

i get list of some VM's , only these i have to remove and not all VM's in the company.

i cannot make any changes in this script, so i have to populate this txt file i suppose as per the command given

$names = Get-Content '\\management server name\C$\Admin\Scripts\input\aplist.txt' ( which is aplist.txt)

and then run .ps1
VMware and Virtualization Consultant
Fellow 2018
Expert of the Year 2017
Commented:
okay so just add the VM names to the txt file, line by line.

edit aplist.txt, and add the name of the VMs in there, and execute the script.

simple.

Author

Commented:
Thanks  will let u know once I execute

Will keepbtcket open till then

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial