如何在windows计划中调用备份sharepoint2010网站集的powershell脚本

最近有个项目需要在在windows计划中使用powershell脚本备份sharepoint2010网站集,打开sharepoint的powershell执行命令管理界面的属性

image

image

查看:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -NoExit " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "

 

先看看PowerShell.exe 的帮助

PowerShell.exe 控制台帮助 从另一工具(如 Cmd.exe)的命令行启动 Windows PowerShell。 语法 PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>] [-EncodedCommand <Base64EncodedCommand>] [-ExecutionPolicy <ExecutionPolicy>] [-File <filePath> <args>] [-InputFormat {Text | XML}] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile] [-OutputFormat {Text | XML}] [-Sta] [-WindowStyle <style>] [-Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] } ] PowerShell[.exe] -Help | -? | /? 参数 -EncodedCommand 接受命令的 Base 64 编码字符串版本。使用此参数可向 Windows PowerShell 提交需要复杂引号或大括号的命令。 -ExecutionPolicy 设置会话的默认执行策略。此参数不会更改在注册表中设置的 Windows PowerShell 执行策略。 -File 运行指定的脚本。请输入脚本文件路径和一些参数。 -InputFormat 描述发送至 Windows PowerShell 的数据格式。有效值为"Text"(文本字符串)或"XML"(序列化 CLIXML 格式)。 -NoExit 运行完启动命令后不退出。 -NoLogo 启动时隐藏版权标志。 -NonInteractive 不向用户显示交互式提示。 -NoProfile 不加载 Windows PowerShell 配置文件。 -OutputFormat 确定 Windows PowerShell 的输出格式。有效值为"Text"(文本字符串)或"XML"(序列化 CLIXML 格式)。 -PSConsoleFile 加载指定的 Windows PowerShell 控制台文件。若要创建控制台文件,请使用 Windows PowerShell 中的 Export-Console cmdlet。 -Sta 使用单线程单元启动 shell。 -Version 启动 Windows PowerShell 的指定版本。请随该参数输入一个版本号,例如"-version 1.0"。 -WindowStyle 将窗口样式设置为 Normal、Minimized、Maximized 或 Hidden。 -Command 按照执行在 Windows PowerShell 命令提示符下键入的命令那样,执行指定的命令和所有参数;如果未指定 NoExit,则执行完命令后将退出。Command 的值可以是"-"、字符串或脚本块。 如果 Command 的值为"-",则从标准输入中读取命令文本。 脚本块必须括在大括号 ({}) 中。只有在 Windows PowerShell 中运行 PowerShell.exe 时才能指定脚本块。脚本的运行结果将作为反序列化 XML 对象(而非活动对象)返回父 shell。 如果 Command 的值为字符串,则 Command 必须是该命令的最后一个形式参数,因为其后键入的所有字符都会被解释为它的实际参数。 若要编写运行 Windows PowerShell 命令的字符串,请使用以下格式: "& {<command>}" 其中,引号指示一个字符串,调用运算符 (&) 用于执行命令。 -Help, -?, /? 显示此消息。如果要在 Windows PowerShell 中键入 PowerShell.exe 命令,请将连字符 (-) 作为命令参数的前缀,而不要使用正斜杠 (/)。在 Cmd.exe 中,既可以使用连字符,也可以使用正斜杠。 示例 PowerShell -PSConsoleFile sqlsnapin.psc1 PowerShell -version 1.0 -NoLogo -InputFormat text -OutputFormat XML PowerShell -Command {Get-EventLog -LogName security} PowerShell -Command "& {Get-EventLog -LogName security}" # To use the -EncodedCommand parameter: $command = "dir 'c:\program files' " $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) powershell.exe -encodedCommand $encodedCommand

 

了解如上信息。我们来编写用powershell备份网站集的脚本。代码如下:

 

#//*************************************************************
#//编辑人:XXX
#//编辑单位:XXX
#//编辑作用:备份所有站点,按照周1到周日创建7个文件夹
#//编制时间:2013.08.26
#//*************************************************************
#**************************************************默认C#盘下,需要改动如下参数
$Location = "d:\"
#网站集URL
$siteUrl=http://XX.XXX.com
#**************************************************
#根目录名称
$RootName="EPBackUp"
#子文件夹
$folderName = (Get-Date).DayOfWeek
#全路径
$folderPath = $Location + "\" + $RootName
#如果根文件夹不存在。则创建根文件夹
If((Test-Path $Location) -eq $False) {
    Write-Host "开始创建根文件夹...---------------"
    New-Item -path $Location -name $RootName -itemType "directory"
    Write-Host "创建根文件夹完毕...---------------"
}


#如果星期文件夹不存在,则创建星期文件夹
$weekPath =$folderPath + "\" + $folderName
If((Test-Path $weekPath) -eq $False) {
    Write-Host "正在创建周期文件夹......."
    New-Item -path $folderPath -name $folderName -itemType "directory"
    Write-Host "创建周期文件夹完毕...---------------"
}


#******************************************************************
#添加如下2行代码为windows计划执行使用,否则windows计划无法执行ps1
$MySnapin="Microsoft.SharePoint.PowerShell"
if ( (Get-PSSnapin -Name $MySnapin -ErrorAction SilentlyContinue) -eq $null )
{
    Write-Host "正在导入sharepoint的commandlet...---------------"
    Add-PSSnapin $MySnapin
    Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force
    Write-Host "导入的sharepoint的commandlet完毕...---------------"
}
#******************************************************************
#整个文件夹全路径
$BackupFilePath = $folderPath + "\" + $folderName

#公司门户文件夹
$epName = $BackupFilePath + "\SPSite_EP.bak"
Write-Host "开始备份公司门户...---------------"
backup-SPSite $siteUrl -Path $epName -force
Write-Host "备份公司门户完毕...---------------"

#学习与发展门户文件夹
$KMSiteUrl = $siteUrl +"/km"
$KMName = $BackupFilePath + "\SPSite_KM.bak"
Write-Host "开始备份学习与发展...---------------"
backup-SPSite $KMSiteUrl -Path $KMName -force
Write-Host "备份学习与发展完毕...---------------"

#协作空间文件夹
$WorkSiteUrl = $siteUrl +"/workspace"
$WorkName = $BackupFilePath + "\SPSite_WorkSpace.bak"
Write-Host "开始备份协作空间...---------------"
backup-SPSite $WorkSiteUrl -Path $WorkName -force
Write-Host "备份协作空间完毕...---------------"


说明:

Add-PSSnapin, alias 为 asnp
这个没有什么难理解的地方。Java里面有import来导入类库,同样C++里面有using
来导入库。在powershell
里面有add-pssnapin来导入内置之外的commandlets。比如你自己写了一个commandlet用来计算及验证MD5摘要,怎么在console下用你自己写的commandlet呢(现在还没有内置的算MD5的commandlet,只能用.NET里面的库)?让add-pssnapin来帮助你。
语法
Add-PSSnapin [-name] <string[]> [-passThru]
[<CommonParameters>]
例子1
add-PSSnapIn

Microsoft.Exchange,Microsoft.Windows.AD
将Microsoft.Exchange
和Microsoft.Windows.AD 
snappin添加到当前console中去。
这两个snappin中的所有类,函数只在当前console中有效。如果想要当前console的snapin在后面可用,请看后面的export-console说明.
例子2
get-pssnapin

-registered | add-pssnapin -passthru
将所有注册过的snapin加入到当前会话中去。

创建windows计划

注意点:

#******************************************************************
#添加如下2行代码为windows计划执行使用,否则windows计划无法执行ps1
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force
#******************************************************************

创建基本任务

image

image

image

image

image

1)、程序脚本:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

2)、添加参数:-NonInteractive  "c:\BackUp.ps1"

image

image

 

我们测试下效果图:

image

 

image

 

posted @ 2013-08-26 16:18  love007  阅读(515)  评论(0编辑  收藏  举报