Clear-Variable isn’t clearing variables!

I have a script which I want to hand out to my team which requires credentials to access the service. The script stores the credentials against the variable $userCredentials.

When they are done I want them to run another script which will log them off and delete anything stored in the variable. I thought clear-variable would work, but it doesn’t. I tried to set $userCredentials = $null but that didn’t work either. Whenever I run $UserCredentials it shows their email and SecurityString for password.

Am I missing something obvious? Is this even possible? This is the code:

Function Disconnect-ReportViewer

{

<#

.SYNOPSIS

This function will disconnect the Report Viewer session and delete any stored user credentials.

.DESCRIPTION

Use this function to close any open Report Viewer sessions. It will close any sessions and remove the stored credentials under $UserCredentials, forcing the user to re-enter any credentials the next time it is run.

`.EXAMPLE` 

Disconnect-ReportViewer

.NOTES

#>

`$UserCredentials = $null` `Remove-PSSession -Name "Report Viewer"` `if ($UserCredentials = $null)` `{` `Write-Host "Your Report Viewer session has " -noNewLine;` `Write-Host "ended " -ForegroundColor red -NoNewline;` `Write-Host "and your stored admin credentials have been " -noNewLine;` `Write-Host "deleted. " -ForegroundColor red -NoNewLine;` `Write-Host "You can no longer use Report Viewer cmdlets.";` `}` `else` `{` `Write-Host "The action did not complete successfully.";` `Write-Host "Please try running the command again";` `}` 

}

submitted by /u/DanTastic_
[link] [comments]

Leave a Reply