0

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?

Sameer
  • 3,124
  • 5
  • 30
  • 57
  • how do you call it from c sharp? – amit dayama Sep 23 '15 at 07:40
  • [Call from C#](http://stackoverflow.com/questions/23234170/memory-leak-using-powershell-remote-calls-in-c-sharp/32562222#32562222) – Sameer Sep 23 '15 at 07:48
  • are you running your application as an administrator? Have you tried running your application by giving full rights to everyone on your temp folder(Just for time being)? – amit dayama Sep 23 '15 at 07:52
  • The file is already getting created which means the process has write permission on the folder. – Sameer Sep 23 '15 at 07:55

0 Answers0