PowerShell script exchange
Post Reply
yupei
Lurker
Posts: 1
Liked: never
Joined: Apr 23, 2021 9:30 am
Full Name: Pei Yu
Contact:

how to use powershell to switch credential which used in protected group

Post by yupei »

how to use power shell to switch credential which used in protected group.
you can see my attachment pic.
which i want to switch the credentials via GUI.
But how to do it in PowerShell way?
Image
oleg.feoktistov
Veeam Software
Posts: 2010
Liked: 670 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: how to use powershell to switch credential which used in protected group

Post by oleg.feoktistov »

Hi Pel Yu and welcome to the R&D forums!

Here is the sample code, which takes into account multiple hosts inside a protection group and changes credentials record only for the selected one. The workflow is explained in comments. Also, please note that you will still need to change sample names used:

Code: Select all

# Get credentials you want to assign to the computer inside a protection group
$creds = Get-VBRCredentials -Name 'CredsName'

# Get target protection group and a container of hosts inside it
$pg = Get-VBRProtectionGroup -Name 'ProtectionGroup' 
$container = $pg.Container

# Create empty array for custom credentials
$customCredsArray = @()

# Compose new custom credentials object for the host you want to assign these creds to and add this object to custom creds array created earlier
$customCredsTarget = New-VBRIndividualComputerCustomCredentials -HostName 'HostName' -Credentials $creds
$customCredsArray += $customCredsTarget

# Loop through existing container and add all other records, which you want to leave intact, to the custom creds array
foreach ($customCreds in $container.CustomCredentials)
{
  if ($customCreds.HostName -eq $customCredsTarget.HostName) {
   continue
  }
  else {
    $customCredsArray += $customCreds
  }
}

# Assign new array of custom credentials to the existing container
$newContainer = Set-VBRIndividualComputerContainer -Container $container -CustomCredentials $customCredsArray

# Update the existing protection group with the changed container
Set-VBRProtectionGroup -ProtectionGroup $pg -Container $newContainer 
Let me know if it helps.
Oleg
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests