Skip to content

Commit fc16bb4

Browse files
New VMWare virtual machines script
1 parent 980195f commit fc16bb4

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Requires -Version 4.0
2+
# Requires -Modules VMware.PowerCLI
3+
4+
<#
5+
.SYNOPSIS
6+
Start, stop, restart or suspend VMWare virtual machines
7+
8+
.DESCRIPTION
9+
This script allows to start, stop, restart or suspend VMWare virtual machines. To can choose a single machine or
10+
multiple machines.
11+
12+
.NOTES
13+
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
14+
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
15+
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
16+
the use and the consequences of the use of this freely available script.
17+
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
18+
©ScriptRunner Software GmbH
19+
20+
.COMPONENT
21+
Requires Module VMware.PowerCLI
22+
23+
.LINK
24+
25+
.Parameter Action
26+
Specifies the Action for the virtual machines
27+
28+
.Parameter VIServer
29+
Specifies the IP address or the DNS name of the vSphere server to which you want to connect
30+
31+
.Parameter VICredential
32+
Specifies a PSCredential object that contains credentials for authenticating with the server
33+
34+
.Parameter VirtualMachines
35+
Specifies the virtual machines to be start, stop, restart or suspend
36+
#>
37+
38+
param(
39+
[ValidateSet("Start", "Stop", "Restart", "Suspend")]
40+
[string]$Action,
41+
[Parameter(Mandatory = $true)]
42+
[string]$VIServer,
43+
[Parameter(Mandatory = $true)]
44+
[pscredential]$VICredential,
45+
[Parameter(Mandatory = $true, HelpMessage = "ASRDisplay(NoAutoSelect)")]
46+
[string[]]$VirtualMachines
47+
)
48+
49+
Import-Module VMware.PowerCLI
50+
51+
try {
52+
$vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
53+
54+
$cmdArgs = @{
55+
'Server' = $vmServer
56+
'Confirm' = $false
57+
'RunAsync' = $true
58+
'VM' = ''
59+
}
60+
61+
if ($vmServer) {
62+
if ($Action -eq "Start") {
63+
foreach ($item in $VirtualMachines) {
64+
try {
65+
$cmdArgs.VM = $item
66+
Start-VM @cmdArgs
67+
Write-Output "Computer $($item) startet successfully"
68+
}
69+
catch {
70+
Write-Output "A problem occured starting the VM $($item)"
71+
}
72+
}
73+
}
74+
}
75+
if ($Action -eq "Stop") {
76+
foreach ($item in $VirtualMachines) {
77+
try {
78+
$cmdArgs.VM = $item
79+
Stop-VM @cmdArgs
80+
Write-Output "Computer $($item) stopped successfully"
81+
}
82+
catch {
83+
Write-Output "A problem occured stopping the VM $($item)"
84+
}
85+
}
86+
}
87+
if ($Action -eq "Restart") {
88+
foreach ($item in $VirtualMachines) {
89+
try {
90+
$cmdArgs.VM = $item
91+
Restart-VM @cmdArgs
92+
Write-Output "Computer $($item) restartet successfully"
93+
}
94+
catch {
95+
Write-Output "A problem occured restarting the VM $($item)"
96+
}
97+
}
98+
}
99+
if ($Action -eq "Suspend") {
100+
foreach ($item in $VirtualMachines) {
101+
try {
102+
$cmdArgs.VM = $item
103+
Suspend-VM @cmdArgs
104+
Write-Output "Computer $($item) suspended successfully"
105+
}
106+
catch {
107+
Write-Output "A problem occured suspending the VM $($item)"
108+
}
109+
}
110+
else {
111+
Write-Output "Connection to VI Server not established"
112+
}
113+
}
114+
}
115+
catch {
116+
throw "Something went wrong"
117+
}
118+
finally {
119+
Disconnect-VIServer -Server $vmServer
120+
}
121+

0 commit comments

Comments
 (0)