Public/Set-MovePlanVmCredential.ps1

Function Set-MovePlanVmCredential {
    <#
    .SYNOPSIS
        Set the Credential for a Move Plan VM
    .DESCRIPTION
 
    .NOTES
 
    .LINK
        Specify a URI to a help page, this will show when Get-Help -Online is used.
    .EXAMPLE
        Get-MoveExampleSomething -Verbose
        Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
    #>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false,
            HelpMessage = 'Move Session Name')][String]$MoveSession = 'Default',

        [Parameter(Mandatory = $true,
            HelpMessage = 'MovePlan ID')][String]$MovePlanId,

        [Parameter(Mandatory = $true,
            HelpMessage = 'VM UUID')][String]$VmUUID,

        [Parameter(Mandatory = $true,
            HelpMessage = 'Credential for the VM')][pscredential]$Credential,

        [Parameter(Mandatory = $false,
            HelpMessage = 'GuestPrepMode')][String]$GuestPrepMode = 'auto',

        [Parameter(HelpMessage = 'Is this a windows device')][switch]$Windows,

        [Parameter()][switch]$PassThru
    )

    ## Get Session Configuration
    $MoveSessionConfig = $global:MoveSessions[$MoveSession]
    if (-not $MoveSessionConfig) {
        Write-Host 'MoveSession: [' -NoNewline
        Write-Host $MoveSession -ForegroundColor Cyan
        Write-Host '] was not Found. Please use the New-MoveSession command.'
        Throw 'Move Session Not Found. Use New-MoveSession command before using features.'
    }

    #Honor SSL Settings
    if ($MoveSessionConfig.AllowInsecureSSL) {
        $MoveCertSettings = @{SkipCertificateCheck = $true }
    } else {
        $MoveCertSettings = @{SkipCertificateCheck = $false }
    }

    $Body = @{
        Spec = @{
            CommonCredentials = @{
                LinuxPassword   = ''
                LinuxUserName   = ''
                WindowsPassword = ''
                WindowsUserName = ''
                PemFile         = ''
            }
            Region            = ''
            VMs               = @(
                @{
                    UserName      = ''
                    Password      = ''
                    UUID          = $VmUUID
                    GuestPrepMode = $GuestPrepMode
                }
            )
        }
    }

    if ($Windows.IsPresent) {
        $Body.Spec.CommonCredentials.WindowsUserName = $Credential.UserName
        $Body.Spec.CommonCredentials.WindowsPassword = $Credential.GetNetworkCredential().Password
    } else {
        $Body.Spec.CommonCredentials.LinuxUserName = $Credential.UserName
        $Body.Spec.CommonCredentials.LinuxPassword = $Credential.GetNetworkCredential().Password
    }

    $RestMethodSplat = @{
        Uri            = 'https://{0}:{1}/move/v2/plans/{2}/prepare' -f $MoveSessionConfig.MoveServer, $MoveSessionConfig.MovePort, $MovePlanId
        TimeoutSec     = 100
        Method         = 'POST'
        WebSession     = $MoveSessionConfig.MoveWebSession
        ProgressAction = 'SilentlyContinue'
        Body           = $Body | ConvertTo-Json -Depth 10 -Compress
    }
    try {
        $Result = Invoke-RestMethod @RestMethodSplat @MoveCertSettings

    } catch {
        throw $_.Exception.Message
    }
    if($PassThru.IsPresent){
        return $Result
    }
}