我有两个脚本可以作为单独的脚本运行,但是当我从另一个脚本中调用一个脚本时,我收到此错误消息:
异常名称:System.Management.Automation.MethodInvocationException 例外类型: 异常消息:异常调用"打开"用" 3"参数:"无法找到路径的一部分' D:\ Data Report \'。
被调用的脚本是将文件从" D:\ Data Report \移动到D:\目录中的另一个文件夹。
我在运行下面的脚本时只收到此错误。有什么想法吗?
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
#
@TheMadTechnician这是另一个独立运作的脚本。
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
{
}
答案 0 :(得分:0)
我即将发表评论,但我认为这样做会耗费太长时间,难以阅读。正如理查德所说,似乎您的问题很可能来自使用Invoke-Expression
,而您只想运行其他脚本。有两种基本的方法可以做到这一点(以及更复杂的方法),所以我会快速为你运行它们。
使用呼叫操作员&
运行脚本。它会这样做:
& D:\file_transfer.ps1
就这么简单。这将运行另一个脚本,它将自动运行第一个脚本。
第二种方法是点源它,这与使用调用操作符非常相似,只是它在同一个会话中运行脚本,因此其他脚本可以访问变量以及您已经声明的内容。从你的脚本看起来不需要,但它通常不会伤害我的经验。你这样做的方式是:
. D:\file_transfer.ps1
希望在不使用Invoke-Expression
cmdlet的情况下清除脚本运行。
奖励信息:在回复某人的评论时,在他们的名字前放置一个@
,它会向他们显示他们有消息。
答案 1 :(得分:0)
我不知道为什么会这样做但是如果第一个脚本运行并且它在$ path中找不到文件那么它会抛出“Exception calling”Open“with”3“argument(s) :“无法找到路径的一部分'D:\ Data Report \'。”错误。所以我不知道为什么,但是当第一个脚本调用第二个脚本时,它认为它正在运行并且文件不存在。
所以我输了一个:
if(Test-Path -Path“$ path *”){
工作代码
}
否则
{
出口
}