Ok, let's do this step by step.
I changed the code to this simplified form.
And I execute the script under an account that has access in the OS running on MyVM. This to avoid all complications due to the credentials.
param( [string]$port)$scriptblock="
`$url=`"http://localhost:$port/`" write-host`$url
"
Invoke-VMScript-VMMyVM-ScriptText$ScriptBlock
When I execute the script (which I saved as Invoke-VMScript-Parameters.ps1) I get the following output
PS C:\Scripts> . 'C:\Scripts\Invoke-VMScript-Parameters.ps1' -Port 124
ScriptOutput
-----------------------------------------------------------------------------------------------
http://localhost:124/
|
-----------------------------------------------------------------------------------------------
Can you try this version and see if you get the same result ?
If not, can you include the messages you get, and also the PowerCLI version you are using ?