Freigeben über


Sending a script file into the VM using PowerShell Direct

Okay, PowerShell Direct is cool - but sometimes you just want to copy a script (or set of scripts) into the virtual machine and run them there.  How can you do that?  Fortunately - PowerShell Direct comes to the rescue.  Here is a simple snippet that will send any text file from the host to the virtual machine over PowerShell Direct:

# Pass in script file - array trick from https://powershell.com/cs/forums/t/4169.aspx
function copyTextFileIntoVM([string]$VMName, $cred, [string]$sourceFilePath, [string]$destinationFilePath){
   $content = Get-Content $sourceFilePath
   icm -VMName $VMName -Credential $cred {param($Script, $file)
         $script | set-content $file} -ArgumentList (,$content), $destinationFilePath}

Once this is done - you can then use PowerShell Direct to kick off any processes you want.

Cheers,
Ben

Comments

  • Anonymous
    July 29, 2015
    The Using scope modifier will make your code a little simpler: icm -VMName $VMName  -Credential $cred { $using:content | set-content $using:destinationFilePath }

  • Anonymous
    July 29, 2015
    Ths is allright for small files but it would be great if you supported making psSessions with powershell direct so we could use the -toSession /-fromSession flags for copy item :)

  • Anonymous
    July 30, 2015
    Wow, this is cool! I know Linux is not supported but think about Powershell Direct and a provider/converter which takes commands from invoke-command and spits them out in a SSH session on the Linux guest...