使用Powershell自动设置文件共享,需要用到WMI对象,WIN32_Share类
- 确保共享的文件夹是否存在,如果不存在就创建
- 创建一个WIN32_Share对象
- 查看WIN32_Share对象支持的方法
- 查看WIN32_Share的create方法的定义
- 添加文件加为共享
- 验证文件是否已经共享
具体代码如下:
$ShareFolderPhysicalPath="E:MyShare" $ShareFolderNetworkPath="通过Powershell共享的文件夹" #文件夹不存在就创建 if(!(Test-Path $ShareFolder)) { New-Item -Path $ShareFolder -type directory } #调用WMI对象 WIN32_Share类 $ShareHandle=[WMIClass]"WIN32_Share" #WIN32_Share有一个Create方法 #可以通过get-member查看 $ShareHandle | Get-Member -MemberType method #查看Create方法的定义 $ShareHandle.Create #添加为共享 $ShareHandle.Create($ShareFolderPhysicalPath,$ShareFolderNetworkPath,0) #查看是否已经共享 Get-WmiObject WIN32_Share | where { ($_.Path -eq $ShareFolderPhysicalPath) -and ($_.Name -eq $ShareFolderNetworkPath) }
本文链接: https://www.pstips.net/powershell-add-share-foldel.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
PS C:\> $ShareHandle.Create
OverloadDefinitions
——————-
System.Management.ManagementBaseObject Create(System.String Path, System.String Name, System.UInt32 Type,
System.UInt32 MaximumAllowed, System.String Description, System.String Password,
System.Management.ManagementObject#Win32_SecurityDescriptor Access)
有关最后一项System.Management.ManagementObject#Win32_SecurityDescriptor Access,具体该如何写语法呢?公司要求Everyone的权限一定是要被删除的,https://msdn.microsoft.com/en-us/library/aa389393(VS.85).aspx 参考了很多资料,实在是无法参透。求指导
广播: Hyper-V Server 2019 安装使用记录 – 源码巴士