2

I need to dynamically build a string parameter using output from a pipe which is then passed to another command.

The source command is Get-VM which has an element called Name

The destination command is Move-VM, which accepts a parameter of -DestinationStoragePath

I need to dynamically manipulate this path based on the source Name to be D:\{0} where {0} is the VM Name.

I have this so far:

Get-VM | Move-VM -DestinationStoragePath [string]::Format("D:\{0}",$_.Name)

But it is throwing an exception, if i statically set the DestinationStoragePath parameter, then it works fine, so its just this little bit that is tripping it up.

Any ideas?

1 Answer 1

7
Get-VM | Move-VM -DestinationStoragePath [string]::Format("D:{0}",$_.Name)

is trying to pass the string [string]::Format("D:{0}",$_.Name) literally to the parameter -DestinationStoragePath.

What you need is execute the expression and return the result by surrounding your expression in parenthesis like this:

Get-VM | % { Move-VM -DestinationStoragePath ([string]::Format("D:{0}",$_.Name)) }
Sign up to request clarification or add additional context in comments.

3 Comments

was so close! This has fixed the problem, but i am now getting another issue with $_.Name being null. Has it been piped corrected from Get-VM?
I'm not awake. Updated in answer. I forgot the Foreach-object.
This works, but the -DestinationStoragePath parameter could be as simple as "D:$($_.Name)". Is there something that [string]::Format() does that I am missing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.