?
Solved

Need some help with PowerShell Script  - Still Learning

Posted on 2016-08-18
6
Medium Priority
?
43 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 2000 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
Is Your AD Toolbox Looking More Like a Toybox?

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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

743 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