PowerShell实现基于SharePoint的网站HomePage Auto-Upgrade Solution

*** Solution Auto-Upgrade Solution

 PS:该项目为公司项目,我还是给他的名字屏蔽掉吧,这是我用PowerShell写的一个自动化升级工具,此为三部自动化工具的第一部。主要是用PowerShell自动完成下载,解压缩一直到部署solution到SharePoint这一过程。

​*** Solution Auto-Upgrade Script

​Home Page Module Demo:

注:目前只支持HomePage Module,如果修改里面的可替换链接和字符串分析和路径分析的话将可以用在***所有部署Solution的模块,以下是文档内容。

User Guide:

1. Right click the script and run the script with PowerShell.

2. Input the package address and press the "Enter".

3. "Save as" the file under the path "C:\ HomePageSolution" (already been created automatically). 

Result:

The solution will be upgraded automatically in the SharePoint Farm.

Script: 

<#ftp function reference#>

Function Invoke-FtpLogin{

Param(

[parameter(Mandatory = $true)]

      [string]$Site = "ftp://10.1.4.5/Monetary_Authority_of_Singapore/HomePageSolution/DailyBuild",

      [string]$User = "Anonymous",

      [string]$Pass = "",

      [int]$Port=21,

      [int]$TimeOut=3000,

      [int]$ReadWriteTimeout=10000

)

Write-Host "Get FTP site dir listing..."

# Do directory listing

$FTPreq = [System.Net.FtpWebRequest]::Create($Site)

$FTPreq.Timeout = $TimeOut                          # msec (default is infinite)

$FTPreq.ReadWriteTimeout = $ReadWriteTimeout        # msec (default is 300,000 - 5 mins)

$FTPreq.KeepAlive = $false                          # (default is enabled)

$FTPreq.Credentials = New-Object System.Net.NetworkCredential($User,$Pass)

$FTPreq.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory

try

{

    $FTPres = $FTPreq.GetResponse()

    Write-Host "$User _ $Pass OK"

    $success = $true

#Write-Host $FTPres.StatusCode -nonewline

#Write-Host $FTPres.StatusDescription 

$FTPres.Close()

}

catch

{

   Write-Host "FAILED: $_"

   $success = $false

}

}

 

<#Unzip function reference#>

Function Unzip-File()

{

    param([string]$ZipFile,[string]$TargetFolder)

    #Make sure the file must be existed.

    if(!(Test-Path $TargetFolder))

    {

        mkdir $TargetFolder

    }

    $shellApp = New-Object -ComObject Shell.Application

    $files = $shellApp.NameSpace($ZipFile).Items()

    $shellApp.NameSpace($TargetFolder).CopyHere($files)

}

 

<#HomePage Solution Auto-Update#>

#If not admin, open the ps1 with admin again.

$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()

$currentWp = [Security.Principal.WindowsPrincipal]$currentWi

if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))

{

 $oldPid = $pid

 $currentFile = $MyInvocation.MyCommand.Path

 Start-Process "$psHome\powershell.exe" -ArgumentList $currentFile -verb runas

 kill $oldPid

}

#Admin runs the script.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

New-Item c:\HomePageSolution -ItemType directory -ErrorAction SilentlyContinue

$ie = new-object -com InternetExplorer.Application

<#User input the newest package address#>

$packageURL = Read-Host "Please input the URL of the HomePage package"

$timeURL = $packageURL.substring(75,10)

$pieces = $timeURL -Split "-"

$cake = -join $pieces

$fileURL = "APPS***HomePageSolution_1.0.0.6_" + $cake +".zip"

$downloadURL = $packageURL + "/" + $fileURL

$ie.Navigate2($downloadURL)

$ie.Visible = $true

<#Wait until the download has been finished.#>

do

{

 Start-Sleep -m 3000

}

until(Get-ChildItem c:\HomePageSolution|where{$_.name -like $fileURL})

<#Unzip the package after the download has been finished.#>

$UnzipPath = "c:\HomePageSolution\" + $fileURL

$goalPath = "c:\HomePageSolution\" + "APPS***HomePageSolution_1.0.0.6_" + $cake

Unzip-File -ZipFile $UnzipPath.tostring() -TargetFolder $goalPath.tostring()

$updatePath = $goalPath + "\" + "APPS***HomePage.wsp"

Update-SPSolution -Identity APPS***HomePage.wsp -LiteralPath $updatePath​ -GACDeployment

Read-Host "Press any key to quit"

 

posted @ 2015-01-15 10:40  天外归云  阅读(393)  评论(0编辑  收藏  举报