简体   繁体   English

使用powershell创建计划任务,而不是存储密码

[英]Create scheduled task with powershell and not store password

I am trying to make a powershell script to create a scheduled task to run a command on demand. 我正在尝试创建一个PowerShell脚本来创建计划任务以按需运行命令。 the following is the code I have thus far. 以下是我到目前为止的代码。

$taskName = "TestTask"
$taskPath = "<taskdir>"
$user = "$env:USERDOMAIN\$env:USERNAME"
$response = Read-host "What's your password?" -AsSecureString 
$password=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($response))
$action = New-ScheduledTaskAction -Execute "task.cmd"
$settings = New-ScheduledTaskSettingsSet -Compatibility WIN8
$principal = New-ScheduledTaskPrincipal -UserId Administrator -LogonType S4U -RunLevel Highest
$inputObject = New-ScheduledTask -Action $action -Principal $principal -   Settings $settings 
Register-ScheduledTask -TaskName $taskName -taskpath $taskPath -InputObject $inputObject -user $user -password $Password

This works well to setup the task the only thing I am looking for is to be able to run the task from any user in the admin group while the Administrator (who the task is run as) is not logged in and I don't want to store the password. 这很适合设置任务我唯一能找到的是能够从管理员组中的任何用户运行任务,而管理员(任务运行的人)没有登录,我不想要存储密码。 When I set up a task through the GUI I can select the button to run whether the user is logged in or not which the above code achieves. 当我通过GUI设置任务时,我可以选择运行的按钮,无论用户是否登录,上述代码都可以登录。 But it won't check the box to say do not store the password. 但它不会检查框中说不存储密码。 When I run the following command in powershell to look at the properties of the created task the output is as followed 当我在powershell中运行以下命令来查看创建的任务的属性时,输出如下

get-scheduledtask testtask | select -ExpandProperty principal

DisplayName         :
GroupId             :
Id                  : Author
LogonType           : Password
RunLevel            : Highest
UserId              : WIN-REH2TQO7H7S\Administrator
ProcessTokenSidType : Default
RequiredPrivilege   :
PSComputerName      :

If I run the same command on a task I created through the GUI with the don't save password check I get the following 如果我通过GUI创建的任务运行相同的命令,并且不保存密码检查,我得到以下内容

get-scheduledtask testtask | select -ExpandProperty principal

DisplayName         :
GroupId             :
Id                  : Author
LogonType           : S4U
RunLevel            : Highest
UserId              : WIN-REH2TQO7H7S\Administrator
ProcessTokenSidType : Default
RequiredPrivilege   :
PSComputerName      :

The only difference I have found is LogonType being S4U vs. Password. 我发现的唯一区别是LogonType是S4U与密码。 So in my powershell I added 所以在我的powershell中我添加了

$principal = New-ScheduledTaskPrincipal -UserId Administrator -LogonType S4U -RunLevel Highest

But even with the LogonType set to S4U in the code it still sets it to password when it creates it. 但即使代码中的LogonType设置为S4U,它仍然会在创建时将其设置为密码。 I can go into the GUI and physically change the task after the powershell code runs and check that box at which point it is set correct. 我可以进入GUI并在powershell代码运行后以物理方式更改任务,并检查该框是否设置正确。 But does anyone have any idea as to why the code wont do it ? 但有没有人知道为什么代码不会这样做? or if I missed something ? 或者如果我错过了什么?

Thanks for any help you can provide, 感谢您的任何帮助,您可以提供,

Mack. 麦克。

I was looking for a similar thing, using the local admin account with 'Do not store password' enabled. 我正在寻找类似的东西,使用本地管理员帐户启用“不存储密码”。 Your post got me on the right track and mine now works. 你的帖子让我走在正确的轨道上,现在我的工作正常。

Try this instead: 试试这个:

Register-ScheduledTask -TaskName $taskName -taskpath $taskPath -InputObject $inputObject

If that fails, try putting the principal in the line and take it out of the InputObject: 如果失败,请尝试将主体放在行中并将其从InputObject中取出:

Register-ScheduledTask -TaskName $taskName -Action $action -Principal $Principal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM