Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run Export-RubrikVM - Illegal request in Invoke-RubrikWebRequest.ps1 line 18 #627

Closed
kamfaima opened this issue May 17, 2020 · 6 comments · Fixed by #630
Closed

Comments

@kamfaima
Copy link

Current Behavior:
Trying to use Powershell to export a VM snapshot to a host and datastore from https://rubrik.gitbook.io/rubrik-sdk-for-powershell/command-documentation/reference/export-rubrikvm#example-2

Note that the original single line command has been broken up into several lines for readability.

PS /Users/kfm>
$vmToExport = Get-RubrikVM $customer_VM -PrimaryClusterID local
$latestSnapshot = Get-RubrikSnapshot -id $vmToExport.id -Latest
Export-RubrikVM -id $latestSnapshot -HostId (Get-RubrikVMwareHost -name $rubrikVMwareHost -PrimaryClusterID local).id -DatastoreId (Get-RubrikVMwareDatastore -name $rubrikVMwareDatastore).id -verbose

VERBOSE: Validate the Rubrik token exists
VERBOSE: Found a Rubrik token for authentication
VERBOSE: Gather API Data for Export-RubrikVM
VERBOSE: Selected 1.0 API Data for Export-RubrikVM                                                                                                                                                                                                                   
VERBOSE: Load API data for Export-RubrikVM
VERBOSE: Description: Export a VMware VM to an existing vSphere environment
VERBOSE: Build the URI
VERBOSE: URI = https://RubrikClusterIP/api/v1/vmware/vm/snapshot/@{date=05/17/2020 08:03:08; indexState=1; slaName=SLAGOLD; vmName=customer_VM; slaId=ecbb9e99-2d0a-4b9f-b4eb-93464f2064e6; replicationLocationIds=System.Object[]; archivalLocationIds=System.Object[]; isOnDemandSnapshot=False; cloudState=0; id=90fd8c40-da42-4e24-b5bf-cfd0c242e575; consistencyLevel=VSS_CONSISTENT; isRetainedByRetentionLockSla=False}/export
VERBOSE: Build the query parameters for <null>
VERBOSE: URI = https://RubrikClusterIP/api/v1/vmware/vm/snapshot/@{date=05/17/2020 08:03:08; indexState=1; slaName=SLAGOLD; vmName=customer_VM; slaId=ecbb9e99-2d0a-4b9f-b4eb-93464f2064e6; replicationLocationIds=System.Object[]; archivalLocationIds=System.Object[]; isOnDemandSnapshot=False; cloudState=0; id=90fd8c40-da42-4e24-b5bf-cfd0c242e575; consistencyLevel=VSS_CONSISTENT; isRetainedByRetentionLockSla=False}/export
VERBOSE: List of set parameters: [id, @{date=05/17/2020 08:03:08; indexState=1; slaName=SLAGOLD; vmName=customer_VM; slaId=ecbb9e99-2d0a-4b9f-b4eb-93464f2064e6; replicationLocationIds=System.Object[]; archivalLocationIds=System.Object[]; isOnDemandSnapshot=False; cloudState=0; id=90fd8c40-da42-4e24-b5bf-cfd0c242e575; consistencyLevel=VSS_CONSISTENT; isRetainedByRetentionLockSla=False}] [HostID, VmwareHost:::7a669324-8d52-4101-b12a-aa02cbf8b12a-host-336] [DatastoreId, DataStore:::7a669324-8d52-4101-b12a-aa02cbf8b12a-datastore-932] [Verbose, True]
VERBOSE: Build the body parameters
VERBOSE: Adding hostId...
VERBOSE: Adding vmName...
VERBOSE: Adding datastoreId...
VERBOSE: Body = {
  "hostId": "VmwareHost:::7a669324-8d52-4101-b12a-aa02cbf8b12a-host-336",
  "datastoreId": "DataStore:::7a669324-8d52-4101-b12a-aa02cbf8b12a-datastore-932"
}
VERBOSE: Submitting the request
VERBOSE: POST https://RubrikClusterIP/api/v1/vmware/vm/snapshot/@{date=05/17/2020 08:03:08; indexState=1; slaName=SLAGOLD; vmName=customer_VM; slaId=ecbb9e99-2d0a-4b9f-b4eb-93464f2064e6; replicationLocationIds=System.Object[]; archivalLocationIds=System.Object[]; isOnDemandSnapshot=False; cloudState=0; id=90fd8c40-da42-4e24-b5bf-cfd0c242e575; consistencyLevel=VSS_CONSISTENT; isRetainedByRetentionLockSla=False}/export with 159-byte payload
VERBOSE: received 64-byte response of content type text/plain
Invoke-WebRequest: /Users/kfm/.local/share/powershell/Modules/Rubrik/5.0.1/Private/Invoke-RubrikWebRequest.ps1:18:19
Line |
  18 |  …   $result = Invoke-WebRequest -UseBasicParsing -SkipCertificateCheck  …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Illegal request-target, unexpected character '[' at position 224

Expected Behavior:
I'd expect Rubrik to being exporting the VM to the desired location. Running the identical command with -whatif seems to validate that the actual command and parameters are correct.

What if: Performing the operation "Export a VMware VM to an existing vSphere environment" on target "@{date=05/17/2020 08:03:08; indexState=1; slaName=SLAGOLD; vmName=customer_VM; slaId=ecbb9e99-2d0a-4b9f-b4eb-93464f2064e6; replicationLocationIds=System.Object[]; archivalLocationIds=System.Object[]; isOnDemandSnapshot=False; cloudState=0; id=90fd8c40-da42-4e24-b5bf-cfd0c242e575; consistencyLevel=VSS_CONSISTENT; isRetainedByRetentionLockSla=False}". 

The problem instead seems to stem from Invoke-RubrikWebRequest.ps1 as per the verbose error message above.

Steps to Reproduce:
Happens every time both on MacOS and Windows 10 (see Context for more information)

Context:

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Rubrik PowerShell Module Version: 5.0.1
  • PowerShell Version: 7
  • Operating System: MacOS Catalina (10.15.4) and Windows 10
@jaapbrasser jaapbrasser self-assigned this May 17, 2020
@jaapbrasser
Copy link
Contributor

Hi Kamfaima,

Thanks for reporting this issue, I'll try to replicate this in our test cluster and give an update here once I've triaged this issue.

@jaapbrasser
Copy link
Contributor

jaapbrasser commented May 17, 2020

Hello Kamfaima,

It looks like you're entering in the entire snapshot object instead of just the id, that's why the url also looks malformed:

VERBOSE: URI = https://RubrikClusterIP/api/v1/vmware/vm/snapshot/@{date=05/17/2020 08:03:08; indexState=1; slaName=SLAGOLD; vmName=customer_VM; slaId=ecbb9e99-2d0a-4b9f-b4eb-93464f2064e6; replicationLocationIds=System.Object[]; archivalLocationIds=System.Object[]; isOnDemandSnapshot=False; cloudState=0; id=90fd8c40-da42-4e24-b5bf-cfd0c242e575; consistencyLevel=VSS_CONSISTENT; isRetainedByRetentionLockSla=False}/export

So using your code this should solve the issue:

$vmToExport = Get-RubrikVM $customer_VM -PrimaryClusterID local
$latestSnapshot = Get-RubrikSnapshot -id $vmToExport.id -Latest | Select-Object -ExpandProperty Id
Export-RubrikVM -id $latestSnapshot -HostId (Get-RubrikVMwareHost -name $rubrikVMwareHost -PrimaryClusterID local).id -DatastoreId (Get-RubrikVMwareDatastore -name $rubrikVMwareDatastore).id -verbose

If you like some other alternative of making your code easier to read you could use splatting as well:

$ExportSplat = @{
    id = (get-rubrikvm jbrasser -PrimaryClusterID local -DetailedObject).snapshots[-1]
    HostId = (Get-RubrikVMwareHost -name 'rubrikesxihost' -PrimaryClusterID 'local').id
    DatastoreId = (Get-RubrikVMwareDatastore -name 'vSAN').id
    Verbose = $true
}

Export-RubrikVM @ExportSplat

Or if you feel like overengineering your splatting you can also splat inside of your initial splatting hashtable:

$ExportSplat = @{
    id = & {
            $VMSplat = @{
                Name = 'jbrasser-win'
                PrimaryClusterID = 'local'
                DetailedObject = $true
            }
            Get-RubrikVM @VMSplat |
                Select-Object -ExpandProperty Id
    }
    
    HostId = & {
            $HostSplat = @{
                name = 'esxi34.rubrik.us'
                PrimaryClusterID = 'local'
            }   
            Get-RubrikVMwareHost @HostSplat |
                Select-Object -ExpandProperty Id
    }

    DatastoreId = Get-RubrikVMwareDatastore |
        Select-Object -ExpandProperty Id 

    Verbose = $true
}

Export-RubrikVM @ExportSplat

Let me know if that works for you!

@kamfaima
Copy link
Author

Hi @jaapbrasser - I can confirm that passing the id works, thanks for spotting that user error - doh!

Does there need to be better error parsing/catching within the Export-VM before passing it to the Invoke-WebRequest? Otherwise it's misleading where the error is actually coming from.

@jaapbrasser
Copy link
Contributor

I think that is a fair point, I can't imagine any scenario in which we would like to parse an object or hashtable as a id parameter.

How does this look:
image

@kamfaima
Copy link
Author

That looks good - thanks for incorporating my idea as a fix!

@jaapbrasser
Copy link
Contributor

No problem at all, I marked it as ready for review so your idea will be merged into the repo over the coming days.

mwpreston added a commit that referenced this issue May 27, 2020
Export-RubrikVM - Illegal request in Invoke-RubrikWebRequest - Fix #627
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment