當我運行下面的腳本時,它拋出錯誤“行程無法訪問該檔案,因為它正被另一個行程使用”:
$RetailSource = Join-Path -Path 'D:\Folder' -ChildPath 'Retail'
$GroupSource = Join-Path -Path 'D:\Folder' -ChildPath 'Group'
$UATSource = Join-Path -Path 'D:\Folder' -ChildPath 'UAT'
$GRSource = Join-Path -Path 'D:\Folder' -ChildPath 'Report'
#$PMDashsource = Join-Path -Path 'D:\Folder' -ChildPath 'Dashboard' $style="<Style>Body{Font:arial 10pt}</Style>"
####################################################################################
# group these paths into a single array variable
####################################################################################
$Source = $RetailSource, $GroupSource, $UATSource, $GRSource
$RetailDest = '\\192.168.1.10\Folder1' # 1st Destination Location
$GroupDest = '\\192.168.1.10\Folder2' # 2nd Destination location
$UATDest = '\\192.168.1.10\Documents\Folder3' # Added 3rd destination location in this version 1.7.2V
#$PMDest = 'D:\ToolUpload\Folder4' $GRDest = '\\192.168.1.10\Folder5'
$ArchiveData = '\\192.168.1.10\backup_{0:yyyyMMddHHmm}' -f (Get-Date)
$LogFolder = 'D:\Uploadlogs-EIP'
#Logs will be generate in csv file at the below location.
$LogFile = 'D:\Uploadlogs-EIP\uploadlog_{0:yyyyMMdd}.csv' -f (Get-Date)
# make sure the output LogFolder exist
# by adding the -Force switch there is no need to use Test-Path first, because if
# the folder already exists, the cmdlet will return the DirectoryInfo of that,
# otherwise it will create a new folder. Since we don't want output, we use $null = ..
$null = New-Item -Path $LogFolder -ItemType Directory -Force
# loop through the files in the source folders and collect the outputted objects.
####################################################################################
# $Source is an ARRAY of paths
####################################################################################
$files = @(Get-ChildItem -Path $Source -File -Force -Recurse)
if ($files.Count -eq 0) { exit } #check the condition if the file doesn't exit then exit the code
$result = $files | ForEach-Object {
Write-Host "Processing file '$($_.FullName)'"
# create an object with (for now) 2 empty properties
$out = $_ | Select-Object @{Name = 'Date'; Expression = { (Get-Date) } },
@{Name = 'Source'; Expression = { $_.FullName } },
@{Name = 'FileSize'; Expression = { $_.Length / 1GB } },
Destination, # depends on the file name
@{Name = 'Archive'; Expression = { $ArchiveData } },
Result
# test if the file has a correct name
if ($_.BaseName -notmatch 'Retail Platform|Group Platform|Dashboard-Group|5 Year Report') {
# file does not have the correct naming convention
$out.Archive = $null
$out.Result = "Error: Incorrect filename"
}
else {
# depending on its name, get the correct destination folder
$destFolder = if ($_.Name -match "Retail Platform.qvw") {
$RetailDest
}
elseif ($_.Name -match "5 Year Report.qvw") {
$GRDest
}
elseif ($_.Name -match "Dashboard-Group.qvw") {
$GRDest
}
elseif ($_.Name -match "Group Intelligence Platform.qvw") {
$GroupDest
}
else {
$UATDest
}
# create the backup destination folder if it didn't already exist
# the first file in column 'Source' is now responsible for creating the backup folder
$null = New-Item -Path $destFolder -ItemType Directory -Force
# get the full path and filename for the destination
$existingFile = Join-Path -Path $destFolder -ChildPath $_.Name
# add the destination folder to the output object
$out.Destination = $destFolder
try {
# if a file with that name already exists in the destination, move it to the Archive folder
if (Test-Path -Path $existingFile -PathType Leaf) {
# create the Archive folder if it didn't already exist
$null = New-Item -Path $ArchiveData -ItemType Directory -Force
Move-Item -Path $existingFile -Destination $ArchiveData -ErrorAction Stop -Force
# add the archived file to the output object
$out.Archive = $existingFile
Write-Host "File '$existingFile' has been backed-up to '$ArchiveData'"
}
# next move the file from the source folder to its destination (either $RetailDest or $GroupDest)
$_ | Move-Item -Destination $destFolder -ErrorAction Stop
$out.Result = 'The tool has been uploaded'
Write-Host "The File '$($_.FullName)' has been moved to '$destFolder'"
}
catch {
# ouch.. something went horribly wrong on a Move-Item action
Write-Warning "An error occurred: $_.Exception.Message"
$out.Result = "Error: $($_.Exception.Message)"
####################################################################
# $LogFile will be saved later as CSV,
####################################################################
}
}
# output the object so it gets collected in variable $result
$out }
# now you can save the results as structured CSV file to open in Excel
$result | Export-Csv -Path $LogFile -UseCulture -NoTypeInformation -Append -Force
# and display on the screen using Out-GridView as the data will probably be too wide for
Format-Table $result | Out-GridView -Title 'Backup results'
# and send an email with the resulting CSV file as attachment
$result1 = $result
$htmlTemplate = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>File upload status</title>
<style>
body, table {font-family: sans-serif; font-size: 10pt; color: #000000;}
table {border: 1px solid black; border-collapse: collapse;}
th {border: 1px solid black; background: #dddddd; padding: 3px;}
td {border: 1px solid black; padding: 3px;}
</style>
</head>
<body>
<H2>Please find the following Tool upload status</H2>
@@TABLE@@
</body>
</html>
"@
$body = $htmlTemplate -replace '@@TABLE@@', (($result1 | ConvertTo-Html -Fragment) -join [Environment]::NewLine)
# if you want, you can save this to a file $body | Set-Content -Path 'C:\Desktop\serverstatus.html'
$EmailParams = @{
SmtpServer = 'xyz.mail.protection.outlook.com'
Port = '25'
Subject = 'The File Upload Status'
To = '[email protected]'
From = "[email protected]"
Body = $body
BodyAsHTML = $true
}
Send-MailMessage @EmailParams
uj5u.com熱心網友回復:
我讀了劇本,我認為該錯誤使用$ out變數和更新其引數,如變$out.archive等,但沒有$out.update()在最后
順便說一句,我看到您正在使用 $null = ... 來避免輸出,最好使用管道 | Out-Null
最好使用行大小寫錯誤更新我們,如果這沒有幫助,它可能會有所幫助
uj5u.com熱心網友回復:
我看到自從我寫這個答案以來你已經改變了你的 SO 用戶名。無論如何,Move-Item如果無法訪問兩個檔案中的任何一個,代碼中有兩個命令都可以顯示該錯誤。
如果您希望錯誤也顯示這是哪個檔案,您可以try{..} catch {..}像這樣更新塊內的代碼:
try {
# if a file with that name already exists in the destination, move it to the Archive folder
if (Test-Path -Path $existingFile -PathType Leaf) {
# store the file that we will try to move in a variable in case we hit the catch{..} block
$fileThatCouldThrowError = $existingFile
# create the Archive folder if it didn't already exist
$null = New-Item -Path $ArchiveData -ItemType Directory -Force
Move-Item -Path $existingFile -Destination $ArchiveData -ErrorAction Stop -Force
# add the archived file to the output object
$out.Archive = $existingFile
Write-Host "File '$existingFile' has been backed-up to '$ArchiveData'"
}
# next move the file from the source folder to its destination (either $RetailDest or $GroupDest)
# if we got here, the file that could throw an exception is in $_.FullName
# store that file in the variable in case we hit the catch{..} block
$fileThatCouldThrowError = $_.FullName
$_ | Move-Item -Destination $destFolder -ErrorAction Stop
$out.Result = 'The tool has been uploaded'
Write-Host "The File '$($_.FullName)' has been moved to '$destFolder'"
}
catch {
# ouch.. something went horribly wrong on a Move-Item action
Write-Warning "An error occurred on file '$fileThatCouldThrowError': $_.Exception.Message"
$out.Result = "Error on file '$fileThatCouldThrowError': $($_.Exception.Message)"
####################################################################
# $LogFile will be saved later as CSV,
####################################################################
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/328047.html
標籤:电源外壳
