Author Posts

October 26, 2015 at 3:38 am

I REALLY wanted to avoid posting here, but I'm out of ideas, and this continues on from a forum post Don Jones answered for me (https://powershell.org/forums/topic/what-is-powershell-made-from/). I posted on StackOverflow but I think the only answers I got were from posts I had googled previously and didn't work for me.

This is one of the several different variations of me trying to call Get-SmbShare in C#:

        private static void GetShareNames()
        {
            InitialSessionState iss = InitialSessionState.CreateDefault();
            iss.ImportPSModule(new[] { "SmbShare" });
            Runspace runspace = RunspaceFactory.CreateRunspace(iss);
            runspace.Open();
            PowerShell ps = PowerShell.Create();
            ps.Runspace = runspace;
            ps.Commands.AddCommand("Get-SMBShare");

            foreach (PSObject result in ps.Invoke())
            {
                Console.WriteLine(result.Members["Name"].Value);
            }

        }
        static void Main(string[] args)
        {
            GetShareNames();
        }

I'm getting this error:

An unhandled exception of type 'System.Management.Automation.CommandNotFoundException' occurred in System.Management.Automation.dll

Additional information: The term 'Get-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.

I understand this isn't really right for this forum, but I think if anyone will have a good idea of where I'm going wrong, it's going to be this community.

October 26, 2015 at 4:17 am

Is this a 32-bit executable, or 64-bit? On my system, the SmbShare module only exists for 64-bit PowerShell, for whatever reason, and your code may work fine if you compile it as x64.

October 26, 2015 at 4:42 am

In the hours I spent changing my code, how did I not think to check that out?

Thank you! Problem solved.