title: Powershell error when invoking another script
body: <p>I have two scripts that run fine as seperate scripts, but when I invoke one from the other then I get this error message:<br></p>
  
  <blockquote>
    <p>Exception Name: System.Management.Automation.MethodInvocationException
    Exception Type:
    Exception Message: Exception calling "Open" with "3" argument(s): "Could not find a part of the path 'D:\Data Report\'.</p>
  </blockquote>
  
  <p>The script being called is moving files from "D:\Data Report\ to another folder in the D:\ directory.</p>
  
  <p>I only get this error when I run the script below. Any ideas?</p>
  
  <pre><code>try
  {
      $folder = "D:\Data Report\" # Enter the root path you want to monitor.
      $filter = '*.*'  # You can enter a wildcard filter here.
      #$scriptPath = {D:\"file_transfer.ps1"}
  
      # In the following line, you can change 'IncludeSubdirectories to $true if required.
      $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
  
      Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action {
          $name = $Event.SourceEventArgs.Name
          $changeType = $Event.SourceEventArgs.ChangeType
          $timeStamp = $Event.TimeGenerated
          Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
  
          #Call the file_transfer.ps1 script
          #Invoke-Expression $scriptPath
          Invoke-Expression D:\file_transfer.ps1
      }
  
  } # end of try
  catch  #Catch any fatel errors and send an email with description
  {
      $ErrorMessage = $_.Exception.Message
      $FailedItem = $_.Exception.ItemName
      $ErrorName = $_.Exception.GetType().FullName
  
      $ExceptionBody = @"
      Exception Name: $ErrorName
      Exception Type: $FailedItem
      Exception Message: $ErrorMessage
      "@
      Send-MailMessage  -SmtpServer "server.com" -From "no-replies@company.com" -To "admin@company.com" -Subject "Fatel Error with Monitor Script" -Body $ExceptionBody
  } # end of catch
  </code></pre>
  
  #
  
  <p>@TheMadTechnician Here is the other script that works on its own.<br></p>
  
  <pre><code>try
  {
  $path="D:\Data Report\" 
  $Miscdir="D:\TEST\Data\Misc"
  $dataFolder = "D:\Data Report\*"
  $items = Get-ChildItem -Path ($path) -ErrorAction Stop
  $ErrorActionPreference= 'silentlycontinue'
  
  # enumerate the items array
  foreach ($item in $items)
  {
  
        # if the item is NOT a directory, then process it.
       if ($item.Attributes -ne "Directory")
       {
             #Write-Host $item.Name
             $filePath = $path+$item.name
  
  
  }
  else  #send email and exit without running.
  {
      Write-Host "$filePath is a directory"
       Exit
  }
  
  function isFileLocked([string]$LockedPath) {
  $file=$filePath
  $oFile = New-Object System.IO.FileInfo $LockedPath
  # Make sure the path is good
  if ((Test-Path -Path $LockedPath) -eq $false)
  {
    echo "Bad Path"
    return $false
  }
        #Try opening file
    $oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
    if ($oStream)
    {
      echo "Got valid stream so file must not be locked"
      $oStream.Close()
      #Pick   ID from file name
      $ ID = ($item.name.Split("_")[0])
      Write-Host $ ID
  
          $listcsv = import-csv d:\number.csv
          foreach($number in $listcsv){
  
          $UNCNumber = $number.UNCNumber
          #Write-Host $UNCNumber
          ANCNumber = $number.NoseNumber
          #Write-Host ANCNumber
          ANCNumber = $number.NoseNumber
  
          if ($ ID -eq $UNCNumber)
  
     {
          #Check/create  ID folder
      ANCNumberdir="D:\TEST\Data\ANCNumber"
      Write-Host ANCNumber
      if (!(Test-Path ANCNumberdir))
       { mkdir ANCNumberdir }
  
       Echo "Nose number found the csv file ANCNumber" 
        $ IDdir = ANCNumber
            Write-Host " id from csv file $ ID" }
            }
  
  #Check/create App id folder/system
     #Pick App ID from file name
      $AppID=($item.name.Split("_")[1])
      Write-Host $AppID
  
  
          $Appiddir="ANCNumberdir\$AppID"
         if (!(Test-Path $Appiddir))
       { mkdir $Appiddir 
           Write-Host $Appiddir
          }
  
  
     Move-Item ($filePath) $Appiddir
       return $false
    } # end if ($oStream)
    else 
    {
  
     echo "InValid stream so file is locked"
      return $true
  
  if (!(Test-Path $Miscdir))
            { 
        mkdir $Miscdir 
        Write-Host $Miscdir
            }             
       Move-Item $path"*" $Miscdir 
    }
  }# end of function
  isFileLocked($filePath)
  
  } 
  
  
  # Send an email if the script ran but a file didn't get moved out of the Data Report folder    
  
       If (Test-Path $dataFolder)
       {
       $DataNumbers = (get-childitem $dataFolder | ?{!($_.PSIsContainer)}).Count
        }
  
  # check how many files are in our Misc folder and email
  
      $MiscNumbers = (get-childitem $miscdir | ?{!($_.PSIsContainer)}).Count
      If ($MiscNumbers -gt 0)
      {
      }
  
      } # end of try
      catch  #Catch any fatel errors and send an email with description
      {
      $ErrorMessage = $_.Exception.Message
      $FailedItem = $_.Exception.ItemName
      $ErrorName = $_.Exception.GetType().FullName 
  
      $ExceptionBody = @"
      Exception Name: $ErrorName
      Exception Type: $FailedItem 
      Exception Message: $ErrorMessage 
  "@
       } # end of catch
  
      finally
      {
  
      }
  </code></pre>