Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
How can I replicate New-SmbGlobalMapping in C# code?

I am writing a service which controls docker containers. I want to have the mounted volume as an Azure share, and thus need to use the SMB Global Mapping. If I use the usual WNetAddConnection2A then I can mount the share just fine in my code, but the containers cannot see it as it is not "global". I can't find source for the PowerShell New-SmbGlobalMapping command (is there a way to see it?) and I can't find a suitable API to call. I hope someone knows the magic incantation I can put in my .NET code.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I can't find source for the PowerShell New-SmbGlobalMapping command (is there a way to see it?) and I can't find a suitable API to call. I hope someone knows the magic incantation I can put in my .NET code.

PowerShell uses WMI

In your case, it calls Create method of the MSFT_SmbMapping class (MSFT_SmbGlobalMapping exactly)

You can use WMI Code Creator to generate/test C# code


EDIT : Test with PowerShell.Create

  • Test as Admin ("requireAdministrator" in manifest) on Windows 10
  • Test code (C#, VS 2015) =>
    // PowerShell calls CredUIPromptForCredentialsW to display the User/Password dialog (you can call it with P/Invoke if needed)
    string sUser = "user@provider.com";
    string sPassword = "myPassword";
    System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential(sUser, sPassword, null);
    System.Security.SecureString securePassword = new System.Security.SecureString();
    foreach (var c in networkCredential.Password)
        securePassword.AppendChar(c);
    // Add reference to :
    // C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll
    // Add :
    // using System.Management.Automation;
    PSCredential psCredential = new PSCredential(networkCredential.UserName, securePassword);

    // Error handling must be improved : if I pass an invalid syntax for "RemotePath" or not launched as Admin,
    // nothing happens (no error, no result) (on Windows 10)
    string sLocalPath = "Q:";
    string sRemotePath = "\\\\DESKTOP-EOPIFM5\\Windows 7";
    using (var ps = PowerShell.Create())
    {
        ps.AddCommand("New-SmbGlobalMapping");
        ps.AddParameter("LocalPath", sLocalPath);
        ps.AddParameter("RemotePath", sRemotePath);
        ps.AddParameter("Credential", psCredential);
        //ps.AddParameter("RequireIntegrity", false);
        //ps.AddParameter("RequirePrivacy", false);    
        try
        {
            System.Collections.ObjectModel.Collection<PSObject> collectionResults = ps.Invoke();
            foreach (PSObject psObl in collectionResults)
            {
                Console.WriteLine("Status : {0}", psObl.Members["Status"].Value.ToString());
                Console.WriteLine("Local Path : {0}", psObl.Members["LocalPath"].Value.ToString());
                Console.WriteLine("Remote Path : {0}\n", psObl.Members["RemotePath"].Value.ToString());
            }
        }
        catch (ParameterBindingException pbe)
        {
            System.Console.WriteLine("\rNew-SmbGlobalMapping error : {0}: {1}",
                          pbe.GetType().FullName, pbe.Message);
        }
    }
    // To get and remove the test mapping in PowerShell :
    // Get-SmbGlobalMapping
    // Remove-SmbGlobalMapping -RemotePath "\\DESKTOP-EOPIFM5\Windows 7" -Force
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!