更新1:
最初,我将其发布为标题:“脚本忽略PowerShell模块中的错误处理”,因为这是当前问题,但是似乎更多的是模块问题,因此我重命名了标题。
更新2:
经过一段让我质疑Azure cmdlet的评论之后,我已经测试了最基本的脚本(添加到模块中),结果是相同的,因为错误没有传递给调用脚本,但是,将-errorVariable
添加到Get-Service确实返回了我可能在处理错误时可以利用的东西(而不是WriteErrorException
):
function Test-MyError($Variable)
{
Try
{
Get-Service -Name $variable -ErrorAction Stop -ErrorVariable bar
#Get-AzureRmSubscription -SubscriptionName $variable -ErrorAction Stop
}
Catch
{
Write-Error $error[0]
$bar
}
}
返回:
Test-MyError "Foo"
Test-MyError : Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.
At line:3 char:1
+ Test-MyError "Foo"
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-MyError
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot find any service with service name 'foo'.
但是,如果我在ISE中运行“Test-MyError”,然后调用该函数,则会得到:
Test-MyError "Foo"
Test-MyError : Cannot find any service with service name 'Foo'.
At line:3 char:1
+ Test-MyError "Foo"
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test-MyError
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot find any service with service name 'Foo'.
因此,我不确定在ISE中运行“Test-MyError”并对其进行调用,以防它在PSM1文件中被点源化然后再调用它时会发生什么情况?
我现在是否必须使用
-ErrorVariable
并对此进行处理?原始问题:
我在模块中有两个功能:Get-Subscription和Get-AllSubscriptions。每个功能都位于其自己的PS1文件中,而PSM1文件是它们的点源。该模块看起来不错,因为可以使用intelisense访问模块脚本,并且模块加载没有问题。我已经在许多模块中使用了这种结构,并且之前没有遇到过这个问题。 (尽管我想知道MS是否已更改了模块在PS 5.1中的工作方式,正如我已经注意到使用
FunctionsToExport='x','y','Z'
和Export-ModuleMember
似乎不像以前那样。)Get-AllSubscriptions调用Get-Subscription。
如果我没有登录Azure,则Get-Subscription会抛出一个已处理的错误,提示我登录。如果我从Get-Subscription.ps1调用Get-Subscription,这将按预期工作。
但是,当我从新的PS1文件Get-AllSubscriptions或从Powershell控制台调用Get-Subscription时,它不起作用。它一直遍历do..until循环,而不会像我期望的那样“处理”错误。在每次迭代中,似乎都抛出一个通用错误:
Get-Subscription : Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.
但是,我确实看到了最后一个错误
Get-Subscription : Unable to find requested subscription after 3 login attempts.
如果我在ISE中执行Get-Subscription,然后在新的PS1文件中或从Get-AllSubscriptions中调用Get-Subscription,则它可以按预期工作,但是,一旦我重新导入模块(
Import-Module AzureVnetTools -Force -Verbose
),它就会返回错误的行为。如果我在调用者脚本中点源Get-Subscription,它可以工作,但是为什么呢?这就是模块的PSM1应该发生的情况。
有人可以帮我弄清楚我在做什么错吗?
(PS 5.1,Windows 7)
获取订阅:
function Get-Subscription
{
[cmdletbinding()]
Param
(
[string]$SubscriptionName,
[string]$UserName,
[string]$code
)
$c=1
Write-Verbose "Checking access to '$SubscriptionName' with user '$UserName'..."
Do
{
Write-Verbose "Attempt $c"
Try
{
$oSubscription = Get-AzureRmSubscription -SubscriptionName $SubscriptionName -ErrorAction Stop -WarningAction SilentlyContinue
Write-Verbose "Subscription found: $($oSubscription.SubscriptionName)."
}
Catch
{
if($error[0].Exception.Message -like "*Please verify that the subscription exists in this tenant*")
{
Write-Verbose "Cannot find subscription '$SubscriptionName' with provided credentials."
$account = Login-AzureRmAccount -Credential (Get-Credential -UserName $Username -Message "Subscription '$SubscriptionName' user' password:")
}
elseif($error[0].Exception.Message -like "*Run Login-AzureRmAccount to login*")
{
Write-Verbose "No logged in session found. Please log in."
$account = Login-AzureRmAccount -Credential (Get-Credential -UserName $Username -Message "Subscription '$SubscriptionName' user' password:")
}
else
{
Write-Error $error[0]
}
}
$c++
}
until(($oSubscription) -or ($c -eq 4))
if($c -eq 4)
{
Write-Error "Unable to find requested subscription after $($c-1) login attempts."
break
}
$oSubscription | Add-Member -MemberType NoteProperty -Name Code -Value $code
$oSubscription
}
获取所有订阅:
function Get-AllSubscriptions
{
[cmdletbinding()]
param
(
[string]$MasterSubscription,
[string]$MasterSubscriptionCode,
[string]$MasterSubscriptionUsername,
[string]$ChildSubscription,
[string]$ChildSubscriptionCode,
[string]$ChildSubscriptionUsername
)
Write-Verbose "Getting all subscriptions..."
$oAllSubscriptions = @()
$oMasterSubscription = Get-Subscription -SubscriptionName $MasterSubscription -UserName $MasterSubscriptionUsername -code $MasterSubscriptionCode -Verbose
$oChildSubscription = Get-Subscription -SubscriptionName $ChildSubscription -UserName $ChildSubscriptionUsername -code $ChildSubscriptionCode -Verbose
$oAllSubscriptions = ($oMasterSubscription,$oChildSubscription)
$oAllSubscriptions
}
测试:
$splat2 = @{
SubscriptionName = "SomeSubscription"
Code = "S02"
Username = "[email protected]"
}
#Write-Output "Dot-source:"
#. "D:\Temp\PS.Modules\AzureVnetTools\functions\public\Get-Subscription.ps1"
Get-Subscription @splat2 -verbose
输出:
Get-Subscription @splat2 -verbose
VERBOSE: Checking access to 'SomeSubscription' with user '[email protected]'...
VERBOSE: Attempt 1
Get-Subscription : Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.
At line:7 char:1
+ Get-Subscription @splat2 -verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-Subscription
VERBOSE: Attempt 2
Get-Subscription : Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.
At line:7 char:1
+ Get-Subscription @splat2 -verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-Subscription
VERBOSE: Attempt 3
Get-Subscription : Exception of type 'Microsoft.PowerShell.Commands.WriteErrorException' was thrown.
At line:7 char:1
+ Get-Subscription @splat2 -verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-Subscription
Get-Subscription : Unable to find requested subscription after 3 login attempts.
At line:7 char:1
+ Get-Subscription @splat2 -verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-Subscription
AzureVnetTools.psm1
#Get public and private function definition files.
$Public = @( Get-ChildItem -Path $PSScriptRoot\Functions\Public\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $PSScriptRoot\Functions\Private\*.ps1 -ErrorAction SilentlyContinue )
#Dot source the files
Foreach($import in @($Public + $Private))
{
#write-error $import.fullname
Try
{
#Write-Host "Dot-sourcing file: $($import.fullname)."
. $import.fullname
}
Catch
{
Write-Error -Message "Failed to import function $($import.fullname): $_"
}
}
Export-ModuleMember -Function $Public.Basename
AzureVnetTools.psd1(相关部分):
FunctionsToExport = '*'
最佳答案
-ErrorAction Stop -WarningAction SilentlyContinue
这是警告而不是错误吗?
关于powershell - PowerShell模块未按预期提供点源/导入功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44970992/