-
Notifications
You must be signed in to change notification settings - Fork 10
Open
fourpastmidnight/rubrik-powershell-sdk
#1Description
When importing the RubrikSecurityCloud module, the module does not return you to the original working directory as is expected:
#
# Load Core DLL:
#
try {
# Record the user's current working directory so that we can return to it when we
# are done loading the module
$currentPath = Get-Location
# Other code below ....
}
catch {
Write-Error "Unable to load Rubrik Security Cloud Module: $_"
}
finally {
# Ensure the working directory has been set back to the original when the script exits
Set-Location $currentPath
}
The problem with this code is that $currentPath
has a different scope inside the try
block than the same variable inside the finally
block, and therefore, when Set-Location $currentPath
is run inside the finally
block, $currentPath
eveluates to $null
and therefore, the default value for the -Path
parameter of Set-Location
is used, which is .
, or, the current working directory, which in this case, effectively does nothing.
Instead, you need to set the $currentPath
variable outside the try
block.
#
# Load Core DLL:
#
# Record the user's current working directory so that we can return to it when we
# are done loading the module
$currentPath = Get-Location
try {
# Other code below ....
}
catch {
Write-Error "Unable to load Rubrik Security Cloud Module: $_"
}
finally {
# Ensure the working directory has been set back to the original when the script exits
Set-Location $currentPath
}
Now this will properly reset the current working directory.
Metadata
Metadata
Assignees
Labels
No labels