在 Visual Studio 2012 中处理 SharePoint 2010 解决方案时,我想运行 PowerShell 脚本以从我的列表之一中删除查找字段,从而允许 Visual Studio 通过删除现有列表并将其替换为自动解决脚本部署冲突我的解决方案中的一个。
但是,在 Visual Studio 中从项目的“部署前命令行”运行 PowerShell 时,当我的脚本尝试使用 时get-spweb
,PowerShell 报告找不到该对象。在 Visual Studio 的输出窗口中向上滚动,我看到它Add-PsSnapin Microsoft.SharePoint.PowerShell
报告了各种问题:
(注意:实际的错误消息具有扩展$(ProjectDir)
值而不是“$(ProjectDir)”作为文本。尝试各种级别的间接以确保我使用的是正确的 64 位版本的 PowerShell 不会更改此工作目录,也不会在调用我的脚本之前使用cd
或set-location
命令对此目录有任何影响。我想知道无效的工作目录是否是问题的一部分......)
The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.
Could not read the XML Configuration file in the folder CONFIG\PowerShell\Registration\.
Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\Registration'.
No xml configuration files loaded.
Unable to register core product cmdlets.
Could not read the Types files in the folder CONFIG\PowerShell\types\.
Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\types'.
"No Types files Found."
Could not read the Format file in the folder CONFIG\PowerShell\format\.
Could not find a part of the path '$(ProjectDir)\CONFIG\PowerShell\format'.
No Format files Found.
直接从命令提示符窗口运行 PowerShell 脚本工作正常,SharePoint 管理单元正确加载,但从 Visual Studio 运行总是失败。以前的研究表明 SharePoint 和 SQL Server 权限存在潜在问题,但我的帐户在这两个权限中都具有完全管理员权限。Visual Studio 正在“以管理员身份”运行。研究还发现了 32 位与 64 位可能存在的问题。我的部署前命令行现在调用%comspec% /c
以确保 64 位。
部署前命令行:
%comspec% /c ""$(ProjectDir)PowerShell Scripts\predeployment-command.cmd" "$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)""
*.cmd 文件:
echo off
rem pre-deployment script
rem call from pre-deployment command line like
rem %comspec% /c ""$(ProjectDir)PowerShell Scripts\predeployment-command.cmd "$(SharePointSiteUrl)" "$(ConfigurationName)" "$(ProjectDir)""
rem
echo Running "predeployment-command.cmd" file
echo
echo %%1 = %~1
echo %%2 = %~2
echo %%3 = %~3
echo
cd "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14"
cd
echo on
powershell.exe -file "%~3PowerShell Scripts\predeployment-script.ps1" -targetWeb "%~1" -CONFIG "%~2"
PowerShell 脚本文件:
<#
.SYNOPSIS
Pre-deployment script for Visual Studio development deployments
add command to run this script in the project's pre-deployment command-line box
.EXAMPLE
powershell .\dev-predeployment-script.ps1
#>
param(
[string] $targetWeb = $(throw "Please specify the site to which Visual Studio is deploying!"),
[string] $CONFIG = $(throw "Please specify the active configuration!")
)
write-host "Running Pre-Deployment PowerShell Script";
# Ensure SharePoint extensions are loaded
$snapin = $(Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue");
if($snapin -eq $null) {
Add-PsSnapin Microsoft.SharePoint.PowerShell;
}
$ErrorActionPreference = 'Stop';
#echo back parameter values:
write-host "TargetWeb = $targetWeb"
write-host "Configuration = $CONFIG"
#get web-site
$web = get-spweb $targetWeb;
if($web -ne $null) {
$recipients = $web.lists["Recipients"];
if($recipients -ne $null) {
$lookupField = $recipients.Fields["Request ID"];
if($lookupField -ne $null) {
$recipients.fields.remove($lookupField);
}
}
}
我正在 64 位 Windows Server 2008 r2 虚拟机上进行开发。