博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows server FileServer 共享文件的 ABE 设定脚本
阅读量:6910 次
发布时间:2019-06-27

本文共 5445 字,大约阅读时间需要 18 分钟。

Param(    [string]$rootPath)$Source = @"using System;using System.Collections.Generic;using System.Runtime.InteropServices;using System.Text;public enum Share_Type : uint {    STYPE_DISKTREE  = 0x00000000,   // Disk Drive    STYPE_PRINTQ    = 0x00000001,   // Print Queue    STYPE_DEVICE    = 0x00000002,   // Communications Device    STYPE_IPC       = 0x00000003,   // InterProcess Communications    STYPE_SPECIAL   = 0x80000000,   // Special share types (C$, ADMIN$, IPC$, etc)    STYPE_TEMPORARY = 0x40000000   // Temporary share }public enum Share_ReturnValue : int {    NERR_Success            = 0,    ERROR_ACCESS_DENIED     = 5,    ERROR_NOT_ENOUGH_MEMORY = 8,    ERROR_INVALID_PARAMETER = 87,    ERROR_INVALID_LEVEL     = 124, // unimplemented level for info    ERROR_MORE_DATA         = 234,    NERR_BufTooSmall        = 2123, // The API return buffer is too small.    NERR_NetNameNotFound    = 2310 // This shared resource does not exist.}[System.Flags]public enum Shi1005_flags {    SHI1005_FLAGS_DFS                          = 0x0001,  // Part of a DFS tree (Cannot be set)    SHI1005_FLAGS_DFS_ROOT                     = 0x0002,  // Root of a DFS tree (Cannot be set)    SHI1005_FLAGS_RESTRICT_EXCLUSIVE_OPENS     = 0x0100,  // Disallow Exclusive file open    SHI1005_FLAGS_FORCE_SHARED_DELETE          = 0x0200,  // Open files can be force deleted    SHI1005_FLAGS_ALLOW_NAMESPACE_CACHING      = 0x0400,  // Clients can cache the namespace    SHI1005_FLAGS_ACCESS_BASED_DIRECTORY_ENUM  = 0x0800,  // Only directories for which a user has FILE_LIST_DIRECTORY will be listed    SHI1005_FLAGS_FORCE_LEVELII_OPLOCK         = 0x1000,  // Prevents exclusive caching    SHI1005_FLAGS_ENABLE_HASH                  = 0x2000,  // Used for server side support for peer caching    SHI1005_FLAGS_ENABLE_CA                    = 0X4000   // Used for Clustered shares}public class NetApi32 {    // ********** Structures **********    // SHARE_INFO_502    [ StructLayout( LayoutKind.Sequential )]    public struct SHARE_INFO_502    {        [MarshalAs(UnmanagedType.LPWStr)]        public string shi502_netname;        public uint shi502_type;        [MarshalAs(UnmanagedType.LPWStr)]        public string shi502_remark;        public Int32 shi502_permissions;        public Int32 shi502_max_uses;        public Int32 shi502_current_uses;        [MarshalAs(UnmanagedType.LPWStr)]        public string shi502_path;        public IntPtr shi502_passwd;        public Int32 shi502_reserved;        public IntPtr shi502_security_descriptor;    }    // SHARE_INFO_1005    [ StructLayout( LayoutKind.Sequential )]    public struct SHARE_INFO_1005    {        public Int32 Shi1005_flags;    }    private class unmanaged {                    //NetShareGetInfo        [DllImport("Netapi32.dll", SetLastError=true)]        internal static extern int NetShareGetInfo(            [MarshalAs(UnmanagedType.LPWStr)] string serverName,            [MarshalAs(UnmanagedType.LPWStr)] string netName,            Int32 level,            ref IntPtr bufPtr         );        //NetShareSetInfo        [DllImport("netapi32.dll", SetLastError=true)]        internal static extern int NetShareSetInfo(            String servername,             String netname,             Int32 level,             ref IntPtr buf,             out UInt32 parm_err        );    }    // ***** Functions *****    public static SHARE_INFO_502 NetShareGetInfo_502(string ServerName, string ShareName) {        Int32 level = 502;        IntPtr lShareInfo = IntPtr.Zero;        SHARE_INFO_502 shi502_Info = new SHARE_INFO_502();        Int32 result = unmanaged.NetShareGetInfo(ServerName, ShareName, level, ref lShareInfo);        if((Share_ReturnValue)result == Share_ReturnValue.NERR_Success) {            shi502_Info = (SHARE_INFO_502)Marshal.PtrToStructure(lShareInfo, typeof(SHARE_INFO_502));        } else {            throw new Exception("Unable to get 502 structure.  Function returned: " + (Share_ReturnValue)result);        }        return shi502_Info;    }    public static SHARE_INFO_1005 NetShareGetInfo_1005(string ServerName, string ShareName) {        Int32 level = 1005;        IntPtr lShareInfo = IntPtr.Zero;        SHARE_INFO_1005 shi1005_Info = new SHARE_INFO_1005();        Int32 result = unmanaged.NetShareGetInfo(ServerName, ShareName, level, ref lShareInfo);        if((Share_ReturnValue)result == Share_ReturnValue.NERR_Success) {            shi1005_Info = (SHARE_INFO_1005)Marshal.PtrToStructure(lShareInfo, typeof(SHARE_INFO_1005));        } else {            throw new Exception("Unable to get 1005 structure.  Function returned: " + (Share_ReturnValue)result);        }        return shi1005_Info;    }    public static int NetShareSetInfo_1005(string ServerName, string ShareName, Int32 Shi1005_flags) {        Int32 level = 1005;        UInt32 err;        SHARE_INFO_1005 shi1005_Info = new SHARE_INFO_1005();        shi1005_Info.Shi1005_flags = Shi1005_flags;        IntPtr lShareInfo = GCHandle.ToIntPtr(GCHandle.Alloc(shi1005_Info));        Int32 result = unmanaged.NetShareSetInfo(ServerName, ShareName, level, ref lShareInfo, out err);        return result;    }}"@#[NetApi32]::NetShareSetInfo_502("MyServer","MyShareName")Add-Type -TypeDefinition $Source -Language CSharp  $SmbShare = new-object NetApi32$SmbShare.NetShareSetInfo_1005($rootPath)#useAge .\NetShareSetInfo.ps1 -rootPath "\\PXXXXXXXXXXXX\share"

 

转载于:https://www.cnblogs.com/yuzhengdong/p/4436467.html

你可能感兴趣的文章
深度:ARC会导致的内存泄露
查看>>
【Android】第18章 位置服务和手机定位—本章示例主界面
查看>>
如何让 protected internal 跨程序集!
查看>>
结对编程作业总结2
查看>>
2018-2019-1 20165231 《信息安全系统设计基础》第七周学习总结
查看>>
转 10 个最佳的 Node.js 的 MVC 框架
查看>>
Linux学习笔记(六)-Linux服务程序的安装和卸载
查看>>
转 @JoinColumn 详解
查看>>
mysql 主从复制
查看>>
详解C/C++预处理器
查看>>
阿里云OSS图片上传plupload.js结合jq-weui 图片上传的插件
查看>>
随机产生id字符串
查看>>
(十七)SpringBoot之使用异步消息服务jms之ActiveMQ
查看>>
第19讲 | 上手搭建一条自己的智能合约
查看>>
Matlab绘制空间几何图
查看>>
在Node.js上搭建React.js开发环境
查看>>
常州大学新生寒假训练会试 题解
查看>>
ASCII,Unicode和UTF-8
查看>>
性能指标术语&理发店模型
查看>>
【几个关于CSS的网站】
查看>>