powershell, vmware

####################################################################################
$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?
pramod1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sebastian TalmonSystem Engineer Datacenter SolutionsCommented:
Do you have PowerCLI installed?
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
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 ?
pramod1Author Commented:
Yes I opened in power cli only

Can you help me in explaining what 2 questions I have asked
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
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 ?
pramod1Author Commented:
Let me try again
pramod1Author Commented:
it says you are not connected to any server ?
pramod1Author Commented:
please connect first using a connect commndlet
pramod1Author 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?
pramod1Author Commented:
looks to me it is asking me to connect to $vcenter1 = 'testw'
pramod1Author Commented:
what command should I use
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
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 MVE^2)VMware and Virtualization ConsultantCommented:
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.
pramod1Author 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 MVE^2)VMware and Virtualization ConsultantCommented:
what do you want the script to do, just clean ALL snapshots or be specific as to what VMs Snapshots you remove ?
pramod1Author 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
Andrew Hancock (VMware vExpert / EE MVE^2)VMware and Virtualization ConsultantCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pramod1Author Commented:
Thanks  will let u know once I execute

Will keepbtcket open till then
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.