2012-04-11 2 views
3

私はこのPowerShellスクリプトを実行しています。最初に実行すると完璧に実行され、2回目に実行されると、.csvが別のプロセスで使用されているために.csvにアクセスできないというエラーが表示されます。スクリプトのどの部分がファイルを "保持"しているのか、 ?私はそ​​れが最後で手放すPowershellファイルを使用していますか? "別のプロセスで使用されています"

clear 
set-executionpolicy remotesigned 
# change this to the directory that the script is sitting in 
cd d:\directory 

############################################# 
# Saves usernames/accountNumbers into array # 
# and creates sql file for writing to # 
############################################# 


# This is the location of the text file containing accounts 
$accountNumbers = (Get-Content input.txt) | Sort-Object 
$accountID=0 
$numAccounts = $accountNumbers.Count 
$outString =$null 
# the name of the sql file containing the query 
$file = New-Item -ItemType file -name sql.sql -Force 


################################### 
# Load SqlServerProviderSnapin100 # 
################################### 

if (!(Get-PSSnapin | ?{$_.name -eq 'SqlServerProviderSnapin110'})) 
{ 
    if(Get-PSSnapin -registered | ?{$_.name -eq 'SqlServerProviderSnapin110'}) 
    { 
     add-pssnapin SqlServerProviderSnapin100 
     Write-host SQL Server Provider Snapin Loaded 
    } 
    else 
    { 
    } 
} 
else 
{ 
Write-host SQL Server Provider Snapin was already loaded 
} 


################################# 
# Load SqlServerCmdletSnapin100 # 
################################# 

if (!(Get-PSSnapin | ?{$_.name -eq 'SqlServerCmdletSnapin100'})) 
{ 
    if(Get-PSSnapin -registered | ?{$_.name -eq 'SqlServerCmdletSnapin100'}) 
    { 
     add-pssnapin SqlServerCmdletSnapin100 
     Write-host SQL Server Cmdlet Snapin Loaded 
    } 
    else 
    { 

    } 
} 
else 
{ 
    Write-host SQL Server CMDlet Snapin was already loaded 
} 




#################### 
# Create SQL query # 
#################### 

# This part of the query is COMPLETELY static. What is put in here will not change. It will usually end with either % or ' 
$outString = "SELECT stuff FROM table LIKE '%" 
# Statement ends at '. loop adds in "xxx' or like 'xxx" 
IF ($numAccounts -gt 0) 
{ 
    For ($i =1; $i -le ($AccountNumbers.Count - 1); $i++) 
    { 
     $outString = $outstring + $AccountNumbers[$accountID] 
     $outString = $outString + "' OR ca.accountnumber LIKE '" 
     $accountID++ 
    } 
    $outString = $outString + $AccountNumbers[$AccountNumbers.Count - 1] 
} 
else 
{ 
    $outString = $outString + $AccountNumbers 
} 
# This is the end of the query. This is also COMPLETELY static. usually starts with either % or ' 
$outString = $outString + "%'more sql stuff" 
add-content $file $outString 
Write-host Sql query dynamically written and saved to file 



########################### 
# Create CSV to email out # 
########################### 

#Make sure to point it to the correct input file (sql query made above) and correct output csv. 
Invoke-Sqlcmd -ServerInstance instance -Database database -Username username -Password password -InputFile sql.sql | Export-Csv -Path output.csv 



#################################### 
# Email the CSV to selected people # 
#################################### 

$emailFrom = "to" 
$emailTo = "from" 
$subject = "test" 
$body = "test" 
$smtpServer = "server" 
# Point this to the correct csv created above 
$filename = "output.csv" 

$att = new-object Net.mail.attachment($filename) 
$msg = new-object net.mail.mailmessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 

$msg.from = $emailFrom 
$msg.to.add($emailto) 
$msg.subject = $subject 
$msg.body = $body 
$msg.attachments.add($att) 

$smtp.Send($msg) 
+0

ファイルの先頭にファイルが存在するかどうかを確認し、存在する場合は削除してみましたか。何がファイルをロックしているのかわかりませんが、私の推測はInvoke-Sqlcmdになります。 – Gisli

答えて

2

行うことができますどのようにあなたは番目の最後に追加しようとすることができます:

$att.Dispose() 
$msg.Dispose() 
$smtp.Dispose() 
+0

クールな感謝。私も$ file.Dispose()を行う必要がありますか? – mhopkins321

+0

私はそうは思わない。 – JPBlanc

1

あなたはまた、試してみて、使用procmonのようなツールをして何を見ることができましたスクリプトは、ファイルのロックを取得し、それを解放しない場合はいつでも実行します。また、(おそらく)問題が.csvファイルであるため、passinの代わりにバイト配列として読み込むことができますそれは添付ファイルとしてのパスです。この方法では、ファイルは一度読み取られ、ロックされません。

関連する問題