I have powershell script to fetch exchange users and this script gets called from C#. This also creates temp files in temp directory. But when called from c# the Get-PSSession|Remove-PSSession
is not deleting temp files. But it removes temp files when the script is executed from Powershell window.
Function GetExchangeUsers($userName, $cred){
try{
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $ExSession -AllowClobber | Out-Null
$exchangeUsers=Get-MailBox -Filter '(RecipientTypeDetails -eq "UserMailBox")' | select UserPrincipalName,InPlaceHolds
return $exchangeUsers
}
finally{
get-module | Remove-Module
Get-PSSession|Out-File "F:\FinallyGetExchangeUsers.txt"
Get-PSSession|Remove-PSSession
Get-PSSession|Out-File "F:\AfterFinallyGetExchangeUsers.txt"
}
}
What can be the issue here?