PowerShell - Get-ChildItem Remote UNC Share

So I'm curious if anyone else has ran into this issue.  I'm working on a file age monitor to retrieve the contents of a directory, grab the newest file, and report back on how many minutes its there.  In the event there are no files, I'm going off of the last modified time of the folder itself.

I'm utilizing Script arguments for the file path, so this monitor could easily be redistributed to other servers.

#Put Directory in Script Arguments
  $dirCheck = $args[0]
#Retrieve Child Items
  Try {
    $DirectoryResult = Get-ChildItem -Path $DirCheck -File -ErrorAction Stop
  } Catch {
    Write-Host "Message:  Child Item - $($Error[0].Exception)"
    #Exit 1
  }
#Retrive Last Write
  If (($DirectoryResult | Measure-Object).Count -gt 0) {
    $LastModified = ($DirectoryResult | Sort-Object LastWriteTime -Descending | Select-Object -First 1).LastWriteTime
  } Else {
    #Retrieve Last Write from directory due to empty folder
    Try {
      $LastModified = (Get-Item $dirCheck -ErrorAction Stop).LastWriteTime
    } Catch {
      Write-Host "Message: Last Write Retrieve - $($Error[0].Exception)"
    }
  }
#Calculation
  Try {
    [Int]$MinutesSinceModified = ((Get-Date) - $LastModified).TotalMinutes
  } Catch {
    Write-Host "Message:  Calculation - $($Error[0].Exception)"
    Exit 1
  }

#Result - Statistic
  Write-Host "Statistic:  $($MinutesSinceModified)"

So the issue I'm running into, is I'm getting access denied when running it under remote execution to a file share.  If I copy and paste the code, and run it in a normal powershell window on the target computer with the monitoring account, it runs with no problems.  So I know my logic and credentials are good.  If I switch the directory to a local directory on the server, it will run with no problems.  So it seems like something is getting lost when targetting the share.

I also turned on debugging and reviewed the log files on that front, and everything looks like it is good.

So any thoughts?

Parents Reply Children
No Data