Initialer Import der Synology Scripts
This commit is contained in:
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
+15
@@ -0,0 +1,15 @@
|
||||
$Config = @{
|
||||
Cluster = "INFRA"
|
||||
CSVGroups = "C:\\Shutdown\\Input.csv"
|
||||
LogPath = "C:\\Shutdown"
|
||||
|
||||
```
|
||||
VCenterServer = "vcsa01.local"
|
||||
|
||||
CredFiles = @{
|
||||
vCenter = "C:\Shutdown\vcenter-cred.xml"
|
||||
storage = "C:\Shutdown\storage.xml"
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
. "C:\\Shutdown\\Config.ps1"
|
||||
|
||||
Import-Module ".\\Modules\\Logging.psm1"
|
||||
Import-Module ".\\Modules\\Retry.psm1"
|
||||
Import-Module ".\\Modules\\Credentials.psm1"
|
||||
Import-Module ".\\Modules\\VCenter.psm1"
|
||||
Import-Module ".\\Modules\\Shutdown.psm1"
|
||||
|
||||
Start-Transcript -Path (Join-Path $Config.LogPath "shutdown.log")
|
||||
|
||||
Write-Log "Shutdown started"
|
||||
|
||||
$vCenterCred = Get-CredentialFromFile $Config.CredFiles.vCenter
|
||||
|
||||
Connect-ToVCenter -Server $Config.VCenterServer -Credential $vCenterCred
|
||||
|
||||
$VMGroups = Import-Csv $Config.CSVGroups -Delimiter ";"
|
||||
$VMGroups | ForEach-Object { $*.Gruppe = [int]$*.Gruppe }
|
||||
|
||||
Stop-UnknownVMs -VMGroups $VMGroups -ExcludeRegex "vCLS|vSAN"
|
||||
Stop-VMsByGroup -VMGroups $VMGroups
|
||||
|
||||
Stop-ClusterSafe -Cluster $Config.Cluster -Reason "Automated shutdown"
|
||||
|
||||
Write-Log "Shutdown completed"
|
||||
|
||||
Stop-Transcript
|
||||
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
+12
@@ -0,0 +1,12 @@
|
||||
function Get-CredentialFromFile {
|
||||
param([string]$Path)
|
||||
|
||||
```
|
||||
if (!(Test-Path $Path)) {
|
||||
throw "Missing credential file: $Path"
|
||||
}
|
||||
|
||||
Import-Clixml -Path $Path
|
||||
```
|
||||
|
||||
}
|
||||
Executable
+26
@@ -0,0 +1,26 @@
|
||||
enum LogLevel {
|
||||
INFO
|
||||
WARN
|
||||
ERROR
|
||||
DEBUG
|
||||
}
|
||||
|
||||
function Write-Log {
|
||||
param(
|
||||
[string]$Message,
|
||||
[LogLevel]$Level = [LogLevel]::INFO
|
||||
)
|
||||
|
||||
```
|
||||
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$line = "[$ts][$Level] $Message"
|
||||
|
||||
switch ($Level) {
|
||||
"INFO" { Write-Host $line -ForegroundColor Gray }
|
||||
"WARN" { Write-Host $line -ForegroundColor Yellow }
|
||||
"ERROR" { Write-Host $line -ForegroundColor Red }
|
||||
"DEBUG" { Write-Host $line -ForegroundColor DarkGray }
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
function Invoke-Retry {
|
||||
param(
|
||||
[scriptblock]$Action,
|
||||
[int]$Retries = 3,
|
||||
[int]$DelaySeconds = 5,
|
||||
[string]$Name = "Operation"
|
||||
)
|
||||
|
||||
```
|
||||
for ($i = 1; $i -le $Retries; $i++) {
|
||||
try {
|
||||
return & $Action
|
||||
}
|
||||
catch {
|
||||
Write-Log "$Name failed ($i/$Retries): $($_.Exception.Message)" -Level WARN
|
||||
|
||||
if ($i -eq $Retries) { throw }
|
||||
|
||||
Start-Sleep $DelaySeconds
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
Executable
+68
@@ -0,0 +1,68 @@
|
||||
function Stop-VMParallel {
|
||||
param([array]$VMs, [int]$Throttle = 8)
|
||||
|
||||
```
|
||||
$VMs | ForEach-Object -Parallel {
|
||||
Import-Module VMware.PowerCLI -ErrorAction SilentlyContinue
|
||||
|
||||
$vm = $_
|
||||
|
||||
if ($vm.PowerState -ne "PoweredOn") { return }
|
||||
|
||||
try {
|
||||
Shutdown-VMGuest -VM $vm -Confirm:$false -ErrorAction Stop
|
||||
}
|
||||
catch {
|
||||
Write-Host "[ERROR] VM failed: $($vm.Name)"
|
||||
}
|
||||
} -ThrottleLimit $Throttle
|
||||
```
|
||||
|
||||
}
|
||||
|
||||
function Stop-VMsByGroup {
|
||||
param($VMGroups)
|
||||
|
||||
```
|
||||
$groups = $VMGroups.Gruppe | Sort-Object -Unique
|
||||
|
||||
foreach ($g in $groups) {
|
||||
|
||||
$names = ($VMGroups | Where-Object Gruppe -eq $g).Server
|
||||
$vms = Get-VM | Where-Object { $names -contains $_.Name }
|
||||
|
||||
if ($vms) {
|
||||
Stop-VMParallel -VMs $vms
|
||||
Start-Sleep 30
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
|
||||
function Stop-UnknownVMs {
|
||||
param($VMGroups, $ExcludeRegex)
|
||||
|
||||
```
|
||||
$vms = Get-VM | Where-Object {
|
||||
($VMGroups.Server -notcontains $_.Name) -and
|
||||
($_.Name -notmatch $ExcludeRegex)
|
||||
}
|
||||
|
||||
if ($vms) {
|
||||
Stop-VMParallel -VMs $vms
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
|
||||
function Stop-ClusterSafe {
|
||||
param([string]$Cluster, [string]$Reason)
|
||||
|
||||
```
|
||||
Invoke-Retry -Name "Cluster shutdown" -Action {
|
||||
Stop-VsanCluster -Cluster $Cluster -PowerOffReason $Reason -ErrorAction Stop | Out-Null
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
function Connect-ToVCenter {
|
||||
param(
|
||||
[string]$Server,
|
||||
[pscredential]$Credential
|
||||
)
|
||||
|
||||
```
|
||||
Import-Module VMware.PowerCLI -ErrorAction Stop
|
||||
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
|
||||
|
||||
Invoke-Retry -Name "vCenter connect" -Action {
|
||||
Connect-VIServer -Server $Server -Credential $Credential -ErrorAction Stop | Out-Null
|
||||
}
|
||||
```
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user