PowerShell script exchange
Post Reply
luanntd
Lurker
Posts: 1
Liked: never
Joined: Aug 19, 2024 7:56 am
Full Name: Luan Nguyen Truong Duy
Contact:

Post-Script in Replication job

Post by luanntd »

Hello everyone,

I am facing an issue with running a script within a Replication job for VM to Cloud. When replicating a VM from the data center to the Cloud, Veeam hides the Re-IP feature. Therefore, I wrote a script to change the IP of the VM after Veeam replicates it to the Cloud. When I tested the script on the VM's PowerShell, I successfully connected to the replicated VM and changed the IP. However, when I added the script to the Post-Script of the Veeam Replication job, I wanted to use the existing username and password from Veeam Credentials & Passwords, but it didn't work because the returned password was null. Is there a way to use the existing username and password in Veeam Credentials to log into the Cloud so that the script can continue changing the IP for the replicated VM?

Here is my script:

# Path to the log file
$logFilePath = "C:\log\changips1_user_log.txt"

# Start logging
Start-Transcript -Path $logFilePath -Append

# Add a line with the full timestamp (date, hour, minute, second) to the log
Write-Output ("[{0:yyyy-MM-dd HH:mm:ss}] Your log message here" -f (Get-Date))

# Import necessary modules
Write-Host "Importing VMware PowerCLI and Veeam PowerShell modules..."
Import-Module -Name "VMware.VimAutomation.Core"
Import-Module -Name "Veeam.Backup.PowerShell"

# Retrieve credentials from Veeam
$veeamCredentialName = "veeam" # Replace with the name of the Credential in Veeam
$veeamCredentials = Get-VBRCredentials -Name $veeamCredentialName

# Check if credentials are null
if ($veeamCredentials -eq $null) {
Write-Host "Failed to retrieve Veeam credentials. Credential is null. Exiting script."
Stop-Transcript
exit 1
}

# Check if username or password is null
if ($veeamCredentials.UserName -eq $null -or $veeamCredentials.Password -eq $null) {
Write-Host "Username or password is null. Exiting script."
Stop-Transcript
exit 1
}

# Convert Veeam credentials to PSCredential
$username = $veeamCredentials.UserName
$password = $veeamCredentials.Password | ConvertTo-SecureString
$vmwareCredentials = New-Object System.Management.Automation.PSCredential($username, $password)

# Connect to VMware vSphere or vCenter using credentials from Veeam
$vmwareServer = "10.255.21.200"
try {
Connect-VIServer -Server $vmwareServer -Credential $vmwareCredentials
Write-Host "Successfully connected to VMware server $vmwareServer."
} catch {
Write-Host "Failed to connect to VMware server $vmwareServer: $($Error[0].Exception.Message)"
Stop-Transcript
exit 1
}

# Name of the replicated VM
$vmName = "vbr-test_luanntd-pfsense-management-Windows-10-DC_replica"

# Retrieve VM object
Write-Host "Retrieving VM $vmName..."
$vm = Get-VM -Name $vmName

# Power on the VM if it is not already powered on
if ($vm.PowerState -ne "PoweredOn") {
Write-Host "Powering on VM $vmName..."
try {
Start-VM -VM $vm -Confirm:$false
Write-Host "VM $vmName is now powered on."
} catch {
Write-Host "Failed to power on VM ${vmName}: $_"
Stop-Transcript
exit 1
}

# Wait a moment for the VM to complete startup
Write-Host "Waiting for VM to complete startup..."
Start-Sleep -Seconds 60
} else {
Write-Host "VM $vmName is already powered on."
}

# Check VMware Tools status on the VM
$vmToolsStatus = $vm.ExtensionData.Guest.ToolsStatus
Write-Host "VMware Tools status for ${vmName}: ${vmToolsStatus}"

if ($vmToolsStatus -eq "toolsNotInstalled" -or $vmToolsStatus -eq "toolsOld") {
Write-Host "VMware Tools is not installed or outdated on $vmName. Initiating installation..."
Install-VMHostPatch -VMHost $vm -Baseline $vmToolsStatus
Write-Host "VMware Tools installation initiated."
} else {
Write-Host "VMware Tools is already installed and up-to-date on $vmName."
}

# Enter credentials into the VM
$password = ConvertTo-SecureString "Luanntd@@123" -AsPlainText -Force
$guestCredential = New-Object System.Management.Automation.PSCredential("Administrator", $password)

# Connect to the VM via Guest OS and change the IP configuration
Write-Host "Connecting to VM $vmName to change IP configuration..."
Invoke-VMScript -VM $vm -ScriptText {
Write-Host "Changing IP configuration..."
netsh interface ip set address "Ethernet0 2" dhcp
netsh interface ip delete dns "Ethernet0 2" all

netsh interface ip set address "Ethernet0 2" static 192.168.19.127 255.255.255.0 192.168.19.10
netsh interface ip set dns "Ethernet0 2" static 8.8.8.8
netsh interface ip add dns "Ethernet0 2" 8.8.4.4 index=2
} -GuestCredential $guestCredential -Confirm:$false

if ($?) {
Write-Host "IP configuration on $vmName updated successfully."
} else {
Write-Host "Failed to update IP configuration on $vmName."
Stop-Transcript
exit 1
}

# Shutdown the VM after changing IP (If necessary)
Write-Host "Stopping VM $vmName..."
Stop-VM -VM $vm -Confirm:$false

if ($?) {
Write-Host "VM $vmName stopped successfully."
} else {
Write-Host "Failed to stop VM $vmName."
}

# Stop logging
Stop-Transcript


Thanks in advance
david.domask
Veeam Software
Posts: 2590
Liked: 606 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Post-Script in Replication job

Post by david.domask »

Hi Luan, welcone to the forums.

First, just a question, have you worked with your Service Provider about reviewing your failover plans? With the Network Extension Appliance ( NEA ), most failover activities should be able to be handled on the networking side, so if you have not discussed this with your provider yet, I recommend do so and discuss a configuration for this.

As for your script, a few things:

1. Always check for $NULL with $NULL on the left side of the operator. The Microsoft article explains why in more detail, but this can lead to incorrect results. It's possible your $NULL checks are false-positives because of this.

2. Can you run the line that fetches the credentials manually with the same string you pass to the -Name parameter and check if multiple credentials are being returned?

3. Keep in mind, pre and post job scripts execute under the account used as a Service Account for the Veeam Backup Service (See the info box). It's possible that the script when running as system is not able to connect to Veeam correctly, and likely you need to check this. The tool PSEXEC can be run with the -i and -s flags to open an interactive session as System -- try your script there and check if it's getting the data from the Veeam cmdlets correctly.

4. You may need to add Connect-VBRServer and pass the local Windows credentials (if they can be used to access the Veeam Backup and Replication server), but I think first test your script under System as mentioned above.

Start with this, but I do recommend discuss with your Provider on your Replication plans, it's possible you can avoid this depending on the configuration you and your Provider end up with.
David Domask | Product Management: Principal Analyst
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests