在 Powershell 中请求参数的正确方法

Correct Way To Request Parameters In Powershell

我正在尝试找出在不使用函数的情况下在 PowerShell 脚本中请求参数的正确方法。使用以下示例脚本,如果我不在函数中包含参数,我会收到错误消息。

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

function SomeFunctionName
{

Param(
[Parameter(Mandatory=$true)]
[string]$CollectionUrl,
[Parameter(Mandatory=$true)]
[string]$SourceList,
[Parameter(Mandatory=$true)]
[string]$DestList,
[Parameter(Mandatory=$true)]
[string]$ExpireDays
) # END PARAMS


#DO SOMETHING WITH THE PARAMETERS

}

如果我删除 "function" 和周围的括号,只是尝试直接在脚本中请求参数,我会收到以下错误:

Missing closing ')' in expression.

您需要将 param(...) 块放在脚本顶部 If/Add-PSSnapin 之前。参数前可以有注释,其他脚本不能有。