Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果未指定powershell,则参数等于none_Powershell_Parameters - Fatal编程技术网

如果未指定powershell,则参数等于none

如果未指定powershell,则参数等于none,powershell,parameters,Powershell,Parameters,我试图将各种参数传递给PowerShell脚本,但是,如果未传递任何值,脚本将退出并出错。例如: .powershellscript.ps1 -path C:\test -Name text.txt -Date 20111212 工作正常,但如果我执行: .powershellscript.ps1 -path C:\test -Name text.txt -Date 我收到以下错误 缺少参数“Date”的参数。请指定一个参数 类型为“System.Object”,请重试。“ 如果未通过脚本传

我试图将各种参数传递给PowerShell脚本,但是,如果未传递任何值,脚本将退出并出错。例如:

.powershellscript.ps1 -path C:\test -Name text.txt -Date 20111212
工作正常,但如果我执行:

.powershellscript.ps1 -path C:\test -Name text.txt -Date
我收到以下错误

缺少参数“Date”的参数。请指定一个参数 类型为“System.Object”,请重试。“

如果未通过脚本传递,我希望能够将日期值分配给$Null

在.powershellscript.ps1中,参数的处理方式如下:

param(
    $path,
    $Name,
    $Date
)

非常感谢您的帮助。

如果您不想传递
$Date
的值,请按如下方式调用脚本:

.powershellscript.ps1 -path C:\test -Name text.txt

$Date
在脚本中应该是
$null

是否有办法保留-Date参数?
.powershellscript.ps1-路径C:\test-Name text.txt-Date$null
。如果您在其中有-Date,那么PowerShell将希望您为其提供一个参数。因此,要么错过它,要么传入
$null