Powershell을 사용하여 공유를 작성하고 액세스 권한을 설정하는 방법.
예를 들면 다음과 같습니다
트릭을 수행해야합니다.
net share "Public=c:\shares\foo" "/GRANT:Users,READ"
물론 작업을 수행하는 위치/방법에 따라 관리 권한으로 PowerShell을 시작해야합니다.
Win32_Share Create 메소드를 사용하십시오. 예:
(Get-WmiObject -List -ComputerName . | Where-Object -FilterScript
{$_.Name -eq "Win32_Share"}).InvokeMethod("Create",
("C:\FolderToShare","ShareName",0,100,"Share description"))
이 방법에 대한 설명서를 찾을 수 있습니다 여기서는 MSDN .
uint32 Create(
[in] string Path,
[in] string Name,
[in] uint32 Type,
[in] uint32 MaximumAllowed,
[in] string Description,
[in] string Password,
[in] Win32_SecurityDescriptor Access
);
매개 변수 :
액세스 권한을 설정하는 방법에 대한 자세한 내용은 MSDN의이 페이지를 참조하십시오 : Win32_SecurityDescriptor Class . 이 기사는 또한 좋은 시작점입니다. WMI 작업 : 파일 및 폴더 .
아래의 기능은 예이며, 원하는대로 조정할 수 있습니다. 주요 제한 사항은 공유를 호스팅 할 시스템에서 실행해야합니다 (또는 PS Remoting을 사용하여 해당 시스템에 먼저 도달해야 함). 스크립트를 실행하는 계정에도 공유를 만들 수있는 충분한 권한이 있어야합니다.
작성된 바와 같이 DirectoryInfo
객체를 인수로 예상하지만 문자열에 맞게 조정하는 것은 어렵지 않습니다. 이 예에는 각각 서로 다른 종류의 액세스 권한을 가진 두 개의 서로 다른 개체 (한 명의 사용자와 한 명의 그룹)에 대한 폴더에 대한 권한이 포함되어 있으므로 복잡한 권한 요구 사항을 혼합하고 일치시키는 방법을 확인할 수 있습니다.
# $folder is a DirectoryInfo object
Function Create-FileShare($folder)
{
$name = $folder.Name
$path = $folder.FullName
$description = "$name"
$domain = "example.com" #AD Domain name here (Optional/Not really used/Here for completeness)
$Method = "Create"
$sd = ([WMIClass] "Win32_SecurityDescriptor").CreateInstance()
#AccessMasks:
#2032127 = Full Control
#1245631 = Change
#1179817 = Read
#Share with the user
$ACE = ([WMIClass] "Win32_ACE").CreateInstance()
$Trustee = ([WMIClass] "Win32_Trustee").CreateInstance()
$Trustee.Name = $name
$Trustee.Domain = $Null
#original example assigned this, but I found it worked better if I left it empty
#$Trustee.SID = ([wmi]"win32_userAccount.Domain='$domain',Name='$name'").sid
$ace.AccessMask = 1245631
$ace.AceFlags = 3 #Should almost always be three. Really. don't change it.
$ace.AceType = 0 # 0 = allow, 1 = deny
$ACE.Trustee = $Trustee
$sd.DACL += $ACE.psObject.baseobject
#Share with Domain Admins
$ACE = ([WMIClass] "Win32_ACE").CreateInstance()
$Trustee = ([WMIClass] "Win32_Trustee").CreateInstance()
$Trustee.Name = "Domain Admins"
$Trustee.Domain = $Null
#$Trustee.SID = ([wmi]"win32_userAccount.Domain='$domain',Name='$name'").sid
$ace.AccessMask = 2032127
$ace.AceFlags = 3
$ace.AceType = 0
$ACE.Trustee = $Trustee
$sd.DACL += $ACE.psObject.baseobject
$mc = [WmiClass]"Win32_Share"
$InParams = $mc.psbase.GetMethodParameters($Method)
$InParams.Access = $sd
$InParams.Description = $description
$InParams.MaximumAllowed = $Null
$InParams.Name = $name
$InParams.Password = $Null
$InParams.Path = $path
$InParams.Type = [uint32]0
$R = $mc.PSBase.InvokeMethod($Method, $InParams, $Null)
switch ($($R.ReturnValue))
{
0 {Write-Host "Share:$name Path:$path Result:Success"; break}
2 {Write-Host "Share:$name Path:$path Result:Access Denied" -foregroundcolor red -backgroundcolor yellow;break}
8 {Write-Host "Share:$name Path:$path Result:Unknown Failure" -foregroundcolor red -backgroundcolor yellow;break}
9 {Write-Host "Share:$name Path:$path Result:Invalid Name" -foregroundcolor red -backgroundcolor yellow;break}
10 {Write-Host "Share:$name Path:$path Result:Invalid Level" -foregroundcolor red -backgroundcolor yellow;break}
21 {Write-Host "Share:$name Path:$path Result:Invalid Parameter" -foregroundcolor red -backgroundcolor yellow;break}
22 {Write-Host "Share:$name Path:$path Result:Duplicate Share" -foregroundcolor red -backgroundcolor yellow;break}
23 {Write-Host "Share:$name Path:$path Result:Reedirected Path" -foregroundcolor red -backgroundcolor yellow;break}
24 {Write-Host "Share:$name Path:$path Result:Unknown Device or Directory" -foregroundcolor red -backgroundcolor yellow;break}
25 {Write-Host "Share:$name Path:$path Result:Network Name Not Found" -foregroundcolor red -backgroundcolor yellow;break}
default {Write-Host "Share:$name Path:$path Result:*** Unknown Error ***" -foregroundcolor red -backgroundcolor yellow;break}
}
}
Windows 7의 경우 다음을 시도하십시오.
net SHARE share=d:\share /GRANT:EVERYONE`,FULL /REMARK:"
위는 PowerShell에서도 작동합니다. 참고`전, FULL