Skip to main content

Script Sauvegarde de partage

Basic :

export ( basic ) :

# Export-Shares.ps1
$shares = Get-WmiObject -Class Win32_Share
$shares | Select-Object Name, Path, Description, MaximumAllowed, InstallDate, Caption, Status, Type, AllowMaximum | Export-Csv -Path "C:\exports\shares_export.csv" -NoTypeInformation

import ( basic ) :

# Import-Shares.ps1
$shares = Import-Csv -Path "C:\exports\shares_export.csv"

foreach ($share in $shares) {
    New-SmbShare -Name $share.Name -Path $share.Path -Description $share.Description -FullAccess "Everyone"
    if ($share.MaximumAllowed -ne $null) {
        Set-SmbShare -Name $share.Name -ConcurrentUserLimit $share.MaximumAllowed
    }
}

# Remarque : Ajustez les permissions d'accès selon vos besoins

Avec droits :

export :

# Export-Shares.ps1
$shares = Get-WmiObject -Class Win32_Share | Where-Object { $_.Type -eq 0 }
$shareInfo = foreach ($share in $shares) {
    $permissions = (Get-SmbShareAccess -Name $share.Name) | Select-Object Name, AccessControlType, AccountName
    [PSCustomObject]@{
        Name              = $share.Name
        Path              = $share.Path
        Description       = $share.Description
        MaximumAllowed    = $share.MaximumAllowed
        InstallDate       = $share.InstallDate
        Caption           = $share.Caption
        Status            = $share.Status
        Type              = $share.Type
        AllowMaximum      = $share.AllowMaximum
        Permissions       = $permissions
    }
}
$shareInfo | Export-Csv -Path "C:\exports\shares_export.csv" -NoTypeInformation

import :

# Import-Shares.ps1
$shares = Import-Csv -Path "C:\exports\shares_export.csv"

foreach ($share in $shares) {
    New-SmbShare -Name $share.Name -Path $share.Path -Description $share.Description

    if ($share.MaximumAllowed -ne $null) {
        Set-SmbShare -Name $share.Name -ConcurrentUserLimit $share.MaximumAllowed
    }

    $permissions = ConvertFrom-Json $share.Permissions
    foreach ($permission in $permissions) {
        Grant-SmbShareAccess -Name $share.Name -AccountName $permission.AccountName -AccessRight $permission.AccessControlType
    }
}