Skip to content

Invoke-HyperVVMCommand

Hyper-V: Executes a command on a virtual machine via PowerShell Direct

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    [string]$VMName,

    [Parameter(Mandatory = $true)]
    [PSCredential]$Credential,

    [scriptblock]$ScriptBlock = { $env:COMPUTERNAME }
)

Process {
    try {
        $session = New-PSSession -VMName $VMName -Credential $Credential -ErrorAction Stop
        
        $output = Invoke-Command -Session $session -ScriptBlock $ScriptBlock -ErrorAction Stop

        $result = [PSCustomObject]@{
            VMName    = $VMName
            Output    = $output
            Action    = "CommandExecutedViaPSDirect"
            Status    = "Success"
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
    finally {
        if ($session) {
            Remove-PSSession -Session $session
        }
    }
}

Specifies the name of the virtual machine.

Specifies the credentials for the guest operating system.

Optional. Specifies the command(s) to execute on the VM. Defaults to retrieving the guest hostname.

An interactive directory of PowerShell scripts.