Solved

Need some help with PowerShell Script  - Still Learning

Posted on 2016-08-18
6
40 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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 …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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