Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test chocolatey package install on a vm. Resets the vm to last clean state and runs the package pointing to a local path that has the .nupkg file
#uses a Hyper-v vm whose name is the same as its computername
#Assumes chocolatey is installed and powershell remoting is configured
#on vm and client as well as cssp auth
#configure remoting and cssp on vm using
# Enable-PSRemoting -Force
# Enable-WSManCredSSP –Role Server
#Enable on client with
# Enable-PSRemoting -Force
# Enable-WSManCredSSP –Role client –DelegateComputer $VmName
#On clienr edit group policy in gpedit.msc Computer Configuration –> Administrative Templates –> System –> Credential Delegation
#select "Allow Delegating Fresh Credentials with NTLM-only Server Authentication"
#Enable the setting, click "Show..." and enter vm computrname as "wsman/<computername>
#See https://rkeithhill.wordpress.com/2009/05/02/powershell-v2-remoting-on-workgroup-joined-computers-%E2%80%93-yes-it-can-be-done/
function Test-VM {
param(
$vmName,
$sourcePath,
$package
)
$vm = Get-VM $VmName
Restore-VMSnapshot $vm -Name $vm.ParentSnapshotName -Confirm:$false
Start-VM $VmName
$creds = Get-Credential -Message "$vmName credentials" -UserName "$env:UserDomain\$env:username"
$me=$env:computername
$expandedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($sourcePath)
$remoteDir = $expandedPath.replace(':','$')
$source="\\$me\$remoteDir"
Write-Host "Waiting for $vmName to start..."
do {Start-Sleep -milliseconds 100}
until ((Get-VMIntegrationService $vm | ?{$_.name -eq "Heartbeat"}).PrimaryStatusDescription -eq "OK")
Write-Host "Using source path $source"
Invoke-Command -ComputerName $vmName -Credential $creds -Authentication Credssp -ScriptBlock {cinst $args[0] -source $args[1]} -Argumentlist $package,$source
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.