附件。使用Powershell添加通配符

时间:2014-09-21 11:52:44

标签: powershell powershell-v2.0

我有一个用动态信息生成的ZIP文件(Report_ PC Name-Date_User)。但是,当我去附加文件时,我无法使用通配符。此目录中只有一个ZIP文件,因此使用通配符不会附加任何其他ZIP文件。

#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 = "deaconf19@gmail.com"
        $Mail.Subject = "Capture Report from " + $PCName + " " + $User + " " + $Date
        $Mail.Body = $Problem.text + $Info
        $Mail.Attachments.Add($File)
        $Mail.Send()
    }

我不再收到错误,但文件第一次不会附加。它将第二次附加,但它确实是之前的.zip不是最新的.zip。我添加了我的整个代码

1 个答案:

答案 0 :(得分:2)

根据msdn article,它显示了源所需的内容。

  

附件的来源。这可以是一个文件(由...表示)   具有文件名的完整文件系统路径)或Outlook项目   构成附件。

这意味着它不接受通配符。为了解决这个问题,您应该使用Get-ChildItem来返回您的邮政编码。

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

那应该返回第一个zip的完整路径。由于Get-ChildItem返回并且对象我们在-ExpandProperty上使用Fullname,因此您只需将完整路径(作为字符串)返回到文件。如果您真的只有一个文件,则不需要-First 1。在包含-First 1的可能性中,将确保仅附加一个文件。

从评论中更新

我发现您在处理文件时遇到问题。我的代码仍然有效,但您的.zip文件或$dir可能存在问题。在声明$file之后我会建议这样的事情:

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

如果您愿意,因为我不知道您在运行代码时是否看到了控制台,您可以使用popup