如何解决 PowerShell 错误“无法连接到 CIM 服务器。指定的服务不作为已安装的服务存在”

如何解决 PowerShell 错误“无法连接到 CIM 服务器。指定的服务不作为已安装的服务存在”

我正在尝试运行一个从 .bat 文件调用的简单 PowerShell 脚本。该脚本的目的是更新现有 Windows 任务计划程序任务的属性,以便它在 .bat 文件指定的时间运行并唤醒计算机以运行该任务。(.bat 无法使用 SCHTASKS.exe 命令自行执行此操作,因为 SCHTASKS 无法正确设置计算机以唤醒,即使任务中已经设置了唤醒条件。我推测 Windows 任务计划程序不够智能,无法在 SCHTASKS 更新任务的触发时间时识别出需要对主板的 RTC 进行编程。)PowerShell 脚本运行但失败并给出我的问题中的错误消息。

我在 Google 上搜索了该错误消息,只找到了对 Azure 毫无帮助的引用,但这与我无关,因为我只是在使用普通的 Windows 10 家庭版电脑。

我是 PowerShell 新手。(我经常使用 .bat。)这是 PowerShell 脚本:

$TName = $args[0]
$TTime = $args[1]
$TTrig = New-ScheduledTaskTrigger -Once -At $TTime
$Settings = New-ScheduledTaskSettingsSet -Hidden -WakeToRun
Set-ScheduledTask -TaskName $TName -Trigger $TTrig -Settings $Settings

第 3 行和第 4 行分别给出了我的问题中包含的错误消息。(第 5 行给出了有关 -Trigger 参数问题的错误消息,但这并不奇怪,因为第 3 行失败了。)

如果我手动将类似的行逐行粘贴到 PowerShell ISE gui(以管理员身份运行)中,我不会收到错误消息,并且它会更新任务。

以下是调用 PowerShell 脚本以管理员权限运行的 .bat 行:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%WAKESCRIPT%"""" """"%WAKETASK%"""" """"%WAKETIME%"""" ' -Verb RunAs}"

其中 WAKESCRIPT 是 PowerShell 脚本的路径和名称,WAKETASK 是要修改的 Windows 任务计划程序任务的名称,WAKETIME 只是我在本次调试中使用的常量字符串 14:00。我在以下位置了解了此 .bat 行的基础知识: https://blog.danskingdom.com/allow-others-to-run-your-powershell-scripts-from-a-batch-file-they-will-love-you-for-it/

有人能帮我解决这个问题吗?提前谢谢了。

答案1

当通过 PowerShell 处理 ST 时,必须采取一些步骤才能达到所需的目标。使用有关该主题的分步文章阅读已发布代码的详细信息。

使用 PowerShell 创建计划任务

创建任务操作

# Get-LatestAppLog.ps1

## Define the log file
$logDir = 'c:\temp'
$logFile = "$logDir\AppLog_$(Get-Date -format 'yyyy-mm-dd_hh-mm-ss-tt').xml"

## Get the ten latest application log events
Get-WinEvent -LogName application -MaxEvents 10 |
Export-CliXml $logFile -Force

# Create a new task action
$taskAction = New-ScheduledTaskAction `
    -Execute 'powershell.exe' `
    -Argument '-File C:\scripts\Get-LatestAppLog.ps1'
$taskAction


# Create a new trigger (Daily at 3 AM)
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 3PM
$tasktrigger

注册新的 PowerShell 计划任务

# The name of your scheduled task.
$taskName = "ExportAppLog"

# Describe the scheduled task.
$description = "Export the 10 newest events in the application log"

# Register the scheduled task
Register-ScheduledTask `
    -TaskName $taskName `
    -Action $taskAction `
    -Trigger $taskTrigger `
    -Description $description

运行任务

Get-ScheduledTaskInfo -TaskName ExportAppLog
Start-ScheduledTask -TaskName ExportAppLog

# Set the task principal's user ID and run level.
$taskPrincipal = New-ScheduledTaskPrincipal -UserId 'DEVPC\svcTask' -RunLevel Highest
# Set the task compatibility value to Windows 10.

$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8

# Update the task principal settings
Set-ScheduledTask -TaskName 'ExportAppLog' -Principal $taskPrincipal -Settings $taskSettings

# Update the task user account and password
Set-ScheduledTask -TaskName 'ExportAppLog' -User $taskPrincipal.UserID -Password 'PASSWORD'

改变触发器

$taskTrigger1 = New-ScheduledTaskTrigger -Daily -At 4:30PM
$taskTrigger2 = New-ScheduledTaskTrigger -Daily -At 1:00AM
Set-ScheduledTask -TaskName 'ExportAppLog' -Trigger $taskTrigger1,$taskTrigger2 -User 'DEVPC\svcTask' -Password 'PASSWORD'

备份计划任务

# Export the scheduled task object to XML
Get-ScheduledTask -TaskName 'ExportAppLog' | Export-Clixml c:\temp\ExportAppLog.xml

删除计划任务

# Unregister the scheduled task
Unregister-ScheduledTask -TaskName 'ExportAppLog' -Confirm:$false
Get-ScheduledTask -TaskName 'ExportAppLog'

恢复计划任务

# Import the Schedule Task backup
$stBackup = Import-Clixml -Path c:\temp\ExportAppLog.xml



# Reset the logon type to "Run only when the user is logged on."
$stBackup.Principal.LogonType = 'Interactive'


# Create a new Scheduled Task object using the imported values
$restoreTask = New-ScheduledTask `
    -Action $stBackup.Actions `
    -Trigger $stBackup.Triggers `
    -Settings $stBackup.Settings `
    -Principal $stBackup.Principal

Register-ScheduledTask `
    -TaskName 'ExportAppLog_restored' `
    -InputObject $restoreTask `
    -User 'DEVPC\svcTask' `
    -Password 'PASSWORD'

答案2

相关内容