Bonjour,

Pour commencer mes connaissances en PowerShell sont très limités.

J'ai un script qui fait un swap automatique de disque USB avec remise en place du partage.
Lorsque j’exécute ce script en admin tout vas bien. Mais lorsque je le lance avec un simple utilisateur il me dit "Access denied".
Il but sur le "Put()":

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
  $UsbFounded.DriveLetter = $null  
  $UsbFounded.Put()  | Out-Null
  $UsbFounded.Dismount($false, $false) | Out-Null
Où $UsbFounded est de type : Win32_Volume

Est-ce que quelqu'un à une idée pour résoudre ce problème ?

Merci de votre réponse rapide.

Voici le code complet :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
$UsbDrive1_Serial = "_________"
$UsbDrive2_Serial = "__________"

$ShareName = "TestShare"

$DrivePath = "L:\"

$WaitTimeOut = 10
#Found the USB device

#Get the List of volume
Write-Output "Looking for Drives"

$UsbFounded = Get-WmiObject -Class Win32_Volume | Where {$_.SerialNumber -eq $UsbDrive1_Serial -or $_.SerialNumber -eq $UsbDrive2_Serial}

#Get the Share  
$FoundedShare =  Get-WmiObject -Class Win32_Share | Where  {$_.Name -eq $ShareName}

$USBLetter= $UsbFounded.Name
$USBSerial = $UsbFounded.SerialNumber

#Test if right volume founded
If (!$USBLetter)
    {
        Write-Output "No Drive founded"
        if ($FoundedShare)
        {
            Write-Output "ShareFolder is still active this will be deleted"
            Pause
            $FoundedShare.delete() | Out-Null
            exit
        }
    }
else
    {
        Write-Output "Found drive :$USBLetter as with Serial Number $USBSerial"
        # Delete the Share
       
        if (!$FoundedShare)
        {
            Write-Output "Share '$ShareName' has not been founded"
        }
        else
        {
            $FoundedShare.delete() | Out-Null
        }
        #Unmout Device
        $UsbFounded.DriveLetter = $null  
        $UsbFounded.Put()  | Out-Null
        $UsbFounded.Dismount($false, $false) | Out-Null

        Write-output "Swap the disk and press enter"
        Pause 
       
       #Found the disk
       $i = 0
        DO
         {  $temp = Get-WmiObject -Class Win32_Volume | Where {$_.SerialNumber -eq $UsbDrive1_Serial -or $_.SerialNumber -eq $UsbDrive2_Serial}
            Write-output "Wait Disk plugged"
            Start-Sleep -Seconds 1
            $i++
        } while ((!$temp)  -and $i -lt $WaitTimeOut)
        if ($i -ge $WaitTimeOut)
            {
            Write-output "Time out by Disk plug-in : abort operation"
            pause
            exit
            }

        #Mount the disk
        cmd.exe /C mountvol $DrivePath $temp.DeviceID

        #Whait disk is mounted
        while (!(Test-Path -Path $DrivePath)) { 
            Write-output "Wait Disk mounted"
            Start-Sleep -Seconds 1

        } 
        #Activate Share
         Write-output "Activate Share : $ShareName"
            $Shares=[WMICLASS]”WIN32_Share”

            $Shares.Create($DrivePath,$ShareName,0) | Out-Null

            Write-Output "Successfull"


    }

  
 Pause
Vincnet68