Powershell script in C# code under azure web job error

%3CLINGO-SUB%20id%3D%22lingo-sub-66406%22%20slang%3D%22en-US%22%3EPowershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66406%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3Ewhen%20i%20try%20to%20execute%20powershell%20script%20using%20the%20powershell%20object%20in%20c%23%20code%20it%20runs%20ok%20on%20my%20desktop%20in%20visual%20studio%20but%20when%20i%20deploy%20the%20samme%20code%20on%20azure%20as%20web%20job%20then%20there%20is%20another%20problem%20and%20it%20breaks.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3ECode%20%3A%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20PowerShell%20ps%20%3D%20PowerShell.Create()%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20InitialSessionState%20initialSession%20%3D%20InitialSessionState.CreateDefault()%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20initialSession.ImportPSModule(new%20string%5B%5D%20%7B%20%22Microsoft.Online.SharePoint.PowerShell%22%20%7D)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20initialSession.ImportPSModule(new%20string%5B%5D%20%7B%20%22Microsoft.SharePoint.Client%22%20%7D)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20initialSession.ImportPSModule(new%20string%5B%5D%20%7B%20%22Microsoft.SharePoint.Client.Runtime%22%20%7D)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Runspace%20rSpace%20%3D%20RunspaceFactory.CreateRunspace(initialSession)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20rSpace.Open()%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20RunspaceInvoke%20invoker%20%3D%20new%20RunspaceInvoke(rSpace)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20const%20string%20connectScript%20%3D%20%22%20%24AdminUrl%20%3D%20%5C%22%3CA%20href%3D%22https%3A%2F%2Ftenant-admin.sharepoint.com%5C%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftenant-admin.sharepoint.com%5C%3C%2FA%3E%22%20%5Cr%5Cn%20%24UserName%20%3D%20%5C%22user%40tenant.onmicrosoft.com%5C%22%20%5Cr%5Cn%20%24Password%20%26nbsp%3B%3D%20%5C%22password%5C%22%20%5Cr%5Cn%20%24SecurePassword%20%3D%20%24Password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%20%5Cr%5Cn%20%24Credentials%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-argumentlist%20%24userName%2C%20%24SecurePassword%20%5Cr%5Cn%20Connect-SPOService%20-Url%20%24AdminUrl%20-Credential%20%24Credentials%20%5Cr%5Cn%20Get-SPOSite%20-Detailed%20-Limit%20All%20%7C%20select%20*%20%22%20%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Console.WriteLine(%22Powershell%20execution%20start%22)%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2Fconst%20string%20getSiteScript%20%3D%20%26nbsp%3B%22..%5C%5C..%5C%5CGetAllSiteColectionOnlineDetails.ps1%22%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20var%20sites%20%3D%20invoker.Invoke(connectScript)%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EError%20in%20webjob%20logs%3A%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%5B05%2F02%2F2017%2004%3A16%3A51%20%26gt%3B%20abb3f6%3A%20INFO%5D%20Powershell%20execution%20start%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%5B05%2F02%2F2017%2004%3A17%3A58%20%26gt%3B%20abb3f6%3A%20ERR%20%5D%26nbsp%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%5B05%2F02%2F2017%2004%3A17%3A58%20%26gt%3B%20abb3f6%3A%20ERR%20%5D%20Unhandled%20Exception%3A%20System.Management.Automation.CommandNotFoundException%3A%20The%20term%20'Connect-SPOService'%20is%20not%20recognized%20as%20the%20name%20of%20a%20cmdlet%2C%20function%2C%20script%20file%2C%20or%20operable%20program.%20Check%20the%20spelling%20of%20the%20name%2C%20or%20if%20a%20path%20was%20included%2C%20verify%20that%20the%20path%20is%20correct%20and%20try%20again.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%5B05%2F02%2F2017%2004%3A17%3A58%20%26gt%3B%20abb3f6%3A%20ERR%20%5D%20%26nbsp%3B%20%26nbsp%3Bat%20System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable%20input)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%5B05%2F02%2F2017%2004%3A17%3A58%20%26gt%3B%20abb3f6%3A%20ERR%20%5D%20%26nbsp%3B%20%26nbsp%3Bat%20System.Management.Automation.RunspaceInvoke.Invoke(String%20script%2C%20IEnumerable%20input)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EThanks%20for%20your%20help%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-66406%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66710%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66710%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20i%20will%20try%20to%20include%20and%20push%20that%20folder%20to%20azure.%20and%20then%20refere%20the%20module.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20if%20i%20can%20do%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20help%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66707%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66707%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EThe%20issue%20will%20be%20that%20the%20Sharepoint%20online%20management%20shell%20is%20not%20installed%20within%20your%20Azure%20context.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EBy%20default%20the%20module%20is%20installed%20in%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EC%3A%5CProgram%20Files%5CSharePoint%20Online%20Management%20Shell%5C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ESo%20you%20will%20need%20to%20copy%20the%20files%20in%26nbsp%3BC%3A%5CProgram%20Files%5CSharePoint%20Online%20Management%20Shell%20%26nbsp%3B(this%20is%20a%20folder%20%26nbsp%3Bcalled%20Microsoft.Online.SharePoint.PowerShell)%20including%20the%20contents%20of%20the%20folder%20to%20Azure.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThen%20include%20the%20location%20where%20you%20put%20the%20folder%20in%20your%20%24env%3APSModulePath%20and%20you%20should%20be%20up%20and%20running.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66703%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66703%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20reply%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20not%20a%20very%20good%20at%20powershell%20all%20i%20am%20trying%20to%20do%20is%20call%20a%20powershell%20script%20from%20the%20C%23%20code%26nbsp%3B%3C%2FP%3E%3CP%3EConnect-SPO%20commandlet%20is%20not%20my%20custom%20module%20or%20anything%20it%20is%20Sharepoint%20online%20management%20shell%20command%20which%20is%20just%20a%20package%20i%20installed%20on%20my%20machine%20to%20run%20it%20works%20fine%20on%20local.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20passing%20the%20dll%20to%20azure%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPath%20to%20code%20where%20all%20dll%20including%20the%20spo%20management%20shell%20dll%20is%26nbsp%3B%3C%2FP%3E%3CP%3ED%3A%5C%5Chome%5C%5Csite%5C%5Cwwwroot%5C%5Capp_data%5C%5Cjobs%5C%5Ctriggered%5C%5CNMLSiteStorage%5C%5C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eexe%20file%20is%20also%20in%20the%20same%20folder%20where%20it%20is%20executing%20the%20code%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecan%20you%20please%20tell%20the%20exact%20code%20how%20to%20set%20that%20variable%3C%2FP%3E%3CPRE%3Eif%20(%24env%3APSModulePath%20-notlike%20%22*%24pathD%3A%5C%5Chome%5C%5Csite%5C%5Cwwwroot%5C%5Capp_data%5C%5Cjobs%5C%5Ctriggered%5C%5CNMLSiteStorage%5C%5C*%22)%0A%7B%0A%24env%3APSModulePath%20%2B%3D%20%22%3B%24path%22%0A%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Econst%20string%20connectScript%20%3D%20%22%20%24path%20%3D%20Split-Path%20-parent%20%24MyInvocation.MyCommand.Definition%20%5Cr%5Cn%20%20Import-Module%20Microsoft.Online.SharePoint.PowerShell%20%5Cr%5Cn%20%24AdminUrl%20%3D%20%5C%22https%3A%2F%2FNewcrestmining-admin.sharepoint.com%5C%22%20%5Cr%5Cn%20%24UserName%20%3D%20%5C%22kanwarjot.singh%40newcrestmining.onmicrosoft.com%5C%22%20%5Cr%5Cn%20%24Password%20%20%3D%20%5C%22Crest99%5C%22%20%5Cr%5Cn%20%24SecurePassword%20%3D%20%24Password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%20%5Cr%5Cn%20%24Credentials%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-argumentlist%20%24userName%2C%20%24SecurePassword%20%5Cr%5Cn%20Connect-SPOService%20-Url%20%24AdminUrl%20-Credential%20%24Credentials%20%5Cr%5Cn%20Get-SPOSite%20-Detailed%20-Limit%20All%20%7C%20select%20*%20%22%3B%3C%2FPRE%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EKJ%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66702%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66702%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20reply%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20not%20a%20very%20good%20at%20powershell%20all%20i%20am%20trying%20to%20do%20is%20call%20a%20powershell%20script%20from%20the%20C%23%20code%26nbsp%3B%3C%2FP%3E%3CP%3EConnect-SPO%20commandlet%20is%20not%20my%20custom%20module%20or%20anything%20it%20is%20Sharepoint%20online%20management%20shell%20command%20which%20is%20just%20a%20package%20i%20installed%20on%20my%20machine%20to%20run%20it%20works%20fine%20on%20local.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20passing%20the%20dll%20to%20azure%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPath%20to%20code%20where%20all%20dll%20including%20the%20spo%20management%20shell%20dll%20is%26nbsp%3B%3C%2FP%3E%3CP%3ED%3A%5C%5Chome%5C%5Csite%5C%5Cwwwroot%5C%5Capp_data%5C%5Cjobs%5C%5Ctriggered%5C%5CNMLSiteStorage%5C%5C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eexe%20file%20is%20also%20in%20the%20same%20folder%20where%20it%20is%20executing%20the%20code%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecan%20you%20please%20tell%20the%20exact%20code%20how%20to%20set%20that%20variable%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Eif%20(%24env%3APSModulePath%20-notlike%20%22*%24pathD%3A%5C%5Chome%5C%5Csite%5C%5Cwwwroot%5C%5Capp_data%5C%5Cjobs%5C%5Ctriggered%5C%5CNMLSiteStorage%5C%5C*%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24env%3APSModulePath%20%2B%3D%20%22%3B%24path%22%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Econst%20string%20connectScript%20%3D%20%22%20%24path%20%3D%20Split-Path%20-parent%20%24MyInvocation.MyCommand.Definition%20%5Cr%5Cn%20%20Import-Module%20Microsoft.Online.SharePoint.PowerShell%20%5Cr%5Cn%20%24AdminUrl%20%3D%20%5C%22https%3A%2F%2FNewcrestmining-admin.sharepoint.com%5C%22%20%5Cr%5Cn%20%24UserName%20%3D%20%5C%22kanwarjot.singh%40newcrestmining.onmicrosoft.com%5C%22%20%5Cr%5Cn%20%24Password%20%20%3D%20%5C%22Crest99%5C%22%20%5Cr%5Cn%20%24SecurePassword%20%3D%20%24Password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%20%5Cr%5Cn%20%24Credentials%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-argumentlist%20%24userName%2C%20%24SecurePassword%20%5Cr%5Cn%20Connect-SPOService%20-Url%20%24AdminUrl%20-Credential%20%24Credentials%20%5Cr%5Cn%20Get-SPOSite%20-Detailed%20-Limit%20All%20%7C%20select%20*%20%22%3B%3C%2FPRE%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EKJ%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66693%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66693%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20will%20need%20to%20replace%20%5CModules%5CMyModules%5CModules%20with%20the%20actual%20path%20where%20your%20modules%20live.%20Ensure%20that%20the%20fodler%20stucture%20is%20correct%20too.%20It%20is%20important%20that%20the%20Modules%20include%20a%20folder%20with%20the%20name%20of%20the%20actual%20module%20and%20then%20the%20module%20files%20need%20to%20exist%20in%20that%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20set%20%24path%20and%20%24env%3A%24PSModulePath%20inside%20your%20script%20(this%20is%20something%20that%20you%20can%20probably%20test%20by%20running%20it%20locally%20on%20your%20PC.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOnce%20the%20%24env%3APSModulePath%20is%20set%20correctly%20you%20will%20find%20that%20the%20modules%20are%20imported%20correctly%20(%20assuming%20that%20the%20module%20files%20are%20located%20in%20the%20corrct%20place.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20few%20years%20ago%20I%20wrote%20some%20posts%20about%20PowerShell%20Modules%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fveenstra.me.uk%2F2015%2F03%2F03%2Fpowershell-modules%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fveenstra.me.uk%2F2015%2F03%2F03%2Fpowershell-modules%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66690%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66690%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20reply%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20refrencing%20the%20path%20in%20the%20c%23%20code%20but%20it%20did%20not%20worked%3C%2FP%3E%3CPRE%3EInitialSessionState%20initialSession%20%3D%20InitialSessionState.CreateDefault()%3B%0AinitialSession.ImportPSModule(new%20string%5B%5D%20%7B%20%22D%3A%5C%5Chome%5C%5Csite%5C%5Cwwwroot%5C%5Capp_data%5C%5Cjobs%5C%5Ctriggered%5C%5CNMLSiteStorage%5C%5CMicrosoft.Online.SharePoint.PowerShell%22%20%7D)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethis%20is%20the%20path%20in%20azure%20web%20job%20where%20all%20dll's%20are%20and%20it%20also%20has%20the%20code%20dll%26nbsp%3B%3C%2FP%3E%3CP%3ED%3A%5C%5Chome%5C%5Csite%5C%5Cwwwroot%5C%5Capp_data%5C%5Cjobs%5C%5Ctriggered%5C%5CNMLSiteStorage%5C%5C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20please%20tell%20do%20you%20want%20me%20to%20add%20it%20before%20the%20script%20with%20the%20above%20path%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%26nbsp%3B%24path%20%3D%20Split-Path%20-parent%20%24MyInvocation.MyCommand.Definition%3C%2FPRE%3E%3CPRE%3Eif%20(%24env%3APSModulePath%20-notlike%20%22*%24path%5CModules%5CMyModules%5CModules*%22)%0A%7B%0A%20%20%20%20%20%20%24env%3APSModulePath%20%2B%3D%20%22%3B%24path%5CModules%5CMyModules%5CModules%22%0A%7D%3C%2FPRE%3E%3CPRE%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%20const%20string%20connectScript%20%3D%20%22Import-Module%20Microsoft.Online.SharePoint.PowerShell%20%5Cr%5Cn%20%24AdminUrl%20%3D%20%5C%22https%3A%2F%2Ftenant-admin.sharepoint.com%5C%22%20%5Cr%5Cn%20%24UserName%20%3D%20%5C%22user%40tenant.onmicrosoft.com%5C%22%20%5Cr%5Cn%20%24Password%20%3D%20%5C%22password%5C%22%20%5Cr%5Cn%20%24SecurePassword%20%3D%20%24Password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%20%5Cr%5Cn%20%24Credentials%20%3D%20New-Object%20-TypeName%20System.Management.Automation.PSCredential%20-argumentlist%20%24userName%2C%20%24SecurePassword%20%5Cr%5Cn%20Connect-SPOService%20-Url%20%24AdminUrl%20-Credential%20%24Credentials%20%5Cr%5Cn%20Get-SPOSite%20-Detailed%20-Limit%20All%20%7C%20select%20*%20%22%3B%3C%2FPRE%3E%3CP%3E%3CBR%20%2F%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66670%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66670%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F42008%22%20target%3D%22_blank%22%3E%40Kanwar%20Jot%20Singh%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20having%20to%20make%20some%20assumptions.%20But%20if%20your%20module%20files%20are%20in%20Azure%20then%20you%20migth%20find%20that%20they%20aren't%20in%20the%20location%20where%20you%20expect%20them%20to%20be.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20general%20I%20first%20get%20the%20path%20where%20I'm%20running%20my%20scripts%20form%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%26nbsp%3B%24path%20%3D%20Split-Path%20-parent%20%24MyInvocation.MyCommand.Definition%3C%2FPRE%3E%3CP%3EThen%20I%20set%20my%20module%20path%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Eif%20(%24env%3APSModulePath%20-notlike%20%22*%24path%5CModules%5CMyModules%5CModules*%22)%0A%7B%0A%20%20%20%20%20%20%24env%3APSModulePath%20%2B%3D%20%22%3B%24path%5CModules%5CMyModules%5CModules%22%0A%7D%3C%2FPRE%3E%3CP%3Eyou%20might%20find%20that%20when%20you%20build%20abnd%20publish%20your%20project%20the%20module%20files%20are%20found%20in%20(and%20therefore%20published%20to)%20the%20bin%2Fdebug%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66590%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66590%22%20slang%3D%22en-US%22%3EI%20have%20just%20deployed%20the%20project%20using%20publish%20to%20azure%20and%20in%20package%20file%20i%20can%20see%20reference%20to%20dll's.%20do%20i%20need%20to%20explicitly%20publish%20the%20dll%20to%20azure.%20can%20you%20tell%20me%20how%20can%20we%20do%20that.%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-66421%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%20script%20in%20C%23%20code%20under%20azure%20web%20job%20error%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-66421%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20shoudl%20try%20to%20set%20%24env%3APSModulePath%20it%20looks%20like%20your%20modules%20aren't%20being%20found.%20Did%20you%20publish%20the%20modules%20to%20your%20Azure%20environment%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

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?

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 @Kanwar Jot Singh,

 

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.

 

 

 

 

 

Thanks for the reply 

 

I tried refrencing the path in the c# code but it did not worked

InitialSessionState initialSession = InitialSessionState.CreateDefault();
initialSession.ImportPSModule(new string[] { "D:\\home\\site\\wwwroot\\app_data\\jobs\\triggered\\NMLSiteStorage\\Microsoft.Online.SharePoint.PowerShell" });

 

this is the path in azure web job where all dll's are and it also has the code dll 

D:\\home\\site\\wwwroot\\app_data\\jobs\\triggered\\NMLSiteStorage\\

 

Can you please tell do you want me to add it before the script with the above path 

 

 $path = Split-Path -parent $MyInvocation.MyCommand.Definition
if ($env:PSModulePath -notlike "*$path\Modules\MyModules\Modules*")
{
      $env:PSModulePath += ";$path\Modules\MyModules\Modules"
}


const string connectScript = "Import-Module Microsoft.Online.SharePoint.PowerShell \r\n $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 * ";


Thanks

You will need to replace \Modules\MyModules\Modules with the actual path where your modules live. Ensure that the fodler stucture is correct too. It is important that the Modules include a folder with the name of the actual module and then the module files need to exist in that folder.

 

 

I would set $path and $env:$PSModulePath inside your script (this is something that you can probably test by running it locally on your PC.)

 

Once the $env:PSModulePath is set correctly you will find that the modules are imported correctly ( assuming that the module files are located in the corrct place.

 

A few years ago I wrote some posts about PowerShell Modules:

 

https://veenstra.me.uk/2015/03/03/powershell-modules/

 

Thanks for the reply

 

I am not a very good at powershell all i am trying to do is call a powershell script from the C# code 

Connect-SPO commandlet is not my custom module or anything it is Sharepoint online management shell command which is just a package i installed on my machine to run it works fine on local.

 

I am passing the dll to azure

 

Path to code where all dll including the spo management shell dll is 

D:\\home\\site\\wwwroot\\app_data\\jobs\\triggered\\NMLSiteStorage\\

 

exe file is also in the same folder where it is executing the code

 

can you please tell the exact code how to set that variable


if ($env:PSModulePath -notlike "*$pathD:\\home\\site\\wwwroot\\app_data\\jobs\\triggered\\NMLSiteStorage\\*")
{
$env:PSModulePath += ";$path"
}

 

const string connectScript = " $path = Split-Path -parent $MyInvocation.MyCommand.Definition \r\n  Import-Module Microsoft.Online.SharePoint.PowerShell \r\n $AdminUrl = \"https://Newcrestmining-admin.sharepoint.com\" \r\n $UserName = \"kanwarjot.singh@newcrestmining.onmicrosoft.com\" \r\n $Password  = \"Crest99\" \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 * ";

Thanks,

KJ

Thanks for the reply

 

I am not a very good at powershell all i am trying to do is call a powershell script from the C# code 

Connect-SPO commandlet is not my custom module or anything it is Sharepoint online management shell command which is just a package i installed on my machine to run it works fine on local.

 

I am passing the dll to azure

 

Path to code where all dll including the spo management shell dll is 

D:\\home\\site\\wwwroot\\app_data\\jobs\\triggered\\NMLSiteStorage\\

 

exe file is also in the same folder where it is executing the code

 

can you please tell the exact code how to set that variable

if ($env:PSModulePath -notlike "*$pathD:\\home\\site\\wwwroot\\app_data\\jobs\\triggered\\NMLSiteStorage\\*")
{
$env:PSModulePath += ";$path"
}

 

const string connectScript = " $path = Split-Path -parent $MyInvocation.MyCommand.Definition \r\n  Import-Module Microsoft.Online.SharePoint.PowerShell \r\n $AdminUrl = \"https://Newcrestmining-admin.sharepoint.com\" \r\n $UserName = \"kanwarjot.singh@newcrestmining.onmicrosoft.com\" \r\n $Password  = \"Crest99\" \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 * ";

Thanks,

KJ

The issue will be that the Sharepoint online management shell is not installed within your Azure context.

 

By default the module is installed in:

 

C:\Program Files\SharePoint Online Management Shell\

 

So you will need to copy the files in C:\Program Files\SharePoint Online Management Shell  (this is a folder  called Microsoft.Online.SharePoint.PowerShell) including the contents of the folder to Azure.

 

Then include the location where you put the folder in your $env:PSModulePath and you should be up and running.

 

Thanks i will try to include and push that folder to azure. and then refere the module.

 

Not sure if i can do it.

 

Thanks for the help