Skip to content

Instantly share code, notes, and snippets.

@minanagehsalalma
Created April 12, 2026 15:03
Show Gist options
  • Select an option

  • Save minanagehsalalma/fafa98998fc922420e397f768332b38d to your computer and use it in GitHub Desktop.

Select an option

Save minanagehsalalma/fafa98998fc922420e397f768332b38d to your computer and use it in GitHub Desktop.
Invoke-VMGuest — PowerShell helper to run bash commands inside a VMware Workstation guest and capture output on the host (works around vmrun's missing stdout pipe).
# Invoke-VMGuest.ps1
#
# PowerShell helper to run bash commands inside a VMware Workstation guest
# and capture their output on the host.
#
# vmrun's runScriptInGuest silently discards stdout — this works around that
# by redirecting output to a temp file in the guest, then pulling it back with
# CopyFileFromGuestToHost.
#
# Requirements:
# - VMware Workstation with vmrun.exe
# - VMware Tools installed and running in the guest
# - A guest user account with write access to /tmp
# ── Configuration ─────────────────────────────────────────────────────────────
$vmrun = 'C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe'
$vmx = 'C:\path\to\your-vm.vmx' # ← path to your .vmx file
$guestUser = 'your-guest-username'
$guestPass = 'your-guest-password'
$guestTmpFile = '/tmp/invoke-vmguest-out.txt'
$hostTmpFile = "$env:TEMP\invoke-vmguest-out.txt"
# ── Helper ────────────────────────────────────────────────────────────────────
function Invoke-VMGuest {
<#
.SYNOPSIS
Runs a bash command inside a VMware Workstation Linux guest and returns
its stdout/stderr to the PowerShell host.
.PARAMETER Cmd
The bash command string to run inside the guest.
.EXAMPLE
Invoke-VMGuest 'df -h /'
.EXAMPLE
Invoke-VMGuest 'ps aux | grep vmware'
.EXAMPLE
Invoke-VMGuest 'sudo apt-get autoremove -y'
.NOTES
vmrun's runScriptInGuest does not return output to the host.
This function works around that limitation by:
1. Redirecting command output to a temp file in the guest
2. Copying that file to the host with CopyFileFromGuestToHost
3. Reading and returning the file contents
#>
param(
[Parameter(Mandatory, Position = 0)]
[string]$Cmd
)
$vmrunArgs = @('-T', 'ws', '-gu', $guestUser, '-gp', $guestPass)
& $vmrun @vmrunArgs runScriptInGuest $vmx '/bin/bash' "$Cmd > $guestTmpFile 2>&1"
& $vmrun @vmrunArgs CopyFileFromGuestToHost $vmx $guestTmpFile $hostTmpFile
if (Test-Path $hostTmpFile) {
Get-Content $hostTmpFile
} else {
Write-Warning "No output file received. The command may have failed or VMware Tools is busy."
}
}
# ── Examples ──────────────────────────────────────────────────────────────────
# Invoke-VMGuest 'df -h /'
# Invoke-VMGuest 'free -h'
# Invoke-VMGuest 'uptime'
# Invoke-VMGuest 'ls -la /tmp'
# Invoke-VMGuest 'du -sh /var/* 2>/dev/null | sort -rh | head -20'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment