- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以通过 ValidateScript
使用自定义函数? ,该函数稍后在脚本中定义。
此外,在调用此函数时是否可以引用其他参数(即假设没有循环依赖)?
我理解为什么这可能是不可能的,但由于它很有用,我希望 MS 实现一些特殊规则,以允许在验证参数发生之前读取脚本并提供函数定义。
例如
#Run-DemoScript.ps1
param (
[Parameter(Mandatory = $true)]
[string]$DbInstance
,
[Parameter(Mandatory = $true)]
[string]$DbCatalog
,
[Parameter(Mandatory = $true)]
#
# Is this possible; i.e.
# - Validate-Country is not defined until later in this script
# - DbInstance and DbCatalog parameters are defined alongside Country
[ValidateScript({Validate-Country -Country $_ -DbInstance $DbInstance -DbCatalog $DbCatalog})]
#
[string]$Country
)
#returns $true if the country is in the database's country table; otherwise false
function Validate-Country {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$DbInstance
,
[Parameter(Mandatory = $true)]
[string]$DbCatalog
,
[Parameter(Mandatory = $true)]
[string]$Country
)
process {
$Country = $Country -replace "'","''"
((Execute-SQLQuery -DbInstance $DbInstance -DbCatalog $DbCatalog -Query "select top 1 1 x from dbo.Country where Name = '$Country'") | Measure-Object | Select -ExpandProperty Count) -gt 0
}
}
function Execute-SQLQuery {
#...
}
"Script ran with Country $Country"
ValidateScript
中属性,并且仍然可以稍后在脚本中访问该函数;例如:
param(
[Parameter(Mandatory = $true)]
[ValidateScript({
function IsValid ($test) {
$test -eq 'test'
}
IsValid $_
})]
[string]$x
)
"Output: $x"
"Is Valid? $(IsValid $x)"
param(
[Parameter(Mandatory = $true)]
[string]$y
,
[Parameter(Mandatory = $true)]
[ValidateScript({
function IsValid ($a,$b) {
$a -eq $b
}
IsValid $_, $y
})]
[string]$x
)
"X: $x"
"Y: $Y"
"Is Valid? $(IsValid $x $y)"
最佳答案
从目前的反馈来看,目前在 PowerShell v4 中似乎无法完全按照我的意愿行事。
我最终使用了一个简单的解决方法来实现这一目标。它增加了一点开销,但并不太痛苦。
ValidateScript
从文件的参数验证。 RUN
, 并将文件的参数完全复制为该函数的参数。该函数出现在脚本中的哪个位置并不重要,只要它在被调用之前即可(参见步骤 4)。 ValidateScript
部分到这个函数的定义。 RUN
传递所有参数的函数(使用 @PSBoundParameters
为简单起见/减少维护)。 process
RUN
块功能。 @PSBoundParameters
中。在步骤 4 中提到。有关更多信息,请参阅 Parameters with default value not in PsBoundParameters? .如果您将默认逻辑与其他参数信息一起复制到函数定义中,这不是问题。 #Run-DemoScript.ps1
param (
[Parameter(Mandatory = $true)]
[string]$DbInstance
,
[Parameter(Mandatory = $true)]
[string]$DbCatalog
,
[Parameter(Mandatory = $true)]
#[ValidateScript({Validate-Country -Country $_ -DbInstance $DbInstance -DbCatalog $DbCatalog})]
[string]$Country
)
#move all logic from main script into here
#copy parameters from file's param definition, only add in validation
function RUN {
param (
[Parameter(Mandatory = $true)]
[string]$DbInstance
,
[Parameter(Mandatory = $true)]
[string]$DbCatalog
,
[Parameter(Mandatory = $true)]
[ValidateScript({Validate-Country -Country $_ -DbInstance $DbInstance -DbCatalog $DbCatalog})]
[string]$Country
)
process {
"Script ran with Country $Country"
}
}
#returns $true if the country is in the database's country table; otherwise false
function Validate-Country {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$DbInstance
,
[Parameter(Mandatory = $true)]
[string]$DbCatalog
,
[Parameter(Mandatory = $true)]
[string]$Country
)
process {
$Country = $Country -replace "'","''"
((Execute-SQLQuery -DbInstance $DbInstance -DbCatalog $DbCatalog -Query "select top 1 1 x from dbo.Country where Name = '$Country'") | Measure-Object | Select -ExpandProperty Count) -gt 0
}
}
function Execute-SQLQuery {
#...
}
RUN @PSBoundParameters #remember to handle default parameters: https://stackoverflow.com/questions/2808973/parameters-with-default-value-not-in-psboundparameters
关于validation - 将 ValidateScript 与稍后在脚本中定义的自定义函数一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34274871/
我正在编写一个插件,有一个ajax调用向用户显示数据。 如果用户想在ajax成功时添加一些js?他可以从他的 js 脚本中做到这一点吗,比如定位这个 ajax 成功事件。 例如: $(documen
我有 html 代码,例如 - x 最初插入 div 'insert_calendar_eda_form'。 Javascript代码 calendar_eda_add
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我已经使用命令 sudo start myservice 启动了一个 upstart 服务。我想要一种方法,以便稍后我(或任何人)可以检查该服务是否已启动并正在运行。检查它的命令是什么? 最佳答案 找
我是一名优秀的程序员,十分优秀!