VMware Cloud Community
jakemike
Contributor
Contributor
Jump to solution

Uploading VMDK to datastore within scriptblock produces odd result

Hi guys,

I'm trying to upload a number of VMDKs from my local C drive over to a datastore. I'm using the "copy-datastoreitem" command and because it cannot be run asynchronously I'm placing the code in a job.

However, I am getting an odd result.

If I run the below command outside a job, it runs perfectly and works as expected. The file gets uploaded to the test folder in the datastore, all good.

try {

$ErrorActionPreference = "Stop";

$logfile = "C:\temp\logfile.log"

Add-PSSnapin "VMware.VimAutomation.Core" -ErrorAction Continue

Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false

connect-viserver "vcenter" -user "lab\vadmin" -password "Password123" -ErrorAction stop

$ds = Get-VMHost -Name "192.168.1.243" | Get-Datastore "datastore1 (2)"

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\"

Copy-DatastoreItem -Item "c:\test\VM01\vm01.vmdk" -Destination ds:\Test\

}

catch {

$errormessage = $_.Exception.Message

$logdate = Get-Date; Add-Content $logfile "$logdate $errormessage"

}

But, if I place it in a scriptblock, I get a weird result. Firstly I get an error: VimDatastore The specified location '\vcenter@443\DC1\datastore1 (2)\Test\VM01.vmdk' does not exist.

The other thing is, a new folder gets created in the datastore called "2fTest" and the VM01.VMDK file appears in there. What the??? (see screen grab below).

Start-Job -name test1 -ScriptBlock {

try {

$ErrorActionPreference = "Stop";

$logfile = "C:\temp\logfile.log"

Add-PSSnapin "VMware.VimAutomation.Core" -ErrorAction Continue

Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false

connect-viserver "vcenter" -user "lab\vadmin" -password "Password123" -ErrorAction stop

$ds = Get-VMHost -Name "192.168.1.243" | Get-Datastore "datastore1 (2)"

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\"

Copy-DatastoreItem -Item "c:\test\VM01\vm01.vmdk" -Destination ds:\Test\

}

catch {

$errormessage = $_.Exception.Message

$logdate = Get-Date; Add-Content $logfile "$logdate $errormessage"

}

}

2019-05-20 10_18_45-vSphere Web Client - Internet Explorer.png

I am at a complete loss as to why it behaves this way.

Any ideas/suggestions/help would be much appreciated.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That doesn't look like verbose output (the line should start with 'VERBOSE').
Looks like there is an error before the copy actually starts.

I can't seem to be able to replicate the error you are getting.
I would advise to first upgrade your PowerCLI version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

The value 0x2F is the ASCII code for a '/' character.
That gives you a hint that the problem might be with the destination name.

Try replacing that line with

   Copy-DatastoreItem -Item "c:\test\VM01\vm01.vmdk" -Destination 'ds:/Test'


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jakemike
Contributor
Contributor
Jump to solution

Hey LucD, thanks for the reply.

I changed it as per your suggestion, but got exactly the same result.

It created a new 2fTest folder with the vmdk in it.

I dont understand why it works outside the scriptblock!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What output does the job return?
Did you do a Receive-Job when it is completed?

What PowerCLI version are you using?
Are you using a very old version (since you still seem to have an Add-PSSnapin in the script)?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jakemike
Contributor
Contributor
Jump to solution

Receive job shows the below.

And yes, I am using a very old version. I will update and see if it helps.

RunspaceId    : a0d763ce-d243-4d39-b3e0-02337009da08

IsConnected   : True

Id            : /VIServer=lab\vadmin@vcenter:443/

ServiceUri    : https://vcenter/sdk

SessionSecret : 73cb313baa21e9cd76eb46becf984950c2c12c49

Name          : vcenter

Port          : 443

SessionId     : 73cb313baa21e9cd76eb46becf984950c2c12c49

User          : lab\vadmin

Uid           : /VIServer=lab\vadmin@vcenter:443/

Version       : 6.5

Build         : 13638625

ProductLine   : vpx

InstanceUuid  : 9697de2e-876b-4f38-9268-53f24d2aa458

RefCount      : 1

ExtensionData : VMware.Vim.ServiceInstance

Client        : VMware.VimAutomation.ViCore.Impl.V1.VimClient

Used            :

Free            :

RunspaceId      : a0d763ce-d243-4d39-b3e0-02337009da08

CurrentLocation :

Name            : DS

Provider        : VMware.VimAutomation.Core\VimDatastore

Root            : \vcenter@443\DC1\datastore1 (2)

Description     :

MaximumSize     :

Credential      : System.Management.Automation.PSCredential

DisplayRoot     :

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The output from the Connect-VIServer and the New-PSDrive cmdlets looks ok.

Can you add the Verbose switch to the cmdlet (and check what the job output shows)?
And make sure you are using single quotes?

   Copy-DatastoreItem -Item 'c:\test\VM01\vm01.vmdk' -Destination 'ds:/Test' -Verbose


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jakemike
Contributor
Contributor
Jump to solution

OK, now it's failing even outside the scriptblock with the same result, which is something I guess...

Below is the verbose result.

Copy-DatastoreItem : 20/05/2019 6:59:16 PM VimDatastore The specified location '\vcenter@443\DC1\datastore1 (2)\Test\VM01.vmdk' does not exist.

At C:\testing\upload testing.ps1:18 char:1

+ Copy-DatastoreItem -Item 'c:\test\VM01\vm01.vmdk' -Destination 'ds:/Test' ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Copy-DatastoreItem], PathException

    + FullyQualifiedErrorId : Core_VmStoreProviderCache_GetItem_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That doesn't look like verbose output (the line should start with 'VERBOSE').
Looks like there is an error before the copy actually starts.

I can't seem to be able to replicate the error you are getting.
I would advise to first upgrade your PowerCLI version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jakemike
Contributor
Contributor
Jump to solution

As usual you are right.

A simple upgrade of PowerCLI fixed me all up.

Thanks for your help, always appreciate it.

0 Kudos