有人可以帮我做这个电子邮件打印功能吗?

问题描述 投票:0回答:1
# Function to print emails
function Print-Emails {
    # Connect to the Exchange Server
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://XXX17.XXX.local/PowerShell/ -Authentication Kerberos
    Import-PSSession $session -DisableNameChecking

    # Retrieve new emails that have arrived since the last check
    $lastCheckTime = Get-ItemPropertyValue -Path "HKCU:\Software\MyScript" -Name "LastCheckTime" -ErrorAction SilentlyContinue
    if (-not $lastCheckTime) {
        # First execution of the script, no previous check time available
        $lastCheckTime = (Get-Date).AddMinutes(-5) # Assume: Check emails of the last 5 minutes
    }
    
    # Retrieve the new emails from the mailbox
    $emails = Get-MailboxFolderStatistics -Identity "[email protected]" -FolderScope Inbox | Select-Object -ExpandProperty Items | Where-Object {$_.ReceivedTime -ge $lastCheckTime}

    # Check if there are new emails
    if ($emails) {
        foreach ($email in $emails) {
            # Print the email
            Write-Host "Printing email from $($email.From) with subject $($email.Subject)"
            # Adjust the print command here to send the email directly to the printer
            Out-Printer -Name "TOSHIBA XXX " -InputObject $email
        }
    }

    # Update last check time
    Set-ItemProperty -Path "HKCU:\Software\MyScript" -Name "LastCheckTime" -Value (Get-Date)

    # End session
    Remove-PSSession $session
}

# Infinite loop that runs every 5 minutes
while ($true) {
    # Check and print emails
    Print-Emails
    # Set wait time (300 seconds = 5 minutes)
    Start-Sleep -Seconds 300
}
$emails = Get-MailboxFolderStatistics -Identity "[email protected]" -FolderScope Inbox | Select-Object -ExpandProperty Items | Where-Object {$_.ReceivedTime -ge $lastCheckTime}

错误消息表明代码未正确格式化为代码,尽管我尝试正确缩进它。如何解决此问题并在 Exchange Server 上使用 PowerShell 成功检索和打印电子邮件?


`

powershell
1个回答
0
投票

这是完整的错误代码:

New-PSSession : [xxx17.xxx.local] 连接到远程服务器“xxx17.xxx.local”时发生以下错误:WinRM 无法处理请求。出现以下错误 使用 Kerberos 身份验证时发生:Kerberos 无法识别计算机“XXX17.XXX.local”。检查电脑是否在网络上,是否 指定的名称拼写是否正确以及用于访问计算机的 Kerberos 配置是否正确。配置 Kerberos 时经常出现的问题是 未为目标配置格式为“HTTP/XXX17.XXX.local”的 SPN。如果不需要 Kerberos,请指定 Negotiate 身份验证机制并提交 重复该过程。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 行数:4 个字符:16

  • ... $session = New-PSSession -ConfigurationName Microsoft.Exchange -Conn ...
  •   + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
      + FullyQualifiedErrorId : NetworkPathNotFound,PSSessionOpenFailed
    

Import-PSSession:无法验证“Session”参数的参数。参数为 NULL。为参数提供有效值并运行命令 再次。 行数:5 个字符:22

  • 导入-PSSession $session -DisableNameChecking
  •   + CategoryInfo : InvalidData: (:) [Import-PSSession], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportPSSessionCommand
    
    

Get-MailboxFolderStatistics:名称“Get-MailboxFolderStatistics”未被识别为 cmdlet、函数、脚本文件或可执行程序的名称。 检查名称的拼写或路径是否正确(如果包含),然后重复该过程。 行数:15 字符:15

  • $emails = Get-MailboxFolderStatistics -Identity "directprint@XXX....
  •   + CategoryInfo : ObjectNotFound: (Get-MailboxFolderStatistics:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
    
    

Set-ItemProperty:找不到路径“HKCU:\Software\MyScript”,因为它不存在。 行数:28 个字符:5

  • Set-ItemProperty -路径“HKCU:\Software\MyScript”-名称“LastCheck ... +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ ~~~~~~~~~~~~~~~+ CategoryInfo : ObjectNotFound: (HKCU:\Software\MyScript:String) [Set-ItemProperty], ItemNotFoundException+ ExcellentQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommandRemove- PSSession:无法验证“Id”参数的参数。参数为 NULL。为参数指定有效值并重新执行命令。在线行:31 个字符:22+ Remove-PSSession $session+ ~~~~~~~+ CategoryInfo : InvalidData: (:) [Remove-PSSession ], ParameterBindingValidationException+ FullQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.RemovePSSessionCommand
© www.soinside.com 2019 - 2024. All rights reserved.