作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以在 PowerShell 中测试脚本范围变量的存在?
我一直在使用PowerShell Community Extensions (PSCX)但我注意到如果您在 Set-PSDebug -Strict
时导入模块设置,产生错误:
The variable '$SCRIPT:helpCache' cannot be retrieved because it has not been set.
At C:\Users\...\Modules\Pscx\Modules\GetHelp\Pscx.GetHelp.psm1:5 char:24
#requires -version 2.0
param([string[]]$PreCacheList)
if ((!$SCRIPT:helpCache) -or $RefreshCache) {
$SCRIPT:helpCache = @{}
}
$SCRIPT:helpCache
而
Set-PSDebug -Strict
is in force 会导致错误,因为尚 undefined variable 。
Test-Variable
cmdlet,但这样的事情不存在!我想看看
variable:
provider 但我不知道如何确定变量的范围。
Set-PSDebug -Strict
时测试变量是否存在?是否有效,不会导致错误?
最佳答案
使用test-path variable:SCRIPT:helpCache
if (!(test-path variable:script:helpCache)) {
$script:helpCache = @{}
}
@'
Set-PsDebug -strict
write-host (test-path variable:script:helpCache)
$script:helpCache = "this is test"
write-host (test-path variable:script:helpCache) and value is $script:helpCache
'@ | set-content stricttest.ps1
.\stricttest.ps1
关于powershell - 如何在 PowerShell 中测试脚本范围变量的存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2763900/
我是一名优秀的程序员,十分优秀!