Solved

Need some help with PowerShell Script  - Still Learning

Posted on 2016-08-18
6
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 37

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 37

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 37

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Job running powershell script 2 32
How to connect printer in workstation by GPO and by site? 6 50
Powershell Code 4 31
Powershell 7zip extract and Move 7 35
Set OWA language and time zone in Exchange for individuals, all users or per database.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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