-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartVM.ps1
56 lines (48 loc) · 1.57 KB
/
StartVM.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<# Import-Module -Name Vmware.PowerCLI
# To ignore CEIP participation.
# Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
# To ignore certificate error
# Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
#>
$Server = 'vmware serverip'
$User = 'root'
$Password = 'pass'
# Save copy logs.
$Logpath = $PSScriptRoot
$LogFile = $Logpath+'\'+'startvms.log'
#Function to Create a Log File
Function Write-Log {
param(
[Parameter(Mandatory = $true)][string] $Message,
[Parameter(Mandatory = $false)] [ValidateSet("INFO","WARNING","ERROR")] [string] $Level = "INFO"
)
$Timestamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
Add-Content -Path $LogFile -Value "$timestamp [$Level] - $Message"
}
Start-Sleep -Seconds 10
Write-Log -level INFO -message "------ PowerOn VM Script started ------"
#Start-Transcript $Logfile
Connect-VIServer -Server $Server -User $User -Password $Password
$Vms = Get-VM
try{
foreach($vm in $Vms)
{
if($vm.PowerState -ne 'PoweredON')
{
Write-Log -Level INFO -Message "Starting Guest VM: $($vm.Name)"
Start-VM -VM $vm.Name -Confirm:$false
Start-Sleep -Seconds 10
}#if
else
{
Write-Log -Level INFO -Message "$($vm.Name) is already powered on...."
}#else
}#foreach
}#try
Catch
{
Write-Log -Level ERROR -Message "Error....!!!!"
}#catch
Disconnect-VIServer -Server $Server -Confirm:$false
Write-Log -level INFO -message "------ PowerOn VM Script ended ------"
#Stop-Transcript