首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带Powershell的Attachments.Add通配符

带Powershell的Attachments.Add通配符
EN

Stack Overflow用户
提问于 2014-09-21 11:52:43
回答 1查看 1.8K关注 0票数 0

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

代码语言:javascript
复制
#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,而不是最近的。我加了我的全部代码

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-21 13:19:25

根据msdn 文章,它显示了源需要的是什么。

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

这意味着它不接受通配符。为了解决这个问题,您应该使用Get-ChildItem返回您的zip的名称。

代码语言:javascript
复制
$File = Get-ChildItem -Path $Dir -Filter "*.zip" | Select -First 1 -ExpandProperty Fullname

这将返回到第一个zip的完整路径。由于Get返回和对象,我们在Fullname上使用了Fullname,所以您只需将完整的路径作为字符串返回到文件。如果您确实只有一个文件,则不需要-First 1。在不太可能的情况下,包括-First 1将确保只附加一个文件。

从注释更新

我发现您仍然有附加文件的问题。我的代码仍然有效,但是您的.zip文件或$dir可能有问题。在声明$file之后,我建议如下所示:

代码语言:javascript
复制
If (! Test-Path $file){Write-Host "$file is not a valid zip file"}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25958903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档