2014-09-21 83 views
0

我有動態信息(Report_ PC名稱-Date_User)生成的ZIP文件。但是,當我去附加文件時,我無法使用通配符。此目錄中只有一個ZIP文件,因此使用通配符將不會附加任何其他ZIP文件。Attachments.Add通配符使用PowerShell

#Directory storage 
    $DIR = "$ENV:TEMP" 

    #Max number of recent screen captures 
    $MAX = "100" 

    #Captures Screen Shots from the recording 
    $SC = "1" 

    #Turn GUI mode on or off 
    $GUI = "0" 

    #Caputres the current computer name 
    $PCName = "$ENV:COMPUTERNAME" 

    #Use either the local name or domain name 
    #$User = "$ENV:UserDomainName" 
    $User = "$ENV:UserName" 

    #Timestamp 
    $Date = Get-Date -UFormat %Y-%b-%d_%H%M 

    #Computer Information 
    $MAC = ipconfig /all | Select-String Physical 
    $IP = ipconfig /all | Select-String IPv4 
    $DNS = ipconfig /all | Select-String "DNS Servers" 

    #Needed to add space after user input information 
    $EMPT = "`n" 

    #Quick capture of the computer information 
    $Info = @" 
    $EMPT 
*** COMPUTER INFORMATION *** 

    $PCName 
    $IP 
    $MAC 
    $DNS 
"@ 

    # Used to attach to the outlook program 
    $File = Get-ChildItem -Path $Dir -Filter "*.zip" | Select -Last 1 -ExpandProperty Fullname 


    $Start_Click = { 
     psr.exe /start /output $DIR\$Date-$PCName-$User.zip /maxsc $MAX /sc $SC /gui $GUI 
    } 

    $Stop_Click={ 
     psr.exe /stop 
    } 

    $Email_Click = {  
     $Outlook = New-Object -Com Outlook.Application 
     $Mail = $Outlook.CreateItem(0) 
     $Mail.To = "[email protected]" 
     $Mail.Subject = "Capture Report from " + $PCName + " " + $User + " " + $Date 
     $Mail.Body = $Problem.text + $Info 
     $Mail.Attachments.Add($File) 
     $Mail.Send() 
    } 

我不再收到錯誤,但該文件不會第一次附加。第二次它會附加,但它不是最新的.zip。我將我的整個代碼

+0

不知道您需要檢查文件是否存在並且可讀。也許需要時間來確定壓縮文件?在嘗試附加之前,使用'Test-Path'來查看zip是否存在。另外如果這仍然是一個問題,請考慮提出另一個問題,因爲我們已經解決了您當前的問題。誰會說你在解決這個問題之後不會有其他問題。 – Matt 2014-09-21 17:28:43

回答

2

按照MSDN的article它顯示了源必須是。

附接的源極。這可以是一個文件(通過用文件名 完整文件系統路徑表示)或 構成附件Outlook項目。

這意味着它不接受通配符。爲了解決這個問題,你應該使用Get-ChildItem來返回你的zip的名字。

$File = Get-ChildItem -Path $Dir -Filter "*.zip" | Select -First 1 -ExpandProperty Fullname 

這應該返回到第一個zip的完整路徑。由於GET-ChildItem回報和對象,我們使用-ExpandPropertyFullname因此您剛剛返回的完整路徑,作爲一個字符串,該文件。如果你真的只有一個文件,-First 1並不是真正需要的。在包括-First 1在內的機會將確保只附加一個文件。

從評論

我看到您有附加文件仍然問題更新。我的代碼仍然有效,但是你的.zip文件或$dir可能有問題。其中$file聲明後,我建議是這樣的:

If (! Test-Path $file){Write-Host "$file is not a valid zip file"} 

如果你願意,因爲我如果你看到你的控制檯,當你運行你的代碼,你可以使用一個popup

+0

編輯與新信息主帖 – JeremyA1 2014-09-21 15:22:03

+0

謝謝我的問題已經解決我還需要改變一些前景設置,但像一個魅力。 – JeremyA1 2014-09-21 15:40:42

+0

對不起,但它是和不工作。當我第一次運行腳本時,第一次.zip文件沒有附加,當它第二次附加剛創建的.zip文件時?我不知道爲什麼。 – JeremyA1 2014-09-21 16:16:43