0

I have a script in which I add a solution and then install it. The script runs fine and the solution is added and deployed, and it looks like this:

param([string]$LiteralPath)

if([string]::IsNullOrEmpty($LiteralPath)){
    Write-Error "ERROR: Please include the path to the WSP file"
    return
}

Add-SPSolution -LiteralPath $LiteralPath -Confirm:$false
Install-SPSolution -Identity {GUID} -GACDeployment -AllWebApplications
Write-Host "Solution added and deployed"

However because the Add-SPSolution command outputs right after it finishes, I get the following output:

Name                           SolutionId                           Deployed
----                           ----------                           --------
{SOLUTION NAME}                {GUID}                               False
Solution added and deployed

Although there's a 2nd line saying the solution is deployed, I want to disable the output from Add-SPSolution to not induce the user in error. How can I do it?

EDIT I was unaware that we could store the output from Add-SPSolution in a variable, which the OP from the supposed duplicate answer was aware of, so I don't think my question classifies as duplicate.

Community
  • 1
  • 1
Pedro Gordo
  • 1,825
  • 3
  • 21
  • 45
  • 1
    Try [the](http://stackoverflow.com/a/18782528/5806659)[se](http://stackoverflow.com/a/8985313/5806659). – K L Feb 15 '16 at 15:40
  • Possible duplicate of [Redirecting output to $null in PowerShell, but ensuring the variable remains set](http://stackoverflow.com/questions/5881174/redirecting-output-to-null-in-powershell-but-ensuring-the-variable-remains-set) – K L Feb 15 '16 at 15:47

2 Answers2

3

The best method would be to use Out-Null which will load the solution but delete the unnecessary output. The desired syntax will be:

Add-SPSolution -LiteralPath $LiteralPath -Confirm:$false | Out-Null

MysticHeroes
  • 164
  • 5
1

If you assign this to a variable, PowerShell won't output / return the result directly in your console.

Example:

$solution = Add-SPSolution -LiteralPath $LiteralPath -Confirm:$false
$install = Install-SPSolution -Identity {GUID} -GACDeployment -AllWebApplications; 

The return of the cmdlets will then be assigned to your variables where you can access them later if you need it.

Harald F.
  • 4,505
  • 24
  • 29
  • It works, thanks! By the way, where can I find the API for the object that is returned into the `$solution` variable? I would like to see the description for the methods I have access to. – Pedro Gordo Feb 15 '16 at 16:00
  • It's the SPSolution object (https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spsolution.aspx). You can at any time pipe the object to the Get-member cmdlet too see what variables and functions are available on the object, if you don't want to look up the API or can't find a the api. Example: $solution | Get-Member – Harald F. Feb 15 '16 at 16:03