Attachments.Add通配符使用PowerShell

问题描述:

我有动态信息(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

按照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