locked
Powershell script working on Console app but not working on ASP.NET RRS feed

  • Question

  • User2085541287 posted

    I Have a powershell script to connect on Skypo for Business Online and it is working on powershell execution also on Console application but when I call from ASP.NET not working:

     string command = @"$PlainPassword ='****';
                                    $UserName = '****';
                                    $SecurePassword = $PlainPassword | ConvertTo - SecureString - AsPlainText - Force;
                                    $SkypeOnlineCred = New - Object - TypeName System.Management.Automation.PSCredential - ArgumentList $UserName, $SecurePassword;
                                    Remove - Variable - Name PlainPassword;
                                    Remove - Variable - Name SecurePassword;
                                    Remove - Variable - Name UserName;
                                    try
                                    { 
                                         $SkypeOnlineSession = New - CsOnlineSession - Credential $SkypeOnlineCred;
                                         Import - PSSession - Session $SkypeOnlineSession | Out - Null;
                                    }catch { Throw $_ }";
    
    
    
                var initial = InitialSessionState.CreateDefault();
                initial.ImportPSModule(new string[] {
                    "C:\\Program Files\\Common Files\\Skype for Business Online\\Modules\\SkypeOnlineConnector\\SkypeOnlineConnectorStartup.psm1"
                 });
    
                
                using (Runspace runspace = RunspaceFactory.CreateRunspace(initial))
                {
    
                    // Open runspace 
                    runspace.Open();
             
                    // Initialize PowerShell engine
                    using (PowerShell shell = PowerShell.Create())
                    {
                        shell.Runspace = runspace;
                        
    
                        // Add the script to the PowerShell object
                        shell.Commands.AddScript(command);
                        try
                        {
                            // Execute the script
                            var results = shell.Invoke();
    
                            if (shell.Streams.Error.Count > 0)
                            {
                                throw new Exception(shell.Streams.Error[0].Exception.Message);
                            }
                            // display results, with BaseObject converted to string
                            // Note : use |out-string for console-like output
                            return results;
                        }
                        catch (Exception e)
                        {
                            throw new Exception("On Invoke" + e.Message);
                        }          
    
                    }
                }
    
    

     

    Friday, June 30, 2017 9:56 AM

All replies

  • User753101303 posted

    Hi,

    not working

    Knowing what actually happens would help especially when knowing the same code can run. Avoid to catch an exception if this is just to show less information (which makes debugging harder).

    Friday, June 30, 2017 11:29 AM
  • User2085541287 posted

    Yes, I did

    the exception is same on ASP.NET, also during debugging from Visual Studio.

    Exception Message:

    "The term 'Get-CsPowerShellEndpoint' 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"

    But same code on Console app works like charm :)

    Friday, June 30, 2017 12:20 PM
  • User475983607 posted

    A console app runs on the client machine.  You're trying to invoke Skype on a web server.

    Friday, June 30, 2017 12:29 PM
  • User2085541287 posted

    The Console app and ASP.NET app are running on the same machine 

    Friday, June 30, 2017 12:36 PM
  • User475983607 posted

    Again, you trying to run the app on a web server.  

    https://technet.microsoft.com/en-us/library/dn362795(v=ocs.15).aspx

    Every time you make a request to the page the code tries to create a new remote session.  Even if it were possible how do you plan to sync the user's request to the skype instance on the server?  I don't think what you are trying to do is possible in a web application or at least the way you are trying to accomplish the task.

    Friday, June 30, 2017 12:47 PM
  • User2085541287 posted

    This is tiny app, which will be available to limited number of users 2-3, connection to the S4B Online is only to get some data and immediately close the session, this will not be so often,

    The same thing, already work with Azure with "Connect-MsolService" 

    Friday, June 30, 2017 1:06 PM