From a5a806ba3a5c2a7642489eec3fb2c617971a34de Mon Sep 17 00:00:00 2001 From: Rui Lopes Date: Mon, 3 Oct 2022 12:54:43 +0100 Subject: [PATCH] fix the windows hyperv smb synced folder creation/destruction closes #12639 --- plugins/hosts/windows/cap/smb.rb | 11 ++----- plugins/hosts/windows/scripts/set_share.ps1 | 29 +++++++++---------- plugins/hosts/windows/scripts/unset_share.ps1 | 12 ++++---- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/plugins/hosts/windows/cap/smb.rb b/plugins/hosts/windows/cap/smb.rb index 02e98da337b..6cb5b3e5ec0 100644 --- a/plugins/hosts/windows/cap/smb.rb +++ b/plugins/hosts/windows/cap/smb.rb @@ -52,9 +52,8 @@ def self.smb_cleanup(env, machine, opts) if prune_shares.size > 0 machine.env.ui.warn("\n" + I18n.t("vagrant_sf_smb.uac.prune_warning") + "\n") - sleep UAC_PROMPT_WAIT @@logger.info("remove shares: #{prune_shares}") - result = Vagrant::Util::PowerShell.execute(script_path, *prune_shares, sudo: true) + result = Vagrant::Util::PowerShell.execute(script_path, *prune_shares, sudo: false) if result.exit_code != 0 failed_name = result.stdout.to_s.sub("share name: ", "") raise SyncedFolderSMB::Errors::PruneShareFailed, @@ -100,14 +99,8 @@ def self.smb_prepare(env, machine, folders, opts) ] end if !shares.empty? - uac_notified = false shares.each_slice(10) do |s_shares| - if !uac_notified - machine.env.ui.warn("\n" + I18n.t("vagrant_sf_smb.uac.create_warning") + "\n") - uac_notified = true - sleep(UAC_PROMPT_WAIT) - end - result = Vagrant::Util::PowerShell.execute(script_path, *s_shares, sudo: true) + result = Vagrant::Util::PowerShell.execute(script_path, *s_shares, sudo: false) if result.exit_code != 0 share_path = result.stdout.to_s.sub("share path: ", "") raise SyncedFolderSMB::Errors::DefineShareFailed, diff --git a/plugins/hosts/windows/scripts/set_share.ps1 b/plugins/hosts/windows/scripts/set_share.ps1 index b483088af82..e3763928922 100644 --- a/plugins/hosts/windows/scripts/set_share.ps1 +++ b/plugins/hosts/windows/scripts/set_share.ps1 @@ -1,37 +1,36 @@ +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + # The names of the user are language dependent! $objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-1-0") $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) -$grant = "$objUser,Full" - -for ($i=0; $i -le $args.length; $i = $i + 3) { +for ($i = 0; ($i+2) -lt $args.length; $i = $i + 3) { $path = $args[$i] $share_name = $args[$i+1] $share_id = $args[$i+2] - - if ($path -eq $null) { - Write-Warning "empty path argument encountered - complete" - exit 0 + if (!$path) { + Write-Error "error - no share path provided" + exit 1 } - if ($share_name -eq $null) { + if (!$share_name) { Write-Output "share path: ${path}" Write-Error "error - no share name provided" exit 1 } - if ($share_id -eq $null) { + if (!$share_id) { Write-Output "share path: ${path}" Write-Error "error - no share ID provided" exit 1 } - $result = net share $share_id=$path /unlimited /GRANT:$grant /REMARK:"${share_name}" - if ($LastExitCode -ne 0) { - $host.ui.WriteLine("share path: ${path}") - $host.ui.WriteErrorLine("error ${result}") - exit 1 - } + New-SmbShare ` + -Name $share_id ` + -Path $path ` + -FullAccess $objUser ` + -Description $share_name } exit 0 diff --git a/plugins/hosts/windows/scripts/unset_share.ps1 b/plugins/hosts/windows/scripts/unset_share.ps1 index 69dad89d12c..6da7dcdc3d5 100644 --- a/plugins/hosts/windows/scripts/unset_share.ps1 +++ b/plugins/hosts/windows/scripts/unset_share.ps1 @@ -1,10 +1,10 @@ +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + ForEach ($share_name in $args) { - $result = net share $share_name /DELETE /YES - if ($LastExitCode -ne 0) { - Write-Output "share name: ${share_name}" - Write-Error "error - ${result}" - exit 1 - } + Remove-SmbShare ` + -Name $share_name ` + -Force } Write-Output "share removal completed" exit 0