Uwe Jäger 3dd03d7efc Make shared folder type smb work for non-english windows
the grant "Everyone,Full" does not work on localized versions of windows, in German it is "Jeder" in Spanish "Todos". Also the net share command returns localized texts, so the -Match does not work for non-english Windows. 

I could not test exactly that version, copied relevant bits from my modified lokal version.
2014-03-17 15:11:45 +01:00

52 lines
1.8 KiB
PowerShell

Param(
[Parameter(Mandatory=$true)]
[string]$path,
[Parameter(Mandatory=$true)]
[string]$share_name,
[string]$host_share_username = $null
)
$ErrorAction = "Stop"
# See all available shares and check alert user for existing/conflicting
# share names.
$path_regexp = [System.Text.RegularExpressions.Regex]::Escape($path)
$name_regexp = [System.Text.RegularExpressions.Regex]::Escape($share_name)
$reg = "(?m)$name_regexp\s+$path_regexp\s"
$existing_share = $($(net share) -join "`n") -Match $reg
if ($existing_share) {
# Always clear the existing share name and create a new one
net share $share_name /delete /y
}
# 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"
if (![string]::IsNullOrEmpty($host_share_username)) {
$computer_name = $(Get-WmiObject Win32_Computersystem).name
$grant = "$computer_name\$host_share_username,Full"
# Here we need to set the proper ACL for this folder. This lets full
# recursive access to this folder.
<#
Get-ChildItem $path -recurse -Force |% {
$current_acl = Get-ACL $_.fullname
$permission = "$computer_name\$host_share_username","FullControl","ContainerInherit,ObjectInherit","None","Allow"
$acl_access_rule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$current_acl.SetAccessRule($acl_access_rule)
$current_acl | Set-Acl $_.fullname
}
#>
}
$result = net share $share_name=$path /unlimited /GRANT:$grant
if ($LastExitCode -eq 0) {
exit 0
}
$host.ui.WriteErrorLine("Error: $result")
exit 1