build 6ddc7a35 | content blog-content@417154f · 338 posts | profiles 32 · corpus 315 | ● 0 skipped | format run

$ soultec get post/powercli-automating-vm-shutdown-triggered-by-usv -o yaml$ soultec get post/powercli-automating-vm-shutdown-triggered-by-usv -o json$ soultec get post/powercli-automating-vm-shutdown-triggered-by-usv -o toml$ soultec get post/powercli-automating-vm-shutdown-triggered-by-usv -o xml$ python3 powercli-automating-vm-shutdown-triggered-by-usv.pyPS> ./powercli-automating-vm-shutdown-triggered-by-usv.ps1$ go run powercli-automating-vm-shutdown-triggered-by-usv.go$ node powercli-automating-vm-shutdown-triggered-by-usv.js

apiVersion: soultec.ch/v1kind: Postmetadata: name: powercli-automating-vm-shutdown-triggered-by-usv locale: de labels: author: dario-doerflinger capability/virtualization: 1.42 capability/automation: 1.81 vendor/vmware: 0.98 annotations: source: blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md route: /de/insights/powercli-automating-vm-shutdown-triggered-by-usv/ schema: /nerd/schema/posts.json markdown: /de/insights/powercli-automating-vm-shutdown-triggered-by-usv.mdspec: title: VM-Shutdown per USV auslösen date: 2017-07-21 author: dario-doerflinger locale: de summary: >- Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt. capabilities: [virtualization, automation] vendors: [vmware] migrated: 2026-08-24 translationReviewed: false draft: false sections: - body: | Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt. Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht. Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten. Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts). ```powershell ############################################################################################ # Script name: EvacuateVMsFromUSV.ps1 # Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts # Version: 1.0 # Date: 20.07.2017 # Author: Dario Doerflinger (virtualfrog.wordpress.com) # History: 20.07.2017 - First tested release ############################################################################################ # Example: # e.g.: .\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwil param ( [string]$SiteToShutdown # Identifier of site ) $vCenter_server = "bezhvcs03.bechtlezh.ch" # clear global ERROR variable $Error.Clear() # import vmware related modules, get the credentials and connect to the vCenter server Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null $creds = Get-VICredentialStoreItem -file "C:\Users\Administrator\Desktop\login.creds" Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null # define global variables $current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss") $log_file = "C:\Users\Administrator\Desktop\\log_$(Get-Date -format "yyyyMMdd").txt" Function SetDRStoAutomatic ($cluster) { try { $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not set DRS Mode to automatic" } } Function RemoveRemovableMediaFromVMs($esxhost) { try { $esxhost | Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not get the vm objects from host." } } Function EvacuateVMsFromHost($esxhost) { try { $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not put host into maintenance mode" } } Function ShutDownHost($esxhost) { try { $esxhost | Stop-VMhost -Confirm:$false -Whatif } catch { Write-Host -Foregroundcolor:red "Could not shut down host" } } ###### Main Program ###### if ($SiteToShutdown -eq "Allschwil") { $hosts = @("bezhesx40.bechtlezh.ch") } elseif ($SiteToShutdown -eq "Pratteln") { $hosts = @("bezhesx41.bechtlezh.ch") } foreach ($esxhost in $hosts) { $cluster = (get-vmhost $esxhost).Parent SetDRStoAutomatic($cluster) $esxihost = Get-VMhost $esxhost RemoveRemovableMediaFromVMs($esxihost) EvacuateVMsFromHost($esxihost) ShutDownHost($esxihost) } # cleanup and removal of loaded VMware modules Disconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-Null Remove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-Null Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null # write all error messages to the log file Add-Content -Path $log_file -Value $Error ```status: corpus: 315 alsoLike: - {ref: solutions/vmware/vmware-cloud-foundation, score: 0.74} - {ref: solutions/vmware/vmware-cloud-foundation/vcf-fleet-management, score: 0.70} - {ref: posts/vmware-explore-las-vegas-hackathon-2025, score: 0.69} search: /de/search.json
{ "apiVersion": "soultec.ch/v1", "kind": "Post", "metadata": { "name": "powercli-automating-vm-shutdown-triggered-by-usv", "locale": "de", "labels": { "author": "dario-doerflinger", "capability/virtualization": 1.42, "capability/automation": 1.81, "vendor/vmware": 0.98 }, "annotations": { "source": "blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md", "route": "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/", "schema": "/nerd/schema/posts.json", "markdown": "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md" } }, "spec": { "title": "VM-Shutdown per USV auslösen", "date": "2017-07-21", "author": "dario-doerflinger", "locale": "de", "summary": "Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.", "capabilities": [ "virtualization", "automation" ], "vendors": [ "vmware" ], "migrated": "2026-08-24", "translationReviewed": false, "draft": false, "sections": [ { "body": "Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.\n\nIch habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.\n\nEine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.\n\nUpdate: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).\n\n```powershell\n\n############################################################################################\n# Script name: EvacuateVMsFromUSV.ps1\n# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts\n# Version: 1.0\n# Date: 20.07.2017\n# Author: Dario Doerflinger (virtualfrog.wordpress.com)\n# History: 20.07.2017 - First tested release\n############################################################################################\n\n# Example: # e.g.: .\\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwil\n\nparam (\n [string]$SiteToShutdown # Identifier of site\n)\n$vCenter_server = \"bezhvcs03.bechtlezh.ch\"\n# clear global ERROR variable\n$Error.Clear()\n\n# import vmware related modules, get the credentials and connect to the vCenter server\nImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\nImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null\n$creds = Get-VICredentialStoreItem -file \"C:\\Users\\Administrator\\Desktop\\login.creds\"\nConnect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null\n\n# define global variables\n\n$current_date = $(Get-Date -format \"dd.MM.yyyy HH:mm:ss\")\n$log_file = \"C:\\Users\\Administrator\\Desktop\\\\log_$(Get-Date -format \"yyyyMMdd\").txt\"\n\nFunction SetDRStoAutomatic ($cluster)\n{\n try {\n $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null\n } catch {\n Write-Host -Foregroundcolor:red \"Could not set DRS Mode to automatic\"\n }\n}\n\nFunction RemoveRemovableMediaFromVMs($esxhost)\n{\n try {\n $esxhost | Get-VM | Where-Object {$_.PowerState -eq \"PoweredOn\"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null\n\n } catch {\n Write-Host -Foregroundcolor:red \"Could not get the vm objects from host.\"\n }\n}\n\nFunction EvacuateVMsFromHost($esxhost)\n{\n try {\n $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null\n } catch {\n Write-Host -Foregroundcolor:red \"Could not put host into maintenance mode\"\n }\n}\n\nFunction ShutDownHost($esxhost)\n{\n try {\n $esxhost | Stop-VMhost -Confirm:$false -Whatif\n } catch {\n Write-Host -Foregroundcolor:red \"Could not shut down host\"\n }\n}\n\n###### Main Program ######\nif ($SiteToShutdown -eq \"Allschwil\") {\n $hosts = @(\"bezhesx40.bechtlezh.ch\")\n\n} elseif ($SiteToShutdown -eq \"Pratteln\")\n{\n $hosts = @(\"bezhesx41.bechtlezh.ch\")\n}\n\nforeach ($esxhost in $hosts)\n{\n $cluster = (get-vmhost $esxhost).Parent\n SetDRStoAutomatic($cluster)\n\n $esxihost = Get-VMhost $esxhost\n RemoveRemovableMediaFromVMs($esxihost)\n EvacuateVMsFromHost($esxihost)\n ShutDownHost($esxihost)\n}\n\n# cleanup and removal of loaded VMware modules\nDisconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-Null\nRemove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-Null\nRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n\n# write all error messages to the log file\nAdd-Content -Path $log_file -Value $Error\n```" } ] }, "status": { "corpus": 315, "alsoLike": [ { "ref": "solutions/vmware/vmware-cloud-foundation", "score": "0.74" }, { "ref": "solutions/vmware/vmware-cloud-foundation/vcf-fleet-management", "score": "0.70" }, { "ref": "posts/vmware-explore-las-vegas-hackathon-2025", "score": "0.69" } ], "search": "/de/search.json" }}
apiVersion = "soultec.ch/v1"kind = "Post"[metadata]name = "powercli-automating-vm-shutdown-triggered-by-usv"locale = "de"[metadata.labels]author = "dario-doerflinger""capability/virtualization" = 1.42"capability/automation" = 1.81"vendor/vmware" = 0.98[metadata.annotations]source = "blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md"route = "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/"schema = "/nerd/schema/posts.json"markdown = "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md"[spec]title = "VM-Shutdown per USV auslösen"date = 2017-07-21author = "dario-doerflinger"locale = "de"summary = "Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt."capabilities = ["virtualization", "automation"]vendors = ["vmware"]migrated = 2026-08-24translationReviewed = falsedraft = false[[spec.sections]]body = '''Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).```powershell############################################################################################# Script name: EvacuateVMsFromUSV.ps1# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts# Version: 1.0# Date: 20.07.2017# Author: Dario Doerflinger (virtualfrog.wordpress.com)# History: 20.07.2017 - First tested release############################################################################################# Example: # e.g.: .\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwilparam ( [string]$SiteToShutdown # Identifier of site)$vCenter_server = "bezhvcs03.bechtlezh.ch"# clear global ERROR variable$Error.Clear()# import vmware related modules, get the credentials and connect to the vCenter serverImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-NullImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null$creds = Get-VICredentialStoreItem -file "C:\Users\Administrator\Desktop\login.creds"Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null# define global variables$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")$log_file = "C:\Users\Administrator\Desktop\\log_$(Get-Date -format "yyyyMMdd").txt"Function SetDRStoAutomatic ($cluster){ try { $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not set DRS Mode to automatic" }}Function RemoveRemovableMediaFromVMs($esxhost){ try { $esxhost | Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not get the vm objects from host." }}Function EvacuateVMsFromHost($esxhost){ try { $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not put host into maintenance mode" }}Function ShutDownHost($esxhost){ try { $esxhost | Stop-VMhost -Confirm:$false -Whatif } catch { Write-Host -Foregroundcolor:red "Could not shut down host" }}###### Main Program ######if ($SiteToShutdown -eq "Allschwil") { $hosts = @("bezhesx40.bechtlezh.ch")} elseif ($SiteToShutdown -eq "Pratteln"){ $hosts = @("bezhesx41.bechtlezh.ch")}foreach ($esxhost in $hosts){ $cluster = (get-vmhost $esxhost).Parent SetDRStoAutomatic($cluster) $esxihost = Get-VMhost $esxhost RemoveRemovableMediaFromVMs($esxihost) EvacuateVMsFromHost($esxihost) ShutDownHost($esxihost)}# cleanup and removal of loaded VMware modulesDisconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-NullRemove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-NullRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# write all error messages to the log fileAdd-Content -Path $log_file -Value $Error```'''[status]corpus = 315search = "/de/search.json"[[status.alsoLike]]ref = "solutions/vmware/vmware-cloud-foundation"score = "0.74"[[status.alsoLike]]ref = "solutions/vmware/vmware-cloud-foundation/vcf-fleet-management"score = "0.70"[[status.alsoLike]]ref = "posts/vmware-explore-las-vegas-hackathon-2025"score = "0.69"
<?xml version="1.0" encoding="UTF-8"?><manifest kind="Post"> <apiVersion>soultec.ch/v1</apiVersion> <metadata> <name>powercli-automating-vm-shutdown-triggered-by-usv</name> <locale>de</locale> <labels> <author>dario-doerflinger</author> <entry key="capability/virtualization">1.42</entry> <entry key="capability/automation">1.81</entry> <entry key="vendor/vmware">0.98</entry> </labels> <annotations> <source>blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md</source> <route>/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/</route> <schema>/nerd/schema/posts.json</schema> <markdown>/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md</markdown> </annotations> </metadata> <spec> <title>VM-Shutdown per USV auslösen</title> <date>2017-07-21</date> <author>dario-doerflinger</author> <locale>de</locale> <summary>Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.</summary> <capabilities> <item>virtualization</item> <item>automation</item> </capabilities> <vendors> <item>vmware</item> </vendors> <migrated>2026-08-24</migrated> <translationReviewed>false</translationReviewed> <draft>false</draft> <sections> <section> <body>Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).```powershell############################################################################################# Script name: EvacuateVMsFromUSV.ps1# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts# Version: 1.0# Date: 20.07.2017# Author: Dario Doerflinger (virtualfrog.wordpress.com)# History: 20.07.2017 - First tested release############################################################################################# Example: # e.g.: .\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwilparam ( [string]$SiteToShutdown # Identifier of site)$vCenter_server = "bezhvcs03.bechtlezh.ch"# clear global ERROR variable$Error.Clear()# import vmware related modules, get the credentials and connect to the vCenter serverImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-NullImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null$creds = Get-VICredentialStoreItem -file "C:\Users\Administrator\Desktop\login.creds"Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null# define global variables$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")$log_file = "C:\Users\Administrator\Desktop\\log_$(Get-Date -format "yyyyMMdd").txt"Function SetDRStoAutomatic ($cluster){ try { $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not set DRS Mode to automatic" }}Function RemoveRemovableMediaFromVMs($esxhost){ try { $esxhost | Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not get the vm objects from host." }}Function EvacuateVMsFromHost($esxhost){ try { $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not put host into maintenance mode" }}Function ShutDownHost($esxhost){ try { $esxhost | Stop-VMhost -Confirm:$false -Whatif } catch { Write-Host -Foregroundcolor:red "Could not shut down host" }}###### Main Program ######if ($SiteToShutdown -eq "Allschwil") { $hosts = @("bezhesx40.bechtlezh.ch")} elseif ($SiteToShutdown -eq "Pratteln"){ $hosts = @("bezhesx41.bechtlezh.ch")}foreach ($esxhost in $hosts){ $cluster = (get-vmhost $esxhost).Parent SetDRStoAutomatic($cluster) $esxihost = Get-VMhost $esxhost RemoveRemovableMediaFromVMs($esxihost) EvacuateVMsFromHost($esxihost) ShutDownHost($esxihost)}# cleanup and removal of loaded VMware modulesDisconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-NullRemove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-NullRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# write all error messages to the log fileAdd-Content -Path $log_file -Value $Error``` </body> </section> </sections> </spec> <status> <corpus>315</corpus> <alsoLike> <item> <ref>solutions/vmware/vmware-cloud-foundation</ref> <score>0.74</score> </item> <item> <ref>solutions/vmware/vmware-cloud-foundation/vcf-fleet-management</ref> <score>0.70</score> </item> <item> <ref>posts/vmware-explore-las-vegas-hackathon-2025</ref> <score>0.69</score> </item> </alsoLike> <search>/de/search.json</search> </status></manifest>
# Code generated by the soultec.ch build. DO NOT EDIT.import jsonPAGE = { "apiVersion": "soultec.ch/v1", "kind": "Post", "metadata": { "name": "powercli-automating-vm-shutdown-triggered-by-usv", "locale": "de", "labels": { "author": "dario-doerflinger", "capability/virtualization": 1.42, "capability/automation": 1.81, "vendor/vmware": 0.98, }, "annotations": { "source": "blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md", "route": "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/", "schema": "/nerd/schema/posts.json", "markdown": "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md", }, }, "spec": { "title": "VM-Shutdown per USV auslösen", "date": "2017-07-21", "author": "dario-doerflinger", "locale": "de", "summary": "Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.", "capabilities": ["virtualization", "automation"], "vendors": ["vmware"], "migrated": "2026-08-24", "translationReviewed": False, "draft": False, "sections": [ { "body": ( "Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.\n" "\n" "Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.\n" "\n" "Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.\n" "\n" "Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).\n" "\n" "```powershell\n" "\n" "############################################################################################\n" "# Script name: EvacuateVMsFromUSV.ps1\n" "# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts\n" "# Version: 1.0\n" "# Date: 20.07.2017\n" "# Author: Dario Doerflinger (virtualfrog.wordpress.com)\n" "# History: 20.07.2017 - First tested release\n" "############################################################################################\n" "\n" "# Example: # e.g.: .\\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwil\n" "\n" "param (\n" " [string]$SiteToShutdown # Identifier of site\n" ")\n" "$vCenter_server = \"bezhvcs03.bechtlezh.ch\"\n" "# clear global ERROR variable\n" "$Error.Clear()\n" "\n" "# import vmware related modules, get the credentials and connect to the vCenter server\n" "Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n" "Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null\n" "$creds = Get-VICredentialStoreItem -file \"C:\\Users\\Administrator\\Desktop\\login.creds\"\n" "Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null\n" "\n" "# define global variables\n" "\n" "$current_date = $(Get-Date -format \"dd.MM.yyyy HH:mm:ss\")\n" "$log_file = \"C:\\Users\\Administrator\\Desktop\\\\log_$(Get-Date -format \"yyyyMMdd\").txt\"\n" "\n" "Function SetDRStoAutomatic ($cluster)\n" "{\n" " try {\n" " $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null\n" " } catch {\n" " Write-Host -Foregroundcolor:red \"Could not set DRS Mode to automatic\"\n" " }\n" "}\n" "\n" "Function RemoveRemovableMediaFromVMs($esxhost)\n" "{\n" " try {\n" " $esxhost | Get-VM | Where-Object {$_.PowerState -eq \"PoweredOn\"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null\n" "\n" " } catch {\n" " Write-Host -Foregroundcolor:red \"Could not get the vm objects from host.\"\n" " }\n" "}\n" "\n" "Function EvacuateVMsFromHost($esxhost)\n" "{\n" " try {\n" " $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null\n" " } catch {\n" " Write-Host -Foregroundcolor:red \"Could not put host into maintenance mode\"\n" " }\n" "}\n" "\n" "Function ShutDownHost($esxhost)\n" "{\n" " try {\n" " $esxhost | Stop-VMhost -Confirm:$false -Whatif\n" " } catch {\n" " Write-Host -Foregroundcolor:red \"Could not shut down host\"\n" " }\n" "}\n" "\n" "###### Main Program ######\n" "if ($SiteToShutdown -eq \"Allschwil\") {\n" " $hosts = @(\"bezhesx40.bechtlezh.ch\")\n" "\n" "} elseif ($SiteToShutdown -eq \"Pratteln\")\n" "{\n" " $hosts = @(\"bezhesx41.bechtlezh.ch\")\n" "}\n" "\n" "foreach ($esxhost in $hosts)\n" "{\n" " $cluster = (get-vmhost $esxhost).Parent\n" " SetDRStoAutomatic($cluster)\n" "\n" " $esxihost = Get-VMhost $esxhost\n" " RemoveRemovableMediaFromVMs($esxihost)\n" " EvacuateVMsFromHost($esxihost)\n" " ShutDownHost($esxihost)\n" "}\n" "\n" "# cleanup and removal of loaded VMware modules\n" "Disconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-Null\n" "Remove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-Null\n" "Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n" "\n" "# write all error messages to the log file\n" "Add-Content -Path $log_file -Value $Error\n" "```" ), }, ], }, "status": { "corpus": 315, "alsoLike": [ { "ref": "solutions/vmware/vmware-cloud-foundation", "score": "0.74", }, { "ref": "solutions/vmware/vmware-cloud-foundation/vcf-fleet-management", "score": "0.70", }, { "ref": "posts/vmware-explore-las-vegas-hackathon-2025", "score": "0.69", }, ], "search": "/de/search.json", },}if __name__ == "__main__": print(json.dumps(PAGE, indent=2, ensure_ascii=False))
# Code generated by the soultec.ch build. DO NOT EDIT.$Page = [ordered]@{ apiVersion = 'soultec.ch/v1' kind = 'Post' metadata = [ordered]@{ name = 'powercli-automating-vm-shutdown-triggered-by-usv' locale = 'de' labels = [ordered]@{ author = 'dario-doerflinger' 'capability/virtualization' = 1.42 'capability/automation' = 1.81 'vendor/vmware' = 0.98 } annotations = [ordered]@{ source = 'blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md' route = '/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/' schema = '/nerd/schema/posts.json' markdown = '/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md' } } spec = [ordered]@{ title = 'VM-Shutdown per USV auslösen' date = '2017-07-21' author = 'dario-doerflinger' locale = 'de' summary = 'Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.' capabilities = @('virtualization', 'automation') vendors = @('vmware') migrated = '2026-08-24' translationReviewed = $false draft = $false sections = @( [ordered]@{ body = @'Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).```powershell############################################################################################# Script name: EvacuateVMsFromUSV.ps1# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts# Version: 1.0# Date: 20.07.2017# Author: Dario Doerflinger (virtualfrog.wordpress.com)# History: 20.07.2017 - First tested release############################################################################################# Example: # e.g.: .\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwilparam ( [string]$SiteToShutdown # Identifier of site)$vCenter_server = "bezhvcs03.bechtlezh.ch"# clear global ERROR variable$Error.Clear()# import vmware related modules, get the credentials and connect to the vCenter serverImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-NullImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null$creds = Get-VICredentialStoreItem -file "C:\Users\Administrator\Desktop\login.creds"Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null# define global variables$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")$log_file = "C:\Users\Administrator\Desktop\\log_$(Get-Date -format "yyyyMMdd").txt"Function SetDRStoAutomatic ($cluster){ try { $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not set DRS Mode to automatic" }}Function RemoveRemovableMediaFromVMs($esxhost){ try { $esxhost | Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not get the vm objects from host." }}Function EvacuateVMsFromHost($esxhost){ try { $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null } catch { Write-Host -Foregroundcolor:red "Could not put host into maintenance mode" }}Function ShutDownHost($esxhost){ try { $esxhost | Stop-VMhost -Confirm:$false -Whatif } catch { Write-Host -Foregroundcolor:red "Could not shut down host" }}###### Main Program ######if ($SiteToShutdown -eq "Allschwil") { $hosts = @("bezhesx40.bechtlezh.ch")} elseif ($SiteToShutdown -eq "Pratteln"){ $hosts = @("bezhesx41.bechtlezh.ch")}foreach ($esxhost in $hosts){ $cluster = (get-vmhost $esxhost).Parent SetDRStoAutomatic($cluster) $esxihost = Get-VMhost $esxhost RemoveRemovableMediaFromVMs($esxihost) EvacuateVMsFromHost($esxihost) ShutDownHost($esxihost)}# cleanup and removal of loaded VMware modulesDisconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-NullRemove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-NullRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# write all error messages to the log fileAdd-Content -Path $log_file -Value $Error```'@ } ) } status = [ordered]@{ corpus = 315 alsoLike = @( [ordered]@{ ref = 'solutions/vmware/vmware-cloud-foundation' score = '0.74' } [ordered]@{ ref = 'solutions/vmware/vmware-cloud-foundation/vcf-fleet-management' score = '0.70' } [ordered]@{ ref = 'posts/vmware-explore-las-vegas-hackathon-2025' score = '0.69' } ) search = '/de/search.json' }}$Page | ConvertTo-Json -Depth 32
// Code generated by the soultec.ch build. DO NOT EDIT.package mainimport ( "encoding/json" "os")var page = map[string]any{ "apiVersion": "soultec.ch/v1", "kind": "Post", "metadata": map[string]any{ "name": "powercli-automating-vm-shutdown-triggered-by-usv", "locale": "de", "labels": map[string]any{ "author": "dario-doerflinger", "capability/virtualization": 1.42, "capability/automation": 1.81, "vendor/vmware": 0.98, }, "annotations": map[string]any{ "source": "blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md", "route": "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/", "schema": "/nerd/schema/posts.json", "markdown": "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md", }, }, "spec": map[string]any{ "title": "VM-Shutdown per USV auslösen", "date": "2017-07-21", "author": "dario-doerflinger", "locale": "de", "summary": "Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.", "capabilities": []any{"virtualization", "automation"}, "vendors": []any{"vmware"}, "migrated": "2026-08-24", "translationReviewed": false, "draft": false, "sections": []any{ map[string]any{ "body": "Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.\n" + "\n" + "Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.\n" + "\n" + "Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.\n" + "\n" + "Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).\n" + "\n" + "```powershell\n" + "\n" + "############################################################################################\n" + "# Script name: EvacuateVMsFromUSV.ps1\n" + "# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts\n" + "# Version: 1.0\n" + "# Date: 20.07.2017\n" + "# Author: Dario Doerflinger (virtualfrog.wordpress.com)\n" + "# History: 20.07.2017 - First tested release\n" + "############################################################################################\n" + "\n" + "# Example: # e.g.: .\\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwil\n" + "\n" + "param (\n" + " [string]$SiteToShutdown # Identifier of site\n" + ")\n" + "$vCenter_server = \"bezhvcs03.bechtlezh.ch\"\n" + "# clear global ERROR variable\n" + "$Error.Clear()\n" + "\n" + "# import vmware related modules, get the credentials and connect to the vCenter server\n" + "Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n" + "Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null\n" + "$creds = Get-VICredentialStoreItem -file \"C:\\Users\\Administrator\\Desktop\\login.creds\"\n" + "Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null\n" + "\n" + "# define global variables\n" + "\n" + "$current_date = $(Get-Date -format \"dd.MM.yyyy HH:mm:ss\")\n" + "$log_file = \"C:\\Users\\Administrator\\Desktop\\\\log_$(Get-Date -format \"yyyyMMdd\").txt\"\n" + "\n" + "Function SetDRStoAutomatic ($cluster)\n" + "{\n" + " try {\n" + " $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not set DRS Mode to automatic\"\n" + " }\n" + "}\n" + "\n" + "Function RemoveRemovableMediaFromVMs($esxhost)\n" + "{\n" + " try {\n" + " $esxhost | Get-VM | Where-Object {$_.PowerState -eq \"PoweredOn\"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null\n" + "\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not get the vm objects from host.\"\n" + " }\n" + "}\n" + "\n" + "Function EvacuateVMsFromHost($esxhost)\n" + "{\n" + " try {\n" + " $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not put host into maintenance mode\"\n" + " }\n" + "}\n" + "\n" + "Function ShutDownHost($esxhost)\n" + "{\n" + " try {\n" + " $esxhost | Stop-VMhost -Confirm:$false -Whatif\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not shut down host\"\n" + " }\n" + "}\n" + "\n" + "###### Main Program ######\n" + "if ($SiteToShutdown -eq \"Allschwil\") {\n" + " $hosts = @(\"bezhesx40.bechtlezh.ch\")\n" + "\n" + "} elseif ($SiteToShutdown -eq \"Pratteln\")\n" + "{\n" + " $hosts = @(\"bezhesx41.bechtlezh.ch\")\n" + "}\n" + "\n" + "foreach ($esxhost in $hosts)\n" + "{\n" + " $cluster = (get-vmhost $esxhost).Parent\n" + " SetDRStoAutomatic($cluster)\n" + "\n" + " $esxihost = Get-VMhost $esxhost\n" + " RemoveRemovableMediaFromVMs($esxihost)\n" + " EvacuateVMsFromHost($esxihost)\n" + " ShutDownHost($esxihost)\n" + "}\n" + "\n" + "# cleanup and removal of loaded VMware modules\n" + "Disconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-Null\n" + "Remove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-Null\n" + "Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n" + "\n" + "# write all error messages to the log file\n" + "Add-Content -Path $log_file -Value $Error\n" + "```", }, }, }, "status": map[string]any{ "corpus": 315, "alsoLike": []any{ map[string]any{ "ref": "solutions/vmware/vmware-cloud-foundation", "score": "0.74", }, map[string]any{ "ref": "solutions/vmware/vmware-cloud-foundation/vcf-fleet-management", "score": "0.70", }, map[string]any{ "ref": "posts/vmware-explore-las-vegas-hackathon-2025", "score": "0.69", }, }, "search": "/de/search.json", },}func main() { enc := json.NewEncoder(os.Stdout) enc.SetEscapeHTML(false) enc.SetIndent("", " ") if err := enc.Encode(page); err != nil { os.Exit(1) }}
// Code generated by the soultec.ch build. DO NOT EDIT.const page = { apiVersion: "soultec.ch/v1", kind: "Post", metadata: { name: "powercli-automating-vm-shutdown-triggered-by-usv", locale: "de", labels: { author: "dario-doerflinger", "capability/virtualization": 1.42, "capability/automation": 1.81, "vendor/vmware": 0.98, }, annotations: { source: "blog-content/posts/de/powercli-automating-vm-shutdown-triggered-by-usv.md", route: "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv/", schema: "/nerd/schema/posts.json", markdown: "/de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md", }, }, spec: { title: "VM-Shutdown per USV auslösen", date: "2017-07-21", author: "dario-doerflinger", locale: "de", summary: "Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.", capabilities: ["virtualization", "automation"], vendors: ["vmware"], migrated: "2026-08-24", translationReviewed: false, draft: false, sections: [ { body: "Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.\n" + "\n" + "Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.\n" + "\n" + "Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.\n" + "\n" + "Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).\n" + "\n" + "```powershell\n" + "\n" + "############################################################################################\n" + "# Script name: EvacuateVMsFromUSV.ps1\n" + "# Description: Evacuate all VMs from one site in a active-active cluster and shut down the Hosts\n" + "# Version: 1.0\n" + "# Date: 20.07.2017\n" + "# Author: Dario Doerflinger (virtualfrog.wordpress.com)\n" + "# History: 20.07.2017 - First tested release\n" + "############################################################################################\n" + "\n" + "# Example: # e.g.: .\\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwil\n" + "\n" + "param (\n" + " [string]$SiteToShutdown # Identifier of site\n" + ")\n" + "$vCenter_server = \"bezhvcs03.bechtlezh.ch\"\n" + "# clear global ERROR variable\n" + "$Error.Clear()\n" + "\n" + "# import vmware related modules, get the credentials and connect to the vCenter server\n" + "Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n" + "Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null\n" + "$creds = Get-VICredentialStoreItem -file \"C:\\Users\\Administrator\\Desktop\\login.creds\"\n" + "Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null\n" + "\n" + "# define global variables\n" + "\n" + "$current_date = $(Get-Date -format \"dd.MM.yyyy HH:mm:ss\")\n" + "$log_file = \"C:\\Users\\Administrator\\Desktop\\\\log_$(Get-Date -format \"yyyyMMdd\").txt\"\n" + "\n" + "Function SetDRStoAutomatic ($cluster)\n" + "{\n" + " try {\n" + " $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not set DRS Mode to automatic\"\n" + " }\n" + "}\n" + "\n" + "Function RemoveRemovableMediaFromVMs($esxhost)\n" + "{\n" + " try {\n" + " $esxhost | Get-VM | Where-Object {$_.PowerState -eq \"PoweredOn\"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null\n" + "\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not get the vm objects from host.\"\n" + " }\n" + "}\n" + "\n" + "Function EvacuateVMsFromHost($esxhost)\n" + "{\n" + " try {\n" + " $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not put host into maintenance mode\"\n" + " }\n" + "}\n" + "\n" + "Function ShutDownHost($esxhost)\n" + "{\n" + " try {\n" + " $esxhost | Stop-VMhost -Confirm:$false -Whatif\n" + " } catch {\n" + " Write-Host -Foregroundcolor:red \"Could not shut down host\"\n" + " }\n" + "}\n" + "\n" + "###### Main Program ######\n" + "if ($SiteToShutdown -eq \"Allschwil\") {\n" + " $hosts = @(\"bezhesx40.bechtlezh.ch\")\n" + "\n" + "} elseif ($SiteToShutdown -eq \"Pratteln\")\n" + "{\n" + " $hosts = @(\"bezhesx41.bechtlezh.ch\")\n" + "}\n" + "\n" + "foreach ($esxhost in $hosts)\n" + "{\n" + " $cluster = (get-vmhost $esxhost).Parent\n" + " SetDRStoAutomatic($cluster)\n" + "\n" + " $esxihost = Get-VMhost $esxhost\n" + " RemoveRemovableMediaFromVMs($esxihost)\n" + " EvacuateVMsFromHost($esxihost)\n" + " ShutDownHost($esxihost)\n" + "}\n" + "\n" + "# cleanup and removal of loaded VMware modules\n" + "Disconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-Null\n" + "Remove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-Null\n" + "Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n" + "\n" + "# write all error messages to the log file\n" + "Add-Content -Path $log_file -Value $Error\n" + "```", }, ], }, status: { corpus: 315, alsoLike: [ { ref: "solutions/vmware/vmware-cloud-foundation", score: "0.74", }, { ref: "solutions/vmware/vmware-cloud-foundation/vcf-fleet-management", score: "0.70", }, { ref: "posts/vmware-explore-las-vegas-hackathon-2025", score: "0.69", }, ], search: "/de/search.json", },};console.log(JSON.stringify(page, null, 2));

# 141 lines · YAML · validated at build# 60 lines · JSON · validated at build# 154 lines · TOML · validated at build# 165 lines · XML · validated at build# 166 lines · Python · read back at build, equals the JSON record# 163 lines · PowerShell · read back at build, equals the JSON record# 176 lines · Go · read back at build, equals the JSON record# 161 lines · JavaScript · read back at build, equals the JSON record · served at /de/insights/powercli-automating-vm-shutdown-triggered-by-usv.md

Beitrag · 2017-07-21

VM-Shutdown per USV auslösen

Wieder eine Kundenanforderung: Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.

2017-07-21Datum
Dario DörflingerAutor
2Min. Lesezeit
Hersteller VMware 0.98

Dieser Beitrag ist von 2017. Er bleibt online, weil er nach wie vor nachgefragt wird, beschreibt aber einen Produktstand von damals.

Wieder eine Kundenanforderung. Die USV soll bei einem Stromausfall ein Script auslösen, das alle VMs von einem Rechenzentrum ins andere verschiebt.

Ich habe angefangen, das folgende Script zu schreiben, bin aber noch nicht fertig, weil ein Teil der Engineers des Kunden in den Ferien ist. Ich führe diesen Post nach, sobald die endgültige Fassung steht.

Eine Anmerkung: Es verwendet denselben VI Credential Store wie mein früheres Script zur automatischen Erstellung von VM-Skeletten.

Update: Dieses Script liegt inzwischen auf GitHub.


############################################################################################
# Script name:     EvacuateVMsFromUSV.ps1
# Description:     Evacuate all VMs from one site in a active-active cluster and shut down the Hosts
# Version:         1.0
# Date:            20.07.2017
# Author:          Dario Doerflinger (virtualfrog.wordpress.com)
# History:         20.07.2017 - First tested release
############################################################################################

# Example: # e.g.: .\EvacuateVMsFromUSV.ps1 -SiteToShutdown Allschwil

param (
    [string]$SiteToShutdown # Identifier of site
)
$vCenter_server = "bezhvcs03.bechtlezh.ch"
# clear global ERROR variable
$Error.Clear()

# import vmware related modules, get the credentials and connect to the vCenter server
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null
Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null
$creds = Get-VICredentialStoreItem -file  "C:\Users\Administrator\Desktop\login.creds"
Connect-VIServer -Server $vCenter_server -User $creds.User -Password $creds.Password |Out-Null

# define global variables

$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")
$log_file = "C:\Users\Administrator\Desktop\\log_$(Get-Date -format "yyyyMMdd").txt"

Function SetDRStoAutomatic ($cluster)
{
    try {
        $cluster | Set-Cluster -DrsEnabled:$true -DrsAutomationLevel FullyAutomated -Confirm:$false |Out-Null
    } catch {
        Write-Host -Foregroundcolor:red "Could not set DRS Mode to automatic"
    }
}

Function RemoveRemovableMediaFromVMs($esxhost)
{
    try {
        $esxhost | Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False |Out-Null

    } catch {
        Write-Host -Foregroundcolor:red "Could not get the vm objects from host."
    }
}

Function EvacuateVMsFromHost($esxhost)
{
    try {
        $esxhost | Set-VMHost -State Maintenance -Evacuate:$true -Confirm:$false |Out-Null
    } catch {
        Write-Host -Foregroundcolor:red "Could not put host into maintenance mode"
    }
}

Function ShutDownHost($esxhost)
{
    try {
       $esxhost | Stop-VMhost -Confirm:$false -Whatif
    } catch {
        Write-Host -Foregroundcolor:red "Could not shut down host"
    }
}

###### Main Program ######
if ($SiteToShutdown -eq "Allschwil") {
    $hosts = @("bezhesx40.bechtlezh.ch")

} elseif ($SiteToShutdown -eq "Pratteln")
{
    $hosts = @("bezhesx41.bechtlezh.ch")
}

foreach ($esxhost in $hosts)
{
    $cluster = (get-vmhost $esxhost).Parent
    SetDRStoAutomatic($cluster)

    $esxihost = Get-VMhost $esxhost
    RemoveRemovableMediaFromVMs($esxihost)
    EvacuateVMsFromHost($esxihost)
    ShutDownHost($esxihost)
}

# cleanup and removal of loaded VMware modules
Disconnect-VIServer -Server $vCenter_server -Confirm:$false |Out-Null
Remove-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue | Out-Null
Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null

# write all error messages to the log file
Add-Content -Path $log_file -Value $Error

Passt ausserdem