Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda