我有一个带有动态信息的ZIP文件(Report_ PC名称-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,而不是最近的。我加了我的全部代码
发布于 2014-09-21 13:19:25
根据msdn 文章,它显示了源需要的是什么。
附件的来源。这可以是一个文件(由带有文件名的完整文件系统路径表示),也可以是构成附件的Outlook项。
这意味着它不接受通配符。为了解决这个问题,您应该使用Get-ChildItem返回您的zip的名称。
$File = Get-ChildItem -Path $Dir -Filter "*.zip" | Select -First 1 -ExpandProperty Fullname这将返回到第一个zip的完整路径。由于Get返回和对象,我们在Fullname上使用了Fullname,所以您只需将完整的路径作为字符串返回到文件。如果您确实只有一个文件,则不需要-First 1。在不太可能的情况下,包括-First 1将确保只附加一个文件。
从注释更新
我发现您仍然有附加文件的问题。我的代码仍然有效,但是您的.zip文件或$dir可能有问题。在声明$file之后,我建议如下所示:
If (! Test-Path $file){Write-Host "$file is not a valid zip file"}如果您愿意,因为我不知道您在运行代码时是否看到了控制台,所以可以使用弹出窗口
https://stackoverflow.com/questions/25958903
复制相似问题