{"id":90,"date":"2012-09-11T16:19:28","date_gmt":"2012-09-11T21:19:28","guid":{"rendered":"http:\/\/www.johndhutton.com\/?p=90"},"modified":"2015-07-06T12:23:05","modified_gmt":"2015-07-06T17:23:05","slug":"sharepoint-2010-powershell-scripts-to-install-web-parts-and-features","status":"publish","type":"post","link":"https:\/\/www.johndhutton.com\/?p=90","title":{"rendered":"SharePoint 2010 PowerShell scripts to install Web Parts and Features"},"content":{"rendered":"<p>For development you can use\u00a0Visual Studio 2010 \u00a0features (right click deploy) to install web parts and features, but once you get into testing\/staging and production enviornemnts you can use the following script<\/p>\n<p><strong>Web Parts<\/strong><\/p>\n<p>POWERSHELL-SharePoint-WebPart.ps1<\/p>\n<pre class=\"prettyprint lang-cpp\">Add-PsSnapin Microsoft.SharePoint.PowerShell\r\n\r\nWrite-Host &#34;Initializing parameters&#34;\r\n$featureAction=$args&#91;3&#93;\r\n$CurrentDir=$args&#91;0&#93;\r\n$solutionName=$args&#91;1&#93; + &#34;.wsp&#34;\r\n$webpartName=$args&#91;1&#93;\r\n$SolutionPath=$CurrentDir + &#34;\\&#34; + $solutionName\r\n$logfile=$CurrentDir + &#34;\\&#34; + $webpartName + &#34;_&#34; + $featureAction + &#34;.log&#34;\r\n$urlPath=$args&#91;2&#93;\r\n$installToURL=$args&#91;4&#93;\r\n\r\nif (( $urlPath -eq $null) -or ( $installToURL -eq $null ))\r\n{\r\n    $installToURL=&#34;No&#34;\r\n}\r\n\r\nWrite-Host &#34;Logfile: &#34; $logfile &#34; Starting....&#34;\r\nStart-Transcript $logfile\r\n\r\nif ( $featureAction -eq &#34;Remove&#34;)\r\n{\r\n    Write-Host &#34;Rretracting and uninstalling solution:&#34; $solutionName\r\n    Uninstall-SPSolution -Identity $solutionName  -allwebapplications -Confirm:$false\r\n\r\n    Write-Host &#34;Waiting till retaction has finished &#34; -nonewline\r\n    do\r\n    {\r\n        Start-Sleep 2\r\n        Write-Host &#34;.&#34; -nonewline\r\n    }\r\n    while ((Get-SPSolution $solutionName).Deployed)\r\n\r\n    Start-Sleep 5\r\n    Write-Host &#34;.&#34; -nonewline\r\n\r\n    Write-Host &#34;Removing solution:&#34; $solutionName &#34; &#34;\r\n    Remove-SPSolution -Identity $solutionName -Confirm:$false\r\n\r\n    Write-Host $solutionName &#34; Now Removed!&#34;\r\n\r\n}\r\n\r\nif ( $featureAction -eq &#34;Install&#34;)\r\n{\r\n\r\n    Write-Host &#34;Adding solution:&#34; $solutionName &#34; &#34;\r\n    Add-SPSolution $SolutionPath \r\n\r\n    if ( $installToURL -ne &#34;Yes&#34; )\r\n    {\r\n        Write-Host &#34;Deploying solution:&#34; $solutionName &#34;to GAC &#34;\r\n        Install-SPSolution -Identity $solutionName  -AllWebApplications -GACDeployment -CASPolicies -Confirm:$false\r\n    }\r\n    else\r\n    {\r\n        Write-Host &#34;Deploying solution:&#34; $solutionName &#34;to&#34; $urlPath &#34;and GAC &#34;\r\n        Install-SPSolution -Identity $solutionName -WebApplication $urlPath -GACDeployment -Confirm:$false\r\n    }\r\n\r\n    Write-Host &#34;Waiting till deployment has finished &#34; -nonewline\r\n    do\r\n    {\r\n        Start-Sleep 2\r\n        Write-Host &#34;.&#34; -nonewline\r\n    }\r\n    while ((Get-SPSolution $solutionName).Deployed)\r\n\r\n    Start-Sleep 5\r\n    Write-Host &#34;.&#34; -nonewline\r\n\r\n    Write-Host $solutionName &#34; Now installed!&#34;\r\n\r\n}\r\n\r\nif ( $featureAction -eq &#34;Update&#34;)\r\n{\r\n    Write-Host &#34;Updating solution:&#34; $solutionName &#34; &#34;\r\n    Update-SPSolution -Identity $solutionName -LiteralPath $SolutionPath -GACDeployment -Confirm:$false\r\n    Write-Host $solutionName &#34; Now updated!&#34;\r\n}\r\n\r\nWrite-Host &#34;&#34; \r\n\r\nStop-Transcript\r\n\r\nRemove-PsSnapin Microsoft.SharePoint.PowerShell<\/pre>\n<pre>To use POWERSHELL-SharePoint-WebPart.ps1, create dos bat file called\r\nmywebpart-install.bat<\/pre>\n<pre class=\"prettyprint lang-cpp\">@echo off\r\nREM                                                    Current               Site Collection\r\nREM               Script to install webpart            Dir    webpart Name   to Install to       Action (Install, Update, Remove)\r\nREM              ----------------------------------    ------ -------------- ------------------- ------------\r\npowershell -file &#34;.\\POWERSHELL-SharePoint-WebPart.ps1&#34; &#34;%CD%&#34; &#34;mywebpart&#34; &#34;http:&#47;&#47;stage.company.com&#34; &#34;Install&#34;<\/pre>\n<p><strong>FEATURES<\/strong><\/p>\n<p>POWERSHELL-SharePoint-Feature.ps1<\/p>\n<pre class=\"prettyprint lang-cpp\">Add-PsSnapin Microsoft.SharePoint.PowerShell\r\n\r\nWrite-Host &#39;Will initialize parameters&#39;\r\n$CurrentDir=$args&#91;0&#93;\r\n$solutionName=$args&#91;1&#93; + &#34;.wsp&#34;\r\n$featureName=$args&#91;1&#93;\r\n$SolutionPath=$CurrentDir + &#34;\\&#34; + $solutionName\r\n$urlPath=$args&#91;2&#93;\r\n$action=$args&#91;3&#93;\r\n\r\n$logfile=$CurrentDir + &#34;\\&#34; + $args&#91;1&#93; + &#34;_&#34; + $action + &#34;.log&#34;\r\n\r\nWrite-Host &#34;Logfile: &#34; + $logfile + &#34; Starting....&#34;\r\nStart-Transcript $logfile\r\n\r\n$errorActionPreference = &#39;Inquire&#39;\r\nif ( $action -eq &#34;Remove&#34; )\r\n{\r\n\tWrite-Host $action &#39; &#39; $featureName &#39;....&#39;\r\n\r\n\tWrite-Host &#39;Will now disable feature, &#39; $featureName &#39; for all site in Site Collection in &#39; $urlPath\r\n\r\n\t# Get Feature object\r\n\t$feature = Get-SPFeature $featureName\r\n\r\n\t#Get web application\r\n\t$webApp = Get-SPWebApplication -Identity $urlPath\r\n\r\n\t#Deactivate site scoped feature for all site collections in a Web Application (checks to see if the feature is activated before attempting to deactivate)\r\n\tWrite-Host &#39;Will now deactivate solution:&#39; $solutionName\r\n\t$webApp | Get-SPSite -limit all | ForEach-Object {\r\n    \tWrite-Host &#39;...Checking Site &#39; $_.Url &#39; for &#39; $solutionName\r\n        if ($_.Features&#91;$feature.ID&#93;) {\r\n            Write-Host &#39;... ...deactiviatng feature &#39; $solutionName &#39; from &#39; $_.Url\r\n            Disable-SPFeature $feature -Url $_.Url -Force -Confirm:$false\r\n\t\t}\r\n\t}\r\n\r\n    $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}\r\n\r\n\tWrite-Host &#39;Will now retract solution:&#39; $solutionName\r\n\tUninstall-SPSolution -Identity $solutionName -Confirm:$false\r\n\r\n\t#Wait for solution to be uninstalled\r\n    while ($solution.Deployed -eq $true)\r\n    {\r\n        Write-Host &#39;.&#39;  -nonewline\r\n\t\tStart-Sleep -s 1\r\n    }\r\n    #make sure job is finished\r\n    while ( $solution.JobExists )\r\n    {\r\n        write-host &#39;.&#39;  -nonewline\r\n        sleep 1\r\n    }\r\n\twrite-host &#39;&#39;\r\n\tWrite-Host &#39;Will now remove solution:&#39; $solutionName\r\n\tRemove-SPSolution -Identity $solutionName -force -Confirm:$false\r\n}\r\n\r\nif ( $action -eq &#34;Install&#34;)\r\n{\r\n\r\n\tWrite-Host &#39;Will now add solution:&#39; $solutionName\r\n\tAdd-SPSolution -LiteralPath $SolutionPath \r\n\r\n\tWrite-Host &#39;Will now deploy solution:&#39; $solutionName\r\n\tInstall-SPSolution -Identity $solutionName -GACDeployment  -force\r\n\r\n    $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}\r\n\r\n    while ( $solution.JobExists )\r\n    {\r\n        write-host &#39;.&#39;  -nonewline\r\n        sleep 1\r\n    }\r\n\twrite-host &#39;&#39;\r\n    write-host &#39;Solution, &#39; $solutionName &#39;, &#39; $action &#39;!&#39;\r\n\r\n}\r\n\r\nif ( $action -eq &#34;Update&#34;)\r\n{\r\n\tWrite-Host &#39;Will now update solution:&#39; $solutionName\r\n\tUpdate-SPSolution -Identity $solutionName -LiteralPath $SolutionPath -GACDeployment  -force\r\n\r\n    $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}\r\n\r\n    while ( $solution.JobExists )\r\n    {\r\n        write-host &#39;.&#39;  -nonewline\r\n        sleep 1\r\n    }\r\n\twrite-host &#39;&#39;\r\n    write-host &#39;Solution, &#39; $solutionName &#39;, &#39; $action &#39;!&#39;\r\n}\r\n\r\n#place bland line to fix log format\r\nwrite-host &#39; &#39;\r\n\r\n#Stop Log\r\nStop-Transcript\r\n\r\n#fix output file so it has newlines in notepad\r\n&#91;string&#93;::Join(&#34;`r`n&#34;,(Get-Content $logfile)) | Out-File $logfile\r\n\r\nRemove-PsSnapin Microsoft.SharePoint.PowerShell\r\n<\/pre>\n<p>To use POWERSHELL-SharePoint-Feature.ps1, create dos bat file called<br \/>\n<b>myFeature-install.bat<\/b><\/p>\n<pre class=\"prettyprint lang-cpp\">@echo off\r\nREM                                                    Current               Site Collection\r\nREM               Script to install feature            Dir    feature Name   to Install to       Action (Install, Update, Remove)\r\nREM              ----------------------------------    ------ -------------- ------------------- ------------\r\npowershell -file &#34;.\\POWERSHELL-SharePoint-WebPart.ps1&#34; &#34;%CD%&#34; &#34;myfeature&#34; &#34;http:&#47;&#47;stage.company.com&#34; &#34;Install&#34;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For development you can use\u00a0Visual Studio 2010 \u00a0features (right click deploy) to install web parts and features, but once you get into testing\/staging and production enviornemnts you can use the following script Web Parts POWERSHELL-SharePoint-WebPart.ps1 Add-PsSnapin Microsoft.SharePoint.PowerShell Write-Host &#34;Initializing parameters&#34; $featureAction=$args&#91;3&#93; $CurrentDir=$args&#91;0&#93; $solutionName=$args&#91;1&#93; + &#34;.wsp&#34; $webpartName=$args&#91;1&#93; $SolutionPath=$CurrentDir + &#34;\\&#34; + &hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,9],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-sharepoint","category-sharepoint-2010","category-sharepoint-2013"],"_links":{"self":[{"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":14,"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johndhutton.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}