WMI打印和Bespoke Powershell脚本

时间:2016-04-04 13:55:43

标签: powershell printing outlook wmi

$OutputFolder = 'C:\tests';
(New-Object -ComObject WScript.Network).SetDefaultPrinter('Xerox-Colour');%{sleep 5;$_};
$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  
Start-Process –FilePath “c:\tests\*.xlsx” –Verb Print  
Start-Process –FilePath “c:\tests\*.doc” –Verb Print   
Start-Process –FilePath “c:\tests\*.xls” –Verb Print   
Start-Process –FilePath “c:\tests\*.pptx” –Verb Print  
Start-Process –FilePath “c:\tests\*.ppt” –Verb Print 
Start-Process –FilePath “c:\tests\*.xls” –Verb Print 
Start-Process –FilePath “c:\tests\*.msg” –Verb Print 
Start-Process –FilePath “c:\tests\*.xlsm” –Verb Print 
Start-Process –FilePath “c:\tests\*.pdf” –Verb Print;

%{sleep 10;$_};


$ns = $outlook.GetNameSpace("MAPI"); 
$inbox = $ns.GetDefaultFolder($olFolderInbox);
$SubFolders = $inbox.Folders

foreach ($SubFolder in $SubFolders)
{
$SubFolder.Items | % {$_.delete()}
};

Remove-Item -Path C:\tests\*.*;
(New-Object -ComObject WScript.Network).SetDefaultPrinter('xerox-BandW')

伙计这个剧本很乱,你能看到我想做什么吗?这将从outlook子文件夹中获取文档,并将它们作为附件打印出来(它还将标题拉为.jpg和.png,但我将其删除)问题是,它似乎可以从B&更改默认打印机; W to Color以彩色打印,但它总是出现在B& W中,我确信它归结为底线的括号:(New-Object -ComObject WScript.Network).SetDefaultPrinter('xerox-BandW')意味着它在{{1}之前运行}命令运行。

任何想法为什么这仍然出现在B& W并且它暂时不会像它应该切换到Color? B& W是默认打印机。

PSVersion 5.0 W7 64位

澄清:Windows 7操作系统 NOT 包含PowerShell中Start-Process cmdlet支持的操作系统级功能。因此,我们需要一种替代方法来尝试更改打印机的颜色选项。

0 个答案:

没有答案