Solved

Need some help with PowerShell Script  - Still Learning

Posted on 2016-08-18
6
37 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
ID: 41761920
## $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
ID: 41761971
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
ID: 41761982
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 2

Author Comment

by:alexr54
ID: 41761992
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
ID: 41761998
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
ID: 41762012
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 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