gpt4 book ai didi

powershell - 在powershell中传递空日期时间参数

转载 作者:行者123 更新时间:2023-12-03 00:59:11 25 4
gpt4 key购买 nike

所以我有一个 powershell 脚本,它将区域和日期时间作为输入,并在脚本内部调用存储过程。

存储过程需要两个输入 - 区域和日期时间戳;日期时间戳始终为空。我该如何解析它?

function Reset {

param ([string]$region,[nullable[Datetime]]$statustimestamp)

$conn = New-Object System.Data.SqlClient.SqlConnection("Server=SQL,15010; Database='STG';User ID=SVC;Password=password;Integrated Security=FALSE")
$conn.Open()
$cmd = $conn.CreateCommand()
$cmd.CommandText = "dbo.Reset'$region' ,'$statustimestamp'"
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
$dataset = New-Object System.Data.DataSet
[void]$adapter.Fill($dataset)
$dataset.tables[0]
$cmd.CommandText
$dataset.Tables[0] | Export-CSV M:\MyReport.csv -encoding UTF8 -NoTypeInformation
Write-Host 'New report M:\MyReport.csv has been successfully generated'
}

我将其执行为

Rest -region IN -statustimestamp NULL

我收到以下错误

Reset : Cannot process argument transformation on parameter 'statustimestamp'. Cannot convert value "NULL" to type "System.DateTime".
Error: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.
At line:1 char:59
+ Reset -region AU -statustimestamp NULL
+ ~~~~
+ CategoryInfo : InvalidData: (:) [Reset], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Reset

最佳答案

补充dee-see's helpful answer :

注意:但是您的参数已声明,$PSBoundParameters.ContainsKey('<parameter-name>')脚本/函数内部告诉您参数是否显式传递给给定参数(默认值不算);例如,在您的问题中调用(如果成功),$PSBoundParameters.ContainsKey('statustimestamp')将指示$true .

如果您希望参数值为 $null 由于遗漏:

将您的参数简单地声明为 [Datetime] $statustimestamp并且在调用时不向其传递任何参数$statustimestamp那么将隐含$null .

# Declare a parameter in a script block (which works like a function)
# and invoke the script block without an argument for that parameter:
PS> & { param([Datetime] $statustimestamp) $null -eq $statustimestamp }

True # $statustimestamp was $null

如果您想支持显式传递 $null 作为参数:

如果您声明一个强制参数,但您希望允许$null,则这可能是必要的作为应使用默认值的明确信号。

不幸的是,参数声明的具体细节当前取决于参数的数据类型是引用类型(例如 [string][System.IO.FileInfo] )还是>值类型(例如 [int][datetime] )。
您可以检查给定类型的 .IsValueType属性来了解它是值类型( $true) 还是引用类型( $false );例如: [datetime].IsValueType 产生 $true )。

如果参数类型是引用类型,则可以使用[AllowNull()]属性:

PS> & { 
param(
[AllowNull()]
[Parameter(Mandatory)]
[System.IO.FileInfo] $Foo # System.IO.FileInfo is a *reference type*
)
$null -eq $Foo
} -Foo $null

True # $Foo was $null

不幸的是,相同的技术不适用于值类型,例如 [DateTime] ,因此您的参数确实必须键入 [Nullable[DateTime] ,如您的问题:

PS> & { 
param(
[Parameter(Mandatory)]
[AllowNull()] # Because the parameter is mandatory, this is *also* needed.
[Nullable[DateTime]] $Foo # System.DateTime is a *value type*
)
$null -eq $Foo
} -Foo $null

True # $Foo was $null

注意:这些要求——需要注意值类型和引用类型之间的区别并且需要使用[Nullable[T]]类型 - 对于 PowerShell 来说是模糊且不典型的。

废除这些要求,转而采用统一的方法(使其适用于值类型,就像适用于引用类型一样) this proposal on GitHub的主题。

关于powershell - 在powershell中传递空日期时间参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58417780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com