[英]Parameter equals none if not specified powershell
I am trying to pass various parameters to a PowerShell script, however if no value is passed that script exits with an error. 我试图将各种参数传递给PowerShell脚本,但是如果没有传递任何值,那么脚本会以错误退出。 For example:
例如:
.powershellscript.ps1 -path C:\test -Name text.txt -Date 20111212
Works fine, however if I execute: 工作正常,但是如果我执行:
.powershellscript.ps1 -path C:\test -Name text.txt -Date
I receive the following error 我收到以下错误
"Missing an argument for parameter 'Date'. Specify a parameter of type 'System.Object' and try again."
“缺少参数'Date'的参数。指定'System.Object'类型的参数,然后重试。”
I want to be able to assign the Date value to $Null if it's not passed through the script. 我希望能够将Date值分配给$ Null,如果它没有通过脚本传递的话。
Inside the .powershellscript.ps1, the parameters is handled as: 在.powershellscript.ps1中,参数处理为:
param(
$path,
$Name,
$Date
)
Any help would be greatly appreciated. 任何帮助将不胜感激。
If you don't want to pass a value for $Date
call the script like this: 如果您不想传递
$Date
的值,请调用以下脚本:
.powershellscript.ps1 -path C:\test -Name text.txt
$Date
should then be $null
in your script. 然后,
$Date
应该是脚本中的$null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.