【问题标题】:new-pssession in scheduled job预定作业中的新会话
【发布时间】: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

【问题讨论】:

标签: powershell powershell-remoting powershell-jobs


【解决方案1】:

当您在 PowerShell 中注册作业时,它会在 Microsoft\Windows\PowerShell 文件夹下的任务计划程序中创建为任务。因此,如果未明确提及凭据,则任何需要在任务中进行身份验证的操作都将失败并出现拒绝访问错误。

您可以通过在 ScriptBlock 中添加 Try{} Catch{} 来测试它。

您可以使用Register-ScheduledJob cmdlet 的-Credential 参数来完成您的任务。然后任务将使用该凭据进行任何操作,

$script_block = {
    param($user_param,$job_name_param)
Try{
    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 -ErrorAction Stop
    $sess > C:\Session.log
    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 
}
Catch{
    $_ > c:\Error.log
}
}

Register-ScheduleJob cmdlet 中使用带有和不带有 -Credetial 参数的上述 ScriptBlock。你可以看到区别。

【讨论】:

  • 代码$sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.company.org/PowerShell/ –SessionOption $PSOptions 不会在工作中引发任何异常。它返回 null 而没有任何错误。而且我已经被告知我可以传递硬编码的凭据,但不要这样做,因为密码可能在工作开始时过期。当我在 Powershell ISE 中启动 $script_block 时:Invoke-Command -ScriptBlock $script_block 它按预期工作。而且我不明白为什么它在工作中不起作用,因为工作是在我的帐户下运行的,而不是系统。
  • 你看不到错误,因为你没有抓住它,执行我通过预定作业发布的代码并查看结果
  • 我向你保证,正如你所提到的,我将我的代码包装在 try catch 中,但它仅在 Import-PSSession 尝试导入 $null 时抛出
  • 是的,我写在第一篇文章中:I can pass to New-PSSession hardcoded credentials, and in that case it works as I expect
  • 不适用于New-PSSession,我说的是Register-ScheduledJob cmdlet 和-Credential
猜你喜欢
  • 2015-04-01
  • 1970-01-01
  • 2016-03-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
相关资源
最近更新 更多