Solved

Need some help with PowerShell Script  - Still Learning

Posted on 2016-08-18
6
32 Views
Last Modified: 2016-08-19
I am making a script which will delete VMs in VSphere based on a list of machines.
However instead of turning off an active machine, I would like to skip it, and notify its been skipped because its active.

Here is my current script, which checks for active machines, but it just turns them off and then deletes them.
Can anyone help with this script which will use the active machines and skip them in the deletion command?


$VMs = (Get-Content c:\lists\servers.txt)
$vmObj = Get-vm $vms
 
foreach($active in $vmObj){
if($active.PowerState -eq "PoweredOn"){
Stop-VM -VM $active -Confirm:$false -RunAsync | Out-Null}
}
Start-Sleep -Seconds 7
 
foreach($delete in $vmObj){
Remove-VM -VM $delete -DeleteFromDisk -Confirm:$false -RunAsync | Out-Null}


Something like:
foreach($active in $vmObj){
if($active.PowerState -eq "PoweredOn"){
write-host "The machine $active is powered on and will not be deleted" }
Start-Sleep -Seconds 7

And then somehow remove the $active machines from the $delete list.

Help?
0
Comment
Question by:alexr54
  • 3
  • 3
6 Comments
 
LVL 36

Accepted Solution

by:
Jian An Lim earned 500 total points
Comment Utility
## $VMs = (Get-Content c:\lists\servers.txt) <-- this line is not working
$vmObj = Get-vm $vms

$newlist = $vmobj
foreach($active in $vmObj){
if($active.PowerState -eq "PoweredOn"){
write-host "The machine $active is powered on and will not be deleted. It will power down soon"
Stop-VM -VM $active -Confirm:$false -RunAsync | Out-Null  
$newlist = $newlist | ? {$_.name -ne $($active.name)}
  }
}
Start-Sleep -Seconds 7


foreach($delete in $newlist){
Remove-VM -VM $delete -DeleteFromDisk -Confirm:$false -RunAsync | Out-Null}
0
 
LVL 2

Author Comment

by:alexr54
Comment Utility
If I am understanding correctly. If I don't want to actually turn off any VMs I can simply remove this line?

Stop-VM -VM $active -Confirm:$false -RunAsync | Out-Null

For example, I can be given a list of 200 VMs to delete. If any of these VMs are in the list by accident, I don't want to turn it off and delete it.
I just want to specify the name is turned on and will not be deleted, then the next function in the script will not execute for the turned on machine name.
0
 
LVL 36

Expert Comment

by:Jian An Lim
Comment Utility
Stop-VM will turn off the VM. so you # it out, then the line will not run.

and in fact, it will be easier to say you just want to delete anything which is not powered ON you can run something like this.

$vmObj = Get-vm $vms

$newlist = $vmobj
foreach($active in ($vmObj | where {$_.powerstate -ne "PoweredOn"}) ){
## remove VM that power is not On 
Remove-VM -VM $delete -DeleteFromDisk -Confirm:$false -RunAsync | Out-Null
  } 
}

Open in new window

The above script will NEVER remove a server that is power on..
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:alexr54
Comment Utility
Thanks.
But I would like to know which were skipped due to being powered on. So that's why I added the write-host to it.

So at this point I just need to import the list, and I will test the above options.
I understand even if the delete command hits a powered on server it won't work. I would just like to avoid the errors from running a delete command on a powered on server.
0
 
LVL 2

Author Comment

by:alexr54
Comment Utility
Also why would this line not work to import names from a txt file? Ive never had an issue with this before

$vms = Get-Content -Path "c:\lists\servers.txt"
0
 
LVL 36

Expert Comment

by:Jian An Lim
Comment Utility
my fault. you are right, it will work. first line is working, i must be blind or mislook a line.

Anyway, if you want to know whether it is power on
you can immediately tell by doing

$vmObj | where {$_.powerstate -eq "PoweredOn"}

Open in new window


This command will tell you which server is powered on.
you don't need it in the loop.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now