Search code examples
powershellpowercli

Invoke-VMScript PowerCLI 6.5 Call Script with Parameters


I am currently developing a Script that should be able to attach a certain Windows share as a network drive with an drive letter in a host system. As the Windows share is dynamic (could change nearly all the time the script get's invoked.) I need to pass this as an parameter to the script running on the VM.

What I currently have.

In my script calling the script stored on my machine.

MainScript.ps1 contains.

$MyVMName = "somevmname"
$Script = "C:\path\to\script\somescript.ps1 -Path \\some\shared\folder"
Invoke-VMScript -VM $MyVMName -ScriptText $Script -ScriptType PowerShell -GuestCredential $MyGuestCredential

somescript.ps1 contains.

Param(
      [Parameter(Mandatory=$true)]
      $Path,
      [Parameter(Mandatory=$true)]
      $Credentials
)

New-PSDrive -Name "H" -PSProvider FileSystem -Root $Path -Credential $Credentials 

Every time I'm executing my MainScript.ps1 it will most likely freeze when the Invoke-VMScript command is been executed. Is there another way to pass the parameters to the script?

The documentation found under

https://pubs.vmware.com/vsphere-65/index.jsp#com.vmware.powercli.cmdletref.doc/Invoke-VMScript.html

does not reveal too much using parameters for the invoked script.


Solution

  • You could try to create a new PowerShell running the script:

    $MyVMName = "somevmname"
    Invoke-VMScript -VM $MyVMName -ScriptText "powershell.exe -file C:\path\to\script\somescript.ps1 -Path \\some\shared\folder" -ScriptType PowerShell -GuestCredential $MyGuestCredential