PowerShell“;“无法解析参数集”;调用其他脚本时
目前,我正在努力使用Invoke Expression调用我的一个PowerShell脚本中的第二个脚本。它当前正在生成一个错误: “无法使用指定的命名参数解析参数集。” 令人恼火的是,它对一个交换机(being-ServerDriveReport)工作良好,但对另一个交换机不起作用 第一个脚本(称为DriveReport.ps1)类似于:PowerShell“;“无法解析参数集”;调用其他脚本时,powershell,parameters,Powershell,Parameters,目前,我正在努力使用Invoke Expression调用我的一个PowerShell脚本中的第二个脚本。它当前正在生成一个错误: “无法使用指定的命名参数解析参数集。” 令人恼火的是,它对一个交换机(being-ServerDriveReport)工作良好,但对另一个交换机不起作用 第一个脚本(称为DriveReport.ps1)类似于: [cmdletbinding()] Param( [Parameter(ParameterSetName="ServerDriveReport")]
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="ServerDriveReport")]
[switch]$ServerDriveReport,
[Parameter(ParameterSetName="VMDriveReport")]
[switch]$VMDriveReport)
If($ServerDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -ServerDriveReport"}
If($VMDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -VMDriveReport"}
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="ServerDriveReport")]
[switch]$ServerDriveReport,
[Parameter(ParameterSetName="VMDriveReport")]
[switch]$VMDriveReport)
If($ServerDriveReport){
# Send an email containing the server drive report}
If($VMDriveReport){
# Send an email contining the VM drive report}
“EmailDriveReport.ps1”脚本类似于:
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="ServerDriveReport")]
[switch]$ServerDriveReport,
[Parameter(ParameterSetName="VMDriveReport")]
[switch]$VMDriveReport)
If($ServerDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -ServerDriveReport"}
If($VMDriveReport){
Invoke-Expression "& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' -VMDriveReport"}
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="ServerDriveReport")]
[switch]$ServerDriveReport,
[Parameter(ParameterSetName="VMDriveReport")]
[switch]$VMDriveReport)
If($ServerDriveReport){
# Send an email containing the server drive report}
If($VMDriveReport){
# Send an email contining the VM drive report}
当运行“DriveReport.ps1-ServerDriveReport”时,一切正常。但是当运行“DriveReport.ps1-VMDriveReport”时,我会收到前面提到的错误消息
以前有人见过这个吗
任何帮助都将不胜感激如果没有试图解决您的即时问题(从发布的代码对我来说不是很明显),请考虑使用“自动<代码> $PSBONDATABORE 变量”,将参数传递给第二个脚本:
[cmdletbinding()]
Param(
[Parameter(ParameterSetName="ServerDriveReport")]
[switch]$ServerDriveReport,
[Parameter(ParameterSetName="VMDriveReport")]
[switch]$VMDriveReport)
)
& 'C:\Scripts\Drive Report\EmailDriveReport.ps1' @PSBoundParameters
一般来说,应该避免调用表达式,因为通常有更健壮的解决方案可用,而且如果在不受信任的字符串上调用,则会带来安全风险。谢谢您的帮助!
我仔细检查了脚本,发现其中一条Else语句错误地调用了文件,从而解决了这个问题。我现在将此更改为:
&建议使用“C:\Scripts\Drive Report\EmailDriveReport.ps1”