【发布时间】:2018-09-29 14:42:12
【问题描述】:
我想编写脚本,为 Exchange 服务器上的某些用户启用邮件转发,并在指定时间将其关闭。
但是当$script_bloc 在作业中执行时:New-PSSession 返回 null 而没有任何错误。
我可以传递给New-PSSession 硬编码凭据,在这种情况下,它可以按我的预期工作,但我不想这样做,因为我的密码可能会在作业开始时过期。
知道为什么New-PSSession 不适合工作吗?以及如何让它发挥作用?
$user = 'username1'
$fwdto = 'username2'
$remove_date = '19.04.2018 08:33:00'
Set-Mailbox -Identity $user -DeliverToMailboxAndForward $true -ForwardingAddress $fwdto
$job_date=[datetime]::Parse($remove_date)
$job_date=[datetime]::Now.AddSeconds(20) #for test
$trigger = New-JobTrigger -Once -At $job_date
$job_name="rfw_" + $user
$script_block = {
param($user_param,$job_name_param)
Start-Transcript $env:USERPROFILE\jobs\$job_name_param.log -Verbose -Append
$PSOptions = New-PSSessionOption –SkipCACheck –SkipRevocationCheck -SkipCNCheck
$sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.company.org/PowerShell/ –SessionOption $PSOptions
Import-PSSession $sess | Out-Default
Set-Mailbox -Identity $user_param -DeliverToMailboxAndForward $false -ForwardingAddress $null | Out-Default
Remove-PSSession $sess | Out-Default
Unregister-ScheduledJob $job_name_param -Force
Stop-Transcript
}
Register-ScheduledJob -Trigger $trigger -Name $job_name -ScriptBlock $script_block -ArgumentList $user, $job_name
【问题讨论】:
-
您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。更重要的是,请阅读the Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples。
标签: powershell powershell-remoting powershell-jobs