-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alterar-EstadoVM.ps1
68 lines (66 loc) · 2.48 KB
/
Alterar-EstadoVM.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
57
58
59
60
61
62
63
64
65
66
67
68
# Script para ativar ou desativar maquina virtual
param([parameter(Mandatory=$true)][int]$Computador,[parameter(Mandatory=$true)][int]$Acao)
$ErrorActionPreference = "Stop"
$FileCSV = "C:\MIGRA\Planilha_Migracao.csv"
$PrefixoServidor = $((Import-Csv $FileCSV -Delimiter ";").HOSTNAMEATUAL).Substring(0,2)
if (($Computador -ge 1 -and $Computador -le 3) -and ($Acao -eq 0 -or $Acao -eq 1)) {
$FileCSV = "C:\MIGRA\Planilha_Migracao.csv"
$CodFilial = $((Import-Csv $FileCSV -Delimiter ";").CODIGO)
$Maquinas = ($PrefixoServidor + $CodFilial + "_GER01"),("S3DCAG"+ $CodFilial),("S3LSHP" + $CodFilial)
$VM = $Maquinas[$Computador-1]
Write-Host "VM:" $VM
if ($Acao -eq 0) {
Write-Host "Desligando VM $VM..."
Try {
Stop-VM -Name $VM -Force | Out-Null
}
Catch {
Write-Host -ForegroundColor Red "Impossivel desligar VM $VM!"
Write-Host -ForegroundColor Red "Mensagem de Erro: " $_.Exception.Message
Write-Host -ForegroundColor Red "Item: " $_.Exception.ItemName
Exit
}
Finally {
Write-Host "VM Desligada."
}
}
else {
Write-Host "Iniciando VM $VM..."
Try {
Start-VM -Name $VM | Out-Null
}
Catch {
Write-Host -ForegroundColor Red "Impossivel ligar VM $VM!"
Write-Host -ForegroundColor Red "Mensagem de Erro: " $_.Exception.Message
Write-Host -ForegroundColor Red "Item: " $_.Exception.ItemName
Exit
}
Finally {
Do
{
Write-Host "Aguardando ativaco da VM $VM..."
$Estado = Get-VMIntegrationService -VMName $VM -Name Heartbeat
Sleep 5
}
while ($Estado.PrimaryStatusDescription -ne "OK")
Write-Host "VM Ligada."
}
}
Write-Host "Script finalizado."
Write-Host
}
else {
Write-Host "***"
Write-Host "Erro de parametro! Eh preciso especificar o parametro -Computador."
Write-Host "Uso:"
Write-Host " .\AlterarEstado-VM.ps1 -Computador {1 | 2 | 3} -Acao {0 | 1}"
Write-Host " Valores para o parametro -Computador:"
Write-Host " 1 = Maquina virtual 1"
Write-Host " 2 = Maquina virtual 2"
Write-Host " 3 = Maquina virtual 3"
Write-Host "***"
Write-Host " Valores para o parametro -Acao:"
Write-Host " 0 = Desligar maquina virtual"
Write-Host " 1 = Ligar maquina virtual"
Write-Host
}