Jump to content
  • 0

Put into maintance mode and deleting multiple machines from catalog via script with import server list txt file


sid van

Question

Hello good day,

 

I have trying to delete a list of VDI in Xendesktop 1912 by using a powershell script.

To speed things up,  I have list of servers to be deleted put into a serverlist.txt.

Now I would like to generate a script that reads from that serverlist.txt (previously i added the servernames  into that list)   

 

The following has to be done:

 

Put the affected servers in maintance mode and show result on screen that servers are in maintance mode and press a key to continue   and shutdown the servers and show result on screen.

After that delete the servers from catalog and show result of the deletion of servers to a a file  for example  deleted-servers.txt. 

 

I have some code but some parts are missing:

 

#================================================= ========= #
# Get server list from serverlist.txt
#================================================= =========

 

Get-Content C:\serverlist.txt

foreach ($machine in $machines) {Set-BrokerMachineMaintenanceMode -InputObject $machine $true}

 

#================================================= ========= #
# How status of maintance status of servers on screen and press any key to continue
#================================================= =========

->> how can I get the result of the maintance status of the servers on screen?

 

Get-BrokerMachine -MachineName  xxxx  | fl InMaintenanceMode  ?

Pause and press any key to continue

 

 

#================================================= ========= #
#  Delete the affected servers from Catalog and write result to deleted-VDI.txt
#================================================= =========

 

Remove-BrokerMachine  -MachineName  xxxxx

 

Write results to file of deleted servers

 

Please help .. thanks in advance

Link to comment

2 answers to this question

Recommended Posts

  • 0

Part of your question seems to imply you don't necessarily have a good handle on some of the features (capabilities) that PowerShell has.... and it's hard to teach all those concepts with a single example.  But here's a few pointers to hopefully get you moving in the right direction, based on how I'd approach the problem... not that I'm an excellent PS teacher or anything, and probably not naming things fully. 

 

I note you read from your file with Get-Content. But you did nothing with the output from it. Your next line uses an object called $machines, so you probably forgot to assign the get-content to that. 

 

The next tip I'll give you is you want to TEST this individually and look at the returned objects along the way. So an example could be to inspect your machine list and confirm what information it has for you - so executing:

  $machines = get-content yourtextfile.txt

and then examining the outcome 

  $machines

will show you the objects and properties in there. 

 

Then you'll want to start building up your code. Might be worth testing the foreach loop using output rather than actions first up.

 

(as a bonus tip, personally I dislike using plurals for items that have multiple objects/values, because you can easily typo $machines somewhere and it won't be picked up in a syntax check; so I often use a pattern like $machinelist and $machine to represent the foreach iteration) 

 

So your foreach might start out as (with my modification for the list): 

 

  foreach ($machine in $machinelist) {

    write-host "Acting on $machine"

   #  Set-BrokerMachineMaintenanceMode -InputObject $machine $true

  }

 

The next thing to note is that each step you do should probably need you to collect the results from a previous step and use that to figure out what to do and what not to do. For example, you might want to check what machines are in maintenance mode before you set them in maintenance mode, and again after the fact to validate that they did go into maintenance.  You can do that many ways, but the simplest is to assign the return of get-brokermachine to a variable, or even just write it out.  The pattern I would use here though is to create an object that I can populate with the progress through the iteration.  So for me I create an empty array object, then in my loop I create a new-object, add members to that object, then add them to the array. Here's what that might look like:

 

$results = @()

foreach ($machine in $machinelist) {
    write-host "Acting on $machine"
    $beforestate = Get-BrokerMachine -MachineName $machine 
    Set-BrokerMachineMaintenanceMode -InputObject $machine -MaintenanceMode $true
    $afterstate = Get-BrokerMachine -MachineName $machine 
    $obj = new-object psobject
    $obj | Add-Member -MemberType NoteProperty -Name MachineName -Value $machine
    $obj | Add-Member -MemberType NoteProperty -Name BeforeMaintenanceState -Value $beforestate.InMaintenanceMode
    $obj | Add-Member -MemberType NoteProperty -Name AfterMaintenanceState -Value $afterstate.InMaintenanceMode
    $results += $obj
}
 

And then the next thing is you can just examine $results to see what happened

 

The next parts of your exercise are pretty simple extensions onto the above, but I'm not writing the whole script for you (I much prefer to teach someone to fish than to hand them a fish).  Two hints...

1) I would create new NoteProperty(s) in the first foreach and then add to that in a subsequent foreach as you remove the machines

2) I would check to see if the object was removed properly by doing a final get-brokermachine, using -erroraction silentlycontinue, to prove no record was found.

  

good luck

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...