gpt4 book ai didi

Powershell 脚本无法将 Register-ScheduledJob 作为 Azure 资源管理器 CustomScriptExtension 执行

转载 作者:行者123 更新时间:2023-12-03 04:42:05 24 4
gpt4 key购买 nike

我有一个用于 Azure 虚拟机规模的部署模板,具有以下扩展配置文件:

"extensionProfile": {
"extensions": [
{
"name": "customScript",
"location": "[resourceGroup().location]",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"settings": {
"fileUris": [
"[concat(parameters('customInstallScriptLocation'), parameters('customInstallScriptFileName'))]"
]
},
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"protectedSettings": {
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File \"', parameters('customInstallScriptFileName'), '\" -adminUsername ', parameters('adminUsername'), ' -adminPassword ', parameters('adminPassword'))]"
}
}
}
]
}

部署 vmss 时,执行自定义脚本,Invoke-WebRequest 和 Start-Process 等命令可以正常工作。但 Register-ScheduledJob 不起作用。当我连接到远程桌面并手动运行此安装脚本时,一切正常。

这是不手动运行时失败的代码:

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:45
Register-ScheduledJob -Trigger $trigger -FilePath $installDir\natPuncherStartupScript.ps1 -Name NPSSOnStartup >> $log
logAndClearErrors $true

错误是:

Register-ScheduledJob : An error occurred while registering scheduled jobdefinition NPSSOnInstall to the Windows Task Scheduler.  The Task Scheduler error is: (32,4):UserId:.

我也尝试过:

$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:45
Register-ScheduledJob -Credential $credential -Trigger $trigger -FilePath $installDir\natPuncherStartupScript.ps1 -Name NPSSOnStartup >> $log
logAndClearErrors $true

但现在错误显示用户名/密码无效。此外,当从远程桌面手动运行它时,效果很好。

我错过了什么?为什么我无法通过自定义脚本扩展安排作业?

最佳答案

如果您想让 vmextension 注册它,您需要以 LocalSystem 帐户登录。

Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User "System" -Settings $settings

Stackoverflow link of this issue I had

关于Powershell 脚本无法将 Register-ScheduledJob 作为 Azure 资源管理器 CustomScriptExtension 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41281482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com