Forum Discussion

KJS's avatar
KJS
Brass Contributor
May 02, 2017

Powershell script in C# code under azure web job error

when i try to execute powershell script using the powershell object in c# code it runs ok on my desktop in visual studio but when i deploy the samme code on azure as web job then there is another problem and it breaks.

Code : 

            PowerShell ps = PowerShell.Create();
            InitialSessionState initialSession = InitialSessionState.CreateDefault();
            initialSession.ImportPSModule(new string[] { "Microsoft.Online.SharePoint.PowerShell" });
            initialSession.ImportPSModule(new string[] { "Microsoft.SharePoint.Client" });
            initialSession.ImportPSModule(new string[] { "Microsoft.SharePoint.Client.Runtime" });
            
            Runspace rSpace = RunspaceFactory.CreateRunspace(initialSession);
            rSpace.Open();
            RunspaceInvoke invoker = new RunspaceInvoke(rSpace);

            const string connectScript = " $AdminUrl = \"https://tenant-admin.sharepoint.com\" \r\n $UserName = \"user@tenant.onmicrosoft.com\" \r\n $Password  = \"password\" \r\n $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force \r\n $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword \r\n Connect-SPOService -Url $AdminUrl -Credential $Credentials \r\n Get-SPOSite -Detailed -Limit All | select * " ;

            Console.WriteLine("Powershell execution start");
            //const string getSiteScript =  "..\\..\\GetAllSiteColectionOnlineDetails.ps1";             
            var sites = invoker.Invoke(connectScript);  


Error in webjob logs: 

[05/02/2017 04:16:51 > abb3f6: INFO] Powershell execution start
[05/02/2017 04:17:58 > abb3f6: ERR ] 
[05/02/2017 04:17:58 > abb3f6: ERR ] Unhandled Exception: System.Management.Automation.CommandNotFoundException: The term 'Connect-SPOService' 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.
[05/02/2017 04:17:58 > abb3f6: ERR ]    at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
[05/02/2017 04:17:58 > abb3f6: ERR ]    at System.Management.Automation.RunspaceInvoke.Invoke(String script, IEnumerable input)

Thanks for your help

9 Replies

  • You shoudl try to set $env:PSModulePath it looks like your modules aren't being found. Did you publish the modules to your Azure environment?

    • KJS's avatar
      KJS
      Brass Contributor
      I have just deployed the project using publish to azure and in package file i can see reference to dll's. do i need to explicitly publish the dll to azure. can you tell me how can we do that.

      Thanks
      • Hi KJS,

         

        I'm having to make some assumptions. But if your module files are in Azure then you migth find that they aren't in the location where you expect them to be.

         

        In general I first get the path where I'm running my scripts form:

         

         

         $path = Split-Path -parent $MyInvocation.MyCommand.Definition

        Then I set my module path 

         

         

        if ($env:PSModulePath -notlike "*$path\Modules\MyModules\Modules*")
        {
              $env:PSModulePath += ";$path\Modules\MyModules\Modules"
        }

        you might find that when you build abnd publish your project the module files are found in (and therefore published to) the bin/debug folder.

         

         

         

         

         

Resources