2017-03-18 71 views
0

所以我有一个脚本,通过一个约1200首歌曲的目录进行梳理,用户选择一首歌曲(然后放入$Selected变量),然后通过“script magic”(I可以在必要时提供代码,但我认为这不是我们的目的),我们会将一封电子邮件发送到我需要发送到的电子邮件帐户。然后我想从目录aaaannnnndddd中删除歌曲,当我遇到问题时。下面是我最初试图删除歌曲与代码:Remove-Item cmdlet无法正常工作

Remove-Item C:\Users\woafr\Desktop\Songs\$Selected -recurse -force

有了这样的代码,我被击中了与此错误消息:

Remove-Item : Cannot remove item C:\Users\woafr\Desktop\Songs\song.mp3: The process cannot access the file C:\Users\woafr\Desktop\Songs\song.mp3' because it is being used by another process

所以我当时读this articethis Stack Overflow threadthis Server Fault thread并将我的代码修改为:

Get-ChildItem C:\Users\woafr\Desktop\Songs\$Selected -recurse -force | Remove-Item

并且仍然遭到了同样的错误。有什么我可以在这里做,将有我删除的歌曲,否则我将不得不做手工

下面是引用整个脚本(恐怖!):

# Search Engine part 
$SearchInput = Read-Host "Enter song name here:" 
$Items = Get-ChildItem C:\Users\woafr\Desktop\Songs -Recurse -Filter *$SearchInput* 
IF (-Not $Items) 
{Write-Host 'Nothing returned... 
The search engine does not care about capitilization (so "That" and "that" are read the exact same by the search engine) 
But it does care about punctuation (so "That''s" and "Thats" are not read the same by the search engine). Try Again' -ForegroundColor Red} 

# Choose you this day what song you want 
IF (-Not $Items) 
{cmd /c pause} 
$Index = 1 
$Count = $Items.Count 
foreach ($Item in $Items) { 
    $Item | Add-Member -MemberType NoteProperty -Name "Index" -Value $Index 
    $Index++ 
} 
$Items | Select-Object Index, Attributes, LastWriteTime, Name | Out-Host 
$Input = Read-Host "Select an item by index number, then press enter (1 to $Count)" 
$Selected = $Items[$Input - 1] 
Write-Host "You have selected $Selected" 


# Email account the script is sending from 
$SMTPServer = "smtp.gmail.com" 
$SMTPPort = "587" 
$Username = "[email protected]" 
$Password = "mypassword" 

# Email the script is sending 
$to = "[email protected]" 
$subject = "Songs To Ingest" 
$body = "Ingest attachment into WideOrbit" 
$attachment = New-Object System.Net.Mail.Attachment("C:\Users\woafr\Desktop\Songs\$Selected") 

$attachment.ContentDisposition.FileName = "$Selected" 

# Act of sending the email 
$message = New-Object System.Net.Mail.MailMessage 
$message.subject = $subject 
$message.body = $body 
$message.to.add($to) 
$message.from = $username 
$message.attachments.add($attachment) 

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); 
$smtp.EnableSSL = $true 
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); 
$smtp.send($message) 
write-host "Mail Sent" -ForegroundColor Green 

cmd /c pause 

# Trying to delete the damn thing 
Get-ChildItem C:\Users\woafr\Desktop\Songs\$Selected -recurse -force | Remove-Item 

cmd /c pause 
+0

您能否提供魔术脚本? –

+0

@Flynn Handley哈哈,是的。刚刚编辑过 –

+0

我注意到你的-force在管道的左侧,我想你可能希望它在右侧,这样它会读取-recurse | Remove-Item -Force否则你试图强制Get-Content,而不是Remove-Item cmdlet。 – Random206

回答

1

所以问题是发送邮件的对象正在锁定文件,请尝试在您的remove-item命令提示符之前使用此代码段:

$smtp = $null 

foreach ($attachment in $message.attachments){ 
$attachment.dispose(); 

} 
$message.dispose();