- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在编写一个脚本来在 Azure 中设置 Redis 缓存。在设置新的缓存之前,我想通过运行以下命令来检查它是否存在:
$cache = Get-AzureManagedCache -Name $cacheName
如果缓存不存在,则输出此“异常”:
Get-AzureManagedCache:找不到缓存服务“PrototypeFOO”在 C:\builds\repos-scm\branches\2.6\2.6.0\scm\AzureDeploymentSandbox\Scripts\Create-RedisCache.ps1:39 char:15+ $cache = 获取-AzureManagedCache -名称 $cacheName+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : CloseError: (:) [Get-AzureManagedCache],ArgumentException +FullyQualifiedErrorId:Microsoft.Azure.Commands.ManagedCache.GetAzureManagedCache
所以我将它包装在一个 try/catch block 中,如下所示:
try
{
$cache = Get-AzureManagedCache -Name $cacheName
}
catch
{
Write-Host "Catching the exception"
}
当我运行它时,它会直接跳过 catch 语句并继续处理。我可以将“-EA SilentlyContinue”附加到第一行以抑制该消息,但我担心这也会抑制合法错误,所以我想避免这种情况。关于如何处理此异常有什么想法吗?
最佳答案
保留 try/catch block ,但将 -ErrorAction Stop
添加到 Azure 调用。
或者,您可以在脚本顶部设置 $ErrorActionPreference='Stop';
,这将自动使所有错误的行为就像在 .NET 中一样。
关于powershell - 是否可以在 Powershell 中处理 Get-AzureManagedCache 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29754619/
我正在编写一个脚本来在 Azure 中设置 Redis 缓存。在设置新的缓存之前,我想通过运行以下命令来检查它是否存在: $cache = Get-AzureManagedCache -Nam
我是一名优秀的程序员,十分优秀!