调用其他脚本时发生Powershell错误

调用其他脚本时发生Powershell错误,powershell,Powershell,我有两个脚本可以作为单独的脚本正常运行,但当我从另一个脚本调用其中一个脚本时,会收到以下错误消息: 异常名称:System.Management.Automation.MethodInvocationException 异常类型: 异常消息:使用“3”参数调用“Open”时出现异常:“找不到路径“D:\Data Report\”的一部分。” 正在调用的脚本正在将文件从“D:\Data Report\移动到D:\directory中的另一个文件夹 我只有在运行下面的脚本时才会出现此错误。有什么想法

我有两个脚本可以作为单独的脚本正常运行,但当我从另一个脚本调用其中一个脚本时,会收到以下错误消息:

异常名称:System.Management.Automation.MethodInvocationException 异常类型: 异常消息:使用“3”参数调用“Open”时出现异常:“找不到路径“D:\Data Report\”的一部分。”

正在调用的脚本正在将文件从“D:\Data Report\移动到D:\directory中的另一个文件夹

我只有在运行下面的脚本时才会出现此错误。有什么想法吗

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
# @这里的MadTechnician是另一个独立工作的脚本。

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
    {

    }

我正要发表评论,但我想那会结束得太长,很难阅读。正如Richard所说,当您只想运行另一个脚本时,您的问题可能来自于使用
Invoke Expression
。有两种基本方法可以做到这一点(还有许多更为复杂的方法),所以我将非常快地为您介绍它们

使用呼叫操作员
&
,运行脚本。将这样做:

& D:\file_transfer.ps1
就这么简单。这将运行另一个脚本,它将自动运行第一个脚本

第二种方法是点源,这与使用Call操作符非常相似,只是它在同一个会话中运行脚本,以便其他脚本可以访问变量以及您尚未声明的内容。从你剧本的外观来看,这是不需要的,但从我的经验来看,这通常没有什么坏处。这样做的方式是:

. D:\file_transfer.ps1
希望这样可以在不使用
Invoke Expression
cmdlet的情况下运行脚本


奖金信息当回复某人的评论时,在他们的姓名前加一个
@
,这将显示他们有一条消息。

我不知道为什么会这样做,但是如果第一个脚本运行时在$path中找不到文件,它将抛出带有“3”参数的“异常调用”Open:“找不到路径“D:\Data Report\”的一部分。错误。因此我不知道为什么,但当第一个脚本调用第二个脚本时,它认为它正在运行,而文件不在那里。 所以我加了一个: if(测试路径-路径“$Path*”){ 工作代码 } 否则
{ 出口
}

为什么要使用
调用表达式
来运行另一个脚本:为什么不直接调用它(并从该脚本中传递它所需的任何数据的参数)。但不包括错误的完整信息(哪个脚本行?)我们正在猜测。建议:删除
try/catch
以查看错误的完整详细信息,直到它正常工作。脚本的哪个部分导致了错误?错误消息中应提供行和字符号(
第行:#char:#
)。在任何情况下,错误消息似乎都非常简单:路径
'D:\Data Report\'
不可访问。脚本是否在没有访问此路径权限的帐户下运行?请在定义$folder后立即尝试添加
测试路径$folder
。Richard--我以为这就是我正在做的。你还能做什么你只要直接打电话就行了“?错误来自正在调用的脚本。当我自己运行它时,它没有错误,但是当从这个脚本调用它时,它会将错误消息发送到我的电子邮件。一切正常,它只是向我发送了一封错误电子邮件谢谢@TheMadTechnician。所以我现在使用&D:\文件\u transfer.ps1。我仍然收到相同的错误消息。异常消息:使用“3”参数调用“Open”时发生异常:“找不到路径“D:\Data Report\”的一部分”。当我从上面的脚本运行它时。我也试过了。当我自己运行文件_transfer.ps1时,它是完美的。生成错误的脚本是您的文件传输脚本,对吗?为什么不给我们看一下脚本中的相关代码?