问题描述
我有一个脚本是从其他脚本中删除的.它可以工作,除非我对Powershell相当陌生,并且是系统管理员而不是开发人员(但请读完我的屁股).我可以让scrtipt从Outlook的收件箱中下载附件,但需要它从子文件夹中下载附件:
I have a script i scraped from other scripts. It works except i am quite new to powershell and am a sys admin not a dev, (but reading my ass off). I can get the scrtipt to work downloading attachments from inbox in outlook but need it to download attachments from a subfolder instead:
############################# Outlook Call ##############################
$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox)
$messages = $inbox.items
write-host $messages.count
$messcount = $messages.count
foreach($message in $messages){
##############Save Attachments################
$filepath = "c:\attachments\"
$message.attachments|foreach {
Write-Host $_.filename
$attr = $_.filename
$_.saveasfile((Join-Path $filepath $_.filename))
$a = $_.filename
If ($a.Contains("")) {
$_.saveasfile((Join-Path $filepath $a))
}
}
}
###########END##########
有任何想法吗?将不胜感激.
Any Ideas anyone? Would be massively grateful.
推荐答案
$OutputFolder = 'C:\tests';
$ErrorActionPreference= 'silentlycontinue'
$outlook = New-Object -ComObject Outlook.Application;
$olFolderInbox = 6;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.GetDefaultFolder($olFolderInbox);
$inbox.Folders `
| ? Name -eq 'colour' `
| % Items `
| % Attachments `
| % {
$OutputFileName = Join-Path -Path $OutputFolder -ChildPath $_.FileName;
if (Test-Path $OutputFileName) {
$FileDirectoryName = [System.IO.Path]::GetDirectoryName($OutputFileName);
$FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($OutputFileName);
$FileExtension = [System.IO.Path]::GetExtension($OutputFileName);
for ($i = 2; Test-Path $OutputFileName; $i++) {
$OutputFileName = "{0} ({1}){2}" -f (Join-Path -Path $FileDirectoryName -ChildPath $FileNameWithoutExtension), $i, $FileExtension;
}
}
Write-Host $OutputFileName;
$_.SaveAsFile($OutputFileName)
}
Remove-Item -Path C:\tests\*.jpg
Remove-Item -Path C:\tests\*.png
Start-Process –FilePath "c:\tests\*.docx" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.xlsx" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.doc" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.xls" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.pptx" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.ppt" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.xls" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.msg" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.xlsm" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.rtf" –Verb Print -PassThru
Start-Process –FilePath "c:\tests\*.pdf" –Verb Print -PassThru `
%{sleep 3;$_}
Remove-Item -Path C:\tests\*.*
这完成了我所需要的,我可能会在每个步骤之后设置一个kill进程,以确保没有文档被卡住.
This accomplishes what i need, i might put a kill process in after each one just to make sure there is no doc stuck.
但是-剩下要做的1件事是我需要将其写入非默认打印机,而不更改现有的默认打印机吗?它在使用默认打印机运行vbscript的服务器上运行,因此如果更改,它将使其混乱.
But - The 1 thing left to accomplish is i need this to write to a non default printer and not change the existing default? This runs on a server with a vbscript already running utilizing default printer so it will mess that up if it changes.
这篇关于转换Powershell脚本保存Outlook附件以将其从收件箱而不是收件箱子文件夹中保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!