In my last blog, I wrote a script that automates the virtual desktop procurement process based on pre-existing computer accounts in Active Directory. This can be located at the next location. However, in this scenario, I prompting the user to enter the name of a storage depot for the location of virtual desktop VHD files.
Well, if there are several SR? What happens if the selected SR fills? How can I adapt to the use of multiple SR without requiring the input of the end user so that the script can evolve?
I gave this reflection and I came up with some answers. However, I will use this blog to introduce a method that seems to me the best of all solutions I came in my personal opinion.
Script Snippet
$ O = Connect-XenServer-Server 192.168.0.0 -username 'root' -Password '111111' $ StorageFilter = Get-XenServer: SR | Where-Object {$ _ sm_config ["use_vhd"] eq 'true'.} | Sort-Object -Property 'physical_utilisation': StorageFilter foreach ($ Q $ StorageFilter) {$ size = $ Q.physical_size Util = $ $ $ Q.physical_utilisation GB = $ 1,000,000,000 DiffSize Size = $ - $ Util $ FreeSpace = $ DiffSize / Go if $ ( "local storage" of $ Q.name_label) {## do nothing with the SR labeled "local storage", which contains the operating system. } Elseif ($ FreeSpace -LT 150) ## 150 is an arbitrary number I came up with. {## Do nothing, since we do not want SRs with low disk space. } Else {## The next line is not needed, I just wanted to see the logic sank. $ Q.name_label + "is more than 150GB" + "$ FreeSpace" $ SR = $ Q $ SR ## This variable will be used elsewhere in the script for VM copies. StorageFilter break ## This breaks the loop after the first eligible SR is located. }}
Script Notes
$ StorageFilter = this variable acquires all SR available that we could see in XenCenter. SR Each object contains a property called 'SM_Config'. SM_Config contains a collection of key = value pairs. SR containing VHD files that represent virtual desktops contain a pair of 'Use_VHD = true "key = value. Therefore, the script selects only the Socialist-Revolutionaries with the value in its respective property. Finally, the script sorts the results based on "natural use" in ascending order. This means, the SR with more free space is on top with RH less free space down.
The script then launches a loop (foreach), which then processes all the SR that were previously filtered with a label called PowerShell: StorageFilter. This label is used to break the loop after the "break" keyword is performed on the label. In the "foreach" loop, the script calculates the free space on each SR. Free space is in terms of gigabytes. The script then filter all SR with the "Local Storage", which would normally be the SR for SR OS and all with free space less than a specified number (I chose for my 150GB arbitrarily laboratory). Finally, the variable $ SR is initialized with the resulting SR and the loop is broken. The variable that contains the $ SR SR result is then used later in the automation script to copy the VHD files. Theoretically, the script will always copy a VM to a SR with the most space available disk during each iteration; . Dynamically
Script Free Space
Just for fun, here's another script fragment which will acquire a list of all the SR containing virtual desktops and their disk space:
$ Storage = Get-XenServer: SR | Where-Object {$ _ sm_config ["use_vhd"] eq 'true'.} | Sort-Object 'physical_utilisation' -Property ForEach ($ x in $ Storage) {$ size = $ x.physical_size Util = $ $ $ x.physical_utilisation GB = $ 1,000,000,000 DiffSize Size = $ - $ Util $ FreeSpace = $ DiffSize / $ GB $ x.name_label + ": $ FreeSpace GB"}
hope this helps and I welcome your comments
.
0 Komentar