powershellwsp

How to execute powershell script from server1 for deploying wsp in server2


I am trying to remotely deploy wsp file present in server2 by running a powershell script in server1.

I am able to successfully log in to the server2 through server1 using the below command:

$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("username",$password)

but I am not able to deploy the wsp file. This is the code that I tried:

Enter-PSSession -ComputerName server2 -Credential $cred
Add-PSSnapin Microsoft.Sharepoint.Powershell –EA 0
Update-SPSolution -Identity TechSoup.Web.wsp -LiteralPath "C:\Program Files ...Debug\Some.wsp" -GacDeployment

I have also tried to put the above code in a script, save it and run the script remotely.

This is the error that I am getting. I believe it is because I don't have admin privileges, I can say this because when I run the deployment code from server2 as admin, the wsp file is getting deployed. So, how can I get admin privileges remotely. The user has the admin privileges, all I need to do is run it with elevated privileges(like right-click and run as admin, but programatically)

Update-SPSolution : Cannot access the local farm. Verify that the local farm is properly configured, currently available, and that you have the appropriate permissions to access the database before trying again

EDIT

I have tried the below script code in admin mode in powershell:

$password = ConvertTo-SecureString "serverpassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("userName",$password)
Enable-PSRemoting
Enable-WSmanCredSSP -Role Server
winrm set winrm/config/winrs '@{MaxShellsPerUser="25"}'
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="600"}'
Enter-PSSession -ComputerName Server2 -Credential $cred  -Authentication credssp

However, I keep getting this error:

Enter-PSSession : Connecting to remote server Server2 failed with the following error message : The WinRM client cannot process the request. CredSSP authentication is currently disabled in the client configuration. Change the client configuration and try the request again. CredSSP authentication must also be enabled in the server configuration. Also, Group Policy must be edited to allow credential delegation to the target computer. Use gpedit.msc and look at the following policy: Computer Configuration -> Administrative Templates -> System -> Credentials Delegation -> Allow Delegating Fresh Credentials. Verify that it is enabled and configured with an SPN appropriate for the target computer. For example, for a target computer name "myserver.domain.com", the SPN can be one of the following: WSMAN/myserver.domain.com or WSMAN/*.domain.com For more information, see the about_Remote_Troubleshooting Help topic

No matter what I try, I get this error. I have tried these techniques:

Can anyone suggest any thing ??


Solution

  • In order to run SharePoint commands remotely please follow the steps outlined in: Remote PowerShell to Manage SharePoint on-premises

    Essentially, after enabling remoting, you have to enable CredSSP access in order for your credentials to be sent to the remote and local computer in order for you to run elevated commands.

    On the server:

    Enable-PSRemoting
    Enable-WSmanCredSSP -Role Server
    winrm set winrm/config/winrs '@{MaxShellsPerUser="25"}'
    winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="600"}'
    

    And on the client:

    Enable-PSRemoting
    Enable-WSmanCredSSP -Role Client -DelegateComputer "server2.contoso.com"
    

    Then on the client you can enter the session:

    Enter-PSSession -ComputerName server2 -Credential $cred  -Authentication Credssp