Creating a Windows Network Share

k v l's Avatar

k v l

16 Jan, 2018 02:26 PM

Hi,

I tried to make a Windows Share with Powershell:
New-SMBShare –Name "#{ProjectNaam}" –Path "#{InstallDirectory}\App_Data\#{ProjectNaam}" `
 –ContinuouslyAvailable `
 -ReadAccess “account”

But i got error:
The term 'New-SMBShare' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Is there someway i can fix this error? Or is there a better/different way of doing this?

King regards

  1. 1 Posted by daytona650 on 16 Jan, 2018 03:15 PM

    daytona650's Avatar

    Have you installed the smbshare module?

  2. 2 Posted by k v l on 16 Jan, 2018 03:18 PM

    k v l's Avatar

    No i dont think so, how do i do that?

    Thnx for the reply

  3. 3 Posted by k v l on 17 Jan, 2018 08:58 AM

    k v l's Avatar

    I found out that i didnt have Powershell 3.0 so i installed it, but i have Windows Server 2008 so i wont work anyway... I'm going to write a little C# code to do it now.

    Grtz

  4. Support Staff 4 Posted by Lawrence Wilson on 23 Jan, 2018 01:48 AM

    Lawrence Wilson's Avatar

    Hi Grtz,
    Thanks for keeping us up to date on this one! I would love to hear how you go with implementing your solution in C#

    Kind regards,
    Lawrence.

  5. 5 Posted by k v l on 23 Jan, 2018 08:08 AM

    k v l's Avatar

    My main function:

    string strServer = "localhost";
    string strShareFolder = this.Bron;
    string strShareName = tMapNaam;
    string strShareDesc = tMapNaam + " Share voor GeometrieService";
    string tNetwerkMapNaam = @"\\" + strServer + @"\" + tMapNaam;
    string tGeometrieServiceGebruiker = "[email blocked]"; // System.Environment.MachineName + "\\tsn.acc.geometrieservice";
    NetApi32.NetError nRetVal = 0;

    try
    {
    nRetVal = this.CreateShare(strServer, strShareFolder, strShareName, strShareDesc, false);

    if (nRetVal == NetApi32.NetError.NERR_Success)
    {
    Console.WriteLine("Share {0} created", strShareName);
    }
    else if (nRetVal == NetApi32.NetError.NERR_AccessDenied)
    {
    Console.WriteLine("Share {0}: Access Denied", strShareName);
    }
    else if (nRetVal == NetApi32.NetError.NERR_DuplicateShare)
    {
    Console.WriteLine("Share {0} already exists", strShareName);
    }
    else
    {
    Console.WriteLine("SharePath error code: " + nRetVal, strShareName);
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }

    Console.WriteLine("tGeometrieServiceGebruiker: " + tGeometrieServiceGebruiker);
    Console.WriteLine("tNetwerkMapNaam: " + tNetwerkMapNaam);

    DirectoryInfo myDirectoryInfo = new DirectoryInfo(tNetwerkMapNaam);
    DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

    myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(tGeometrieServiceGebruiker, FileSystemRights.Read, AccessControlType.Allow));

    foreach(FileSystemAccessRule tFileSystemAccessRule in myDirectorySecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
    {
    string userName = tFileSystemAccessRule.IdentityReference.Value;
    string userRights = tFileSystemAccessRule.FileSystemRights.ToString();
    string userAccessType = tFileSystemAccessRule.AccessControlType.ToString();
    Console.WriteLine(userName + " : " + userAccessType + " : " + userRights);
    }

    myDirectoryInfo.SetAccessControl(myDirectorySecurity);

    Console.WriteLine("Einde SharePath");

    Create share function:

    NetApi32.NetError CreateShare(string strServer, string strPath, string strShareName, string strShareDesc, bool bAdmin)
    {
    NetApi32.SHARE_INFO_502 shInfo = new NetApi32.SHARE_INFO_502();
    shInfo.shi502_netname = strShareName;
    shInfo.shi502_type = (uint)NetApi32.SHARE_TYPE.STYPE_DISKTREE;
    if (bAdmin)
    {
    shInfo.shi502_type = (uint)NetApi32.SHARE_TYPE.STYPE_SPECIAL;
    shInfo.shi502_netname += "$";
    }
    shInfo.shi502_permissions = 0;
    shInfo.shi502_path = strPath;
    shInfo.shi502_passwd = IntPtr.Zero;
    shInfo.shi502_remark = strShareDesc;
    shInfo.shi502_max_uses = -1;
    shInfo.shi502_security_descriptor = IntPtr.Zero;

    string strTargetServer = strServer;
    if (strServer.Length != 0)
    {
    strTargetServer = strServer;
    if (strServer[0] != '\\')
    {
    strTargetServer = "\\\\" + strServer;
    }
    }
    int nRetValue = 0;
    // Call Net API to add the share..
    int nStSize = Marshal.SizeOf(shInfo);
    IntPtr buffer = Marshal.AllocCoTaskMem(nStSize);
    Marshal.StructureToPtr(shInfo, buffer, false);
    nRetValue = NetApi32.NetShareAdd(strTargetServer, 502, buffer, IntPtr.Zero);
    Marshal.FreeCoTaskMem(buffer);

    return (NetApi32.NetError)nRetValue;
    }

    I run this piece of code bij running the exe from powershell (passing the folder name etc as parameters):
    $p = Start-Process $ServerWoonConnectController -ArgumentList $command, $arg1

  6. Support Staff 6 Posted by Lawrence Wilson on 31 Jan, 2018 04:37 AM

    Lawrence Wilson's Avatar

    Hi Grtz,
    Thanks for letting us know your solution on this!

    Kind regards,
    Lawrence.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac