Powershell脚本仅适用于显式参数声明
我编写了以下脚本,用于在SharePoint中签出项目:Powershell脚本仅适用于显式参数声明,powershell,sharepoint,Powershell,Sharepoint,我编写了以下脚本,用于在SharePoint中签出项目: #############################################################################################################################################################################################################################
#######################################################################################################################################################################################################################################################
# Check out - Checks out a file saved in the given directory
#######################################################################################################################################################################################################################################################
Param (
[Parameter(Mandatory=$true)]
[string]$site,
[Parameter(Mandatory=$true)]
[string]$listName,
[Parameter(Mandatory=$true)]
[string]$doc
)
Add-PSSnapin Microsoft.Sharepoint.Powershell
$spWeb = Get-SPWeb $site
$list = $spWeb.Lists |? {$_.Title -eq $listName}
ForEach ($item in $list.Items) {
$itemFile = $item.File
if($itemFile.CheckOutStatus -eq "none" -and $itemFile.Name -eq $doc) {
try {
$itemFile.CheckOut()
Write-Host "Item checked out" -BackgroundColor Green
} catch {Write-Host "Error when checking in item" -BackgroundColor Red}
}
}
在删除Param
块并显式声明参数后,我能够运行此脚本而不会出现任何问题。通过显式声明,我的意思是在脚本顶部设置$site=http://mysite/mypage
etc
但是,只要我添加了Param
块,脚本就不再工作了
我这样称呼它:
\\path\to\file\script.ps1-站点http://mysite/mypage -从powershell控制台列出“列表名称”-文档“文档名称”
控制台中没有输出,并且文件未签出
我已经使用相同的参数设置创建了其他脚本,这些工作正常,但由于某些原因,只有在脚本中声明参数时,此脚本才能工作。。。。任何建议都将不胜感激 “删除param块并显式声明参数”是什么意思?你需要一个param块来为脚本提供参数(不使用$args)。@MikeShepard他解释了他的意思(创建一个同名变量并为其赋值,而不是将其作为参数)@Bassie你能调试一下吗,看看在执行过程中你的参数值是什么?您可以在ISE中加载脚本,设置断点,然后在ISE控制台中使用参数调用脚本,断点将正常工作。然后,您可以使用控制台查看执行上下文中的变量,并且可以逐步查看代码以更好地了解发生了什么?在您的params声明中,它表示$listname,因此您应该使用-listname“My list name”调用您的脚本@Bassie在ISE中调试既快速又简单。但是,另一种调试方法是点源脚本:
\\path\to\file\script.ps1-站点http://mysite/mypage -列出“列表名称”-doc“doc名称”
,然后在脚本运行后查看脚本的变量<代码>$spweb、$list等。在脚本运行后都应该有值。您可以检查它们是否具有正确的值。基于这些值,您可以很好地了解脚本中发生了什么。另一种方法是:setpsdebug-trace1
,它将显示所有脚本执行的每一行,直到关闭为止。