简体   繁体   English

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

[英]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.

相关问题 Powershell 等于日期不等于 - Powershell equals dates is not equals 如果在调用函数时没有指定布尔参数,那么在Powershell中是否有一种方法可以自动为真? - Is there a way in Powershell for a boolean parameter to automatically be true if not specified when the function is called? 运行其参数值在Powershell中指定为变量的可执行文件 - Running an executable whose parameter values are specified as variables in powershell 如何确定是否指定了PowerShell Cmdlet参数值? - How do I determine if a PowerShell Cmdlet parameter value was specified? 为什么在PowerShell中等于“ -eq”而不仅仅是“ =”? - Why is equals “-eq” in PowerShell and not just “=”? Powershell Remove-WmiObject:指定的参数超出了有效值的范围。 参数名称:路径 - Powershell Remove-WmiObject: Specified argument was out of the range of valid values. Parameter name: path Powershell:无法将“System.Object[]”转换为参数“Name”所需的类型“System.String”。 不支持指定的方法 - Powershell: Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported 未指定Azure PowerShell证书 - Azure PowerShell certificate not specified Powershell代码片段同时等于true和false - powershell snippet equals true and false at the same time PowerShell If 语句:IF 等于多个特定文本 - PowerShell If Statements: IF equals multiple specific Texts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM