Hello Everyone!
I was hoping someone could possibly help me out with an issue I have been having when it comes to PowerShell scripts running PowerCLI commands. I have a couple scripts that run after backup jobs and replication jobs that connect to vCenter and run commands against VMs. One script will have vCenter reboot a server and the other will remove all snapshots associated with a VM after it being replicated. In both instances I can see in vCenter that the commands are trying to execute twice. I was wondering if anyone else has experienced this issue?
Here is my current setup: Veeam 9.0 on a 2012R2 Sever OS. PowerShell 5.1 installed on the 2012R2 OS. VMware.PowerCLI module (Version 10.1.1.8827524) installed on the 2012R2 Server, which was installed from the PowerShell Gallery (PSGallery).
Thank you in advance for any and all help with this!!!!
Without seeing your script it's hard to provide any helpful advice.
Sure, here is the first one:
Connect-VIServer vCenter -Credential (Import-Clixml PathToCredential.dat)
$Snapshots = Get-Snapshot -VM TESTING-VM
foreach ($Snap in $Snapshots) {
Remove-Snapshot -Snapshot $Snap -Confirm:$false
}
Disconnect-VIServer vCenter -Confirm:$false
Here is the second:
Param(
[Parameter(Mandatory=$True)]
[string]$ComputerName
)
function Send-HardRebootEmail {
<#
.SYNOPSIS
This will send an email alerting to the Hard Reboot
.DESCRIPTION
This function will send an email to inform staff that
a particular VM needed to be hard rebooted during the
Veeam backup process.
#>
BEGIN{}
PROCESS{
$Message="<br>WARNING - $ComputerName failed to reboot properly.<br>A hard reboot was performed on $ComputerName on $(Get-Date -UFormat "%a %D %T")<br>"
$Title = "TESTING - Hard Reboot of $ComputerName"
$Post = "<P>Thank You, <br> IT Support</P>"
$MessageParameters = @{
Subject = "TESTING - Hard Reboot of $ComputerName $(Get-Date -f yyyy-MM-dd)"
Body = ConvertTo-Html -Title $Title -Body $Message -Post $Post | Out-String
From = "SysAdmin@Company.com"
To = "SysAdmin@Company.com"
SmtpServer = "smtp.company.com"
}
Send-MailMessage @MessageParameters -BodyAsHtml
}
END{}
}
function HardRestart {
<#
.SYNOPSIS
Performs a hard restart of the VM through vCenter
.DESCRIPTION
This function will stop the VM by connecting to vCenter
and issuing a Stop-VM command. It will then wait for 30
seconds and issue a Start-VM command.
#>
BEGIN{}
PROCESS{
Stop-VM -VM $ComputerName | Out-Null
Start-Sleep -Seconds 30
Start-VM -VM $ComputerName | Out-Null
Send-HardRebootEmail
}
END{}
}
#----------Set Variables----------#
$LOG = "\\PathTo\Restart.log"
$Count = 0
#----------Set Domain Name----------#
try {
Test-Connection -ComputerName ($ComputerName + ".domain.local") -Count 1 -ErrorAction Stop | Out-Null
$DomainName = ($ComputerName + ".domain.local")
} catch {
try {
Test-Connection -ComputerName ($ComputerName + ".domain.com") -Count 1 -ErrorAction Stop | Out-Null
$DomainName = ($ComputerName + ".domain.com")
} catch {
Write-Output "This Computer does not exist: $ComputerName"
break
}
}
#----------Main Program----------#
Set-PowerCLIConfiguration -Scope AllUsers -InvalidCertificateAction Ignore -ParticipateInCeip $false -Confirm:$false | Out-Null
Connect-VIServer -Server vCenter -Credential (Import-Clixml -path PathToCredentails.dat) | Out-Null
Restart-Computer -ComputerName $DomainName -Force -ErrorAction SilentlyContinue
Do {
Start-Sleep -Seconds 2
$Count = $Count + 1
} Until (((Test-Connection $DomainName -Count 1 -Quiet) -eq $false) -or ($Count -eq 15))
if ($Count -eq 15){
HardRestart | Out-Null
}
Add-Content -Path $LOG -Value "$(Get-Date -UFormat "%a %D %T") Sent Restart CMD to $ComputerName"
Disconnect-VIServer -Server vCenter -Confirm:$false
For some reason when i posted this script, the forum added some weird tagging to it, especially in the portion for sending the email. That tagging is not there in my actual script.