I have powershell script which will create the WebApplication and SiteCollection and upload the solution. Creation of WebApplication and SiteCollection for user X works fine but Add-SpUserSolution is failing with UnauthorizedAccessException. Powershell process is running with credentials of user X where as logged in user is Y.
Both user X and Y are farm admin and site collection admin as well as System admin (Db admin too)
What is the possible reason for this exception ?
Regards Jeez
Edit
As suggested by Anders i tried with start-process by invoking SetUpSpApp.ps1 as below
ExecSetup.ps1 (Machine A) start-process powershell C:\SetUpSpApp.ps1 -Credential $USerXCred SetUpSpApp.ps1 (Machine A) function SetUp() { #creation of WebApp using New-SPWebApplication goes here ... $site = New-SPSite $Url -OwnerAlias $OwnerLogin -Name $Name -Template $Template -Language $Language #other stuff .. } i tried invoking ExecSetup.ps1 using invoke-command from machine B ( logged in user Y)
invoke-command -ScriptBlock { C:\ExecSetup.ps1 } -Computer "hostname" -Credential $UserXscred fails with Access denied exception
invoke-command -ScriptBlock { C:\SetUp.ps1 } -Computer "hostname" -Credential $UserXscred script executes but fails when control reaches the Command New-SPSite with exception User not found exception
Same thing happened when I tried invoking scripts using WMI
ConnectionOptions options = new ConnectionOptions(); options.Impersonation = ImpersonationLevel.Impersonate; options.Username = "userX"; options.Password = "passwd"; options.EnablePrivileges = true; ManagementScope scope = new ManagementScope( "\\\\MacineA\\root\\cimv2", options); scope.Connect(); ObjectGetOptions objectGetOptions = new ObjectGetOptions(); ManagementPath managementPath = new ManagementPath("Win32_Process"); ManagementClass processClass = new ManagementClass (scope, managementPath, objectGetOptions); ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); // inParams["CommandLine"] = @"powershell.exe c:\ExecSetup.ps1"; inParams["CommandLine"] = ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null); Console.WriteLine("Creation of the process returned: " + outParams["returnValue"]); uint pid = (uint) outParams["processId"];