Skip to content

Commit 26b6a66

Browse files
committed
Clarified logging for better troubleshooting
1 parent 6742753 commit 26b6a66

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Assert-AutoShutdownSchedule.ps1

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ param(
5555
[bool]$Simulate = $false
5656
)
5757

58-
$VERSION = "2.0"
58+
$VERSION = "2.0.1"
5959

6060
# Define function to check current time against specified range
6161
function CheckScheduleEntry ([string]$TimeRange)
@@ -279,15 +279,33 @@ try
279279
}
280280
Write-Output "Current UTC/GMT time [$($currentTime.ToString("dddd, yyyy MMM dd HH:mm:ss"))] will be checked against schedules"
281281

282+
# Retrieve subscription name from variable asset if not specified
283+
if($AzureSubscriptionName -eq "Use *Default Azure Subscription* Variable Value")
284+
{
285+
$AzureSubscriptionName = Get-AutomationVariable -Name "Default Azure Subscription"
286+
if($AzureSubscriptionName.length -gt 0)
287+
{
288+
Write-Output "Specified subscription name/ID: [$AzureSubscriptionName]"
289+
}
290+
else
291+
{
292+
throw "No subscription name was specified, and no variable asset with name 'Default Azure Subscription' was found. Either specify an Azure subscription name or define the default using a variable setting"
293+
}
294+
}
295+
282296
# Retrieve credential
283297
write-output "Specified credential asset name: [$AzureCredentialName]"
284298
if($AzureCredentialName -eq "Use *Default Automation Credential* asset")
285299
{
286300
# By default, look for "Default Automation Credential" asset
287301
$azureCredential = Get-AutomationPSCredential -Name "Default Automation Credential"
288-
if($azureCredential -eq $null)
302+
if($azureCredential -ne $null)
303+
{
304+
Write-Output "Attempting to authenticate as: [$($azureCredential.UserName)]"
305+
}
306+
else
289307
{
290-
throw "No automation credential name was specified, and no credential asset with name 'Default Automation Credential' was found. Either specify a stored credential name or define the default using a credential asset"
308+
throw "No automation credential name was specified, and no credential asset with name 'Default Automation Credential' was found. Either specify a stored credential name or define the default using a credential asset"
291309
}
292310
}
293311
else
@@ -306,27 +324,13 @@ try
306324
# Check for returned userID, indicating successful authentication
307325
if(Get-AzureAccount -Name $azureCredential.UserName)
308326
{
309-
Write-Output "Successfully authenticated as user: $($azureCredential.UserName)"
327+
Write-Output "Successfully authenticated as user: [$($azureCredential.UserName)]"
310328
}
311329
else
312330
{
313331
throw "Authentication failed for credential [$($azureCredential.UserName)]. Ensure a valid Azure Active Directory user account is specified which is configured as a co-administrator (using classic portal) and subscription owner (modern portal) on the target subscription. Verify you can log into the Azure portal using these credentials."
314332
}
315333

316-
# Retrieve subscription name from variable asset if not specified
317-
if($AzureSubscriptionName -eq "Use *Default Azure Subscription* Variable Value")
318-
{
319-
$AzureSubscriptionName = Get-AutomationVariable -Name "Default Azure Subscription"
320-
if($AzureSubscriptionName.length -gt 0)
321-
{
322-
Write-Output "Specified subscription name/ID: $AzureSubscriptionName"
323-
}
324-
else
325-
{
326-
throw "No subscription name was specified, and no variable asset with name 'Default Azure Subscription' was found. Either specify an Azure subscription name or define the default using a variable setting"
327-
}
328-
}
329-
330334
# Validate subscription
331335
$subscriptions = @(Get-AzureSubscription | where {$_.SubscriptionName -eq $AzureSubscriptionName -or $_.SubscriptionId -eq $AzureSubscriptionName})
332336
if($subscriptions.Count -eq 1)

0 commit comments

Comments
 (0)