OpsaC - Operating as PowerShell code
published: October 13, 2021 author: Tinu tags: PowerCLI - VMware categories: Virtualisierung
Mit PowerCLI Invoke-VMScript kann ein Command oder ein Script auf einer VM ausgeführt und das Resultat zurück gegeben werden.
#region Guest-VM
$guestvm = 'test123'
$guestaccount = 'localadmin'
$guestpassword = Read-Host -AsSecureString -Prompt "Enter the password for $($guestaccount)"
$guestcred = New-Object System.Management.Automation.PSCredential ($guestpassword, $guestpassword)
#endregion
$command = 'Get-ChildItem C:\Temp'
if(Get-VM -Name $guestvm) {
$VMScriptRet = Invoke-VMScript -VM $guestvm - Credential $guesrcred -ScriptText $command
$VMScriptRet
}
Ein Script wird als String (Text) auf die Guest VM übertragen und dort ausgeführt. Das Resultat kommt ebenfalls als String (Text) zurück. Will man ein Object zurückgeben, so kann man dieses in einen JSON-String und dann wieder in ein Object zurück konvertieren.
#region Guest-VM
$guestvm = 'test123'
$guestaccount = 'localadmin'
$guestpassword = Read-Host -AsSecureString -Prompt "Enter the password for $($guestaccount)"
$guestcred = New-Object System.Management.Automation.PSCredential ($guestpassword, $guestpassword)
#endregion
$Scriptblock = @'
Get-Process | ConvertTo-Json -Depth 1 -WarningAction SilentlyContinue
'@
if(Get-VM -Name $guestvm) {
$VMScriptRet = Invoke-VMScript -VM $guestvm - Credential $guesrcred -ScriptText $Scriptblock -ScriptType PowerShell
if($VMScriptRet){
$VMScriptRet.Output | ConvertFrom-Json
}
}
Script auf Guest VM kopieren.
#region Guest-VM
$guestvm = 'test123'
$guestaccount = 'localadmin'
$guestpassword = Read-Host -AsSecureString -Prompt "Enter the password for $($guestaccount)"
$guestcred = New-Object System.Management.Automation.PSCredential ($guestpassword, $guestpassword)
#endregion
$SourceScript = 'D:\Scripts\Get-AllProcess.ps1'
$TargetPath = 'C:\Temp\'
if(Get-VM -Name $guestvm) {
Copy-VMGuestFile -Source $SourceScript -Destination $TargetPath -VM $guestvm -LocalToGuest -Credential $guesrcred
}
Ein Script kann ebenfalls mit Invoke-VMScript ausgeführt werden, dazu wird dem Parameter ScriptText anstelle eines Commands oder ScriptBlocks der Pfad zu dem (lokal vorhandenen) Script mitgegeben.
#region Guest-VM
$guestvm = 'test123
$guestaccount = 'localadmin'
$guestpassword = Read-Host -AsSecureString -Prompt "Enter the password for $($guestaccount)"
$guestcred = New-Object System.Management.Automation.PSCredential ($guestpassword, $guestpassword)
#endregion
$ScriptFullName = 'C:\Temp\Get-AllProcess.ps1'
if(Get-VM -Name $guestvm) {
$VMScriptRet = Invoke-VMScript -VM $guestvm -Credential $guesrcred -ScriptText $ScriptFullName -ScriptType PowerShell
if($VMScriptRet){
$VMScriptRet.Output | ConvertFrom-Json
}
}
#region Guest-VM
$guestvm = 'test123
$guestaccount = 'localadmin'
$guestpassword = Read-Host -AsSecureString -Prompt "Enter the password for $($guestaccount)"
$guestcred = New-Object System.Management.Automation.PSCredential ($guestpassword, $guestpassword)
#endregion
$ScriptText = "Start-Process powershell -Credential $guestcred -ArgumentList '-noprofile -noninteractive -command get-service'"
if(Get-VM -Name $guestvm) {
$VMScriptRet = Invoke-VMScript -VM $guestvm - Credential $guesrcred -ScriptText $ScriptText -ScriptType PowerShell
if($VMScriptRet){
$VMScriptRet.Output
}
}