Skip to content

Importing RubrikSecurityCloud does not return you to the original working directory #198

@fourpastmidnight

Description

@fourpastmidnight

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions