deadlydog / AzureDevOps.WindowsScheduledTasks

An Azure DevOps extension for installing and uninstalling Windows Scheduled Tasks
MIT License
10 stars 3 forks source link

What is the correct configuration for an agent on a virtual machine? #13

Closed Episodex closed 4 years ago

Episodex commented 4 years ago

I'm trying to disable a scheduler task during Release pipeline. I use your Disable Windows Scheduled Task. The configuration is as follows: obraz

I get the error:

2020-06-19T06:25:09.9123030Z ##[section]Starting: Disable Windows Scheduled Task 'DEMO Alarms App'
2020-06-19T06:25:09.9227652Z ==============================================================================
2020-06-19T06:25:09.9228047Z Task         : Windows Scheduled Task - Disable
2020-06-19T06:25:09.9228359Z Description  : Disables a Windows Scheduled Task
2020-06-19T06:25:09.9228605Z Version      : 2.0.22
2020-06-19T06:25:09.9228839Z Author       : Daniel Schroeder
2020-06-19T06:25:09.9230321Z Help         : [More Information](https://github.com/deadlydog/AzureDevOps.WindowsScheduledTasks)
2020-06-19T06:25:09.9230775Z ==============================================================================
2020-06-19T06:25:09.9291545Z ##[warning]Task 'DisableWindowsScheduledTask' (2.0.22) is using deprecated task execution handler. The task should use the supported task-lib: https://aka.ms/tasklib
2020-06-19T06:25:09.9307420Z Preparing task execution handler.
2020-06-19T06:25:10.3999543Z Executing the powershell script: C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22/Code/Disable-WindowsScheduledTask-TaskEntryPoint.ps1
2020-06-19T06:25:10.8366909Z ##[error]System.Management.Automation.MethodInvocationException: Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object." ---> System.NullReferenceException: Object reference not set to an instance of an object.
2020-06-19T06:25:10.8369748Z    at System.Management.Automation.Language.InternalScriptExtent.get_Text()
2020-06-19T06:25:10.8370133Z    at System.Management.Automation.Language.Parser.SimpleNameRule()
2020-06-19T06:25:10.8370478Z    at System.Management.Automation.Language.Parser.MemberNameRule()
2020-06-19T06:25:10.8371873Z    at System.Management.Automation.Language.Parser.MemberAccessRule(ExpressionAst targetExpr, Token operatorToken)
2020-06-19T06:25:10.8372957Z    at System.Management.Automation.Language.Parser.CheckPostPrimaryExpressionOperators(Token token, ExpressionAst expr)
2020-06-19T06:25:10.8373819Z    at System.Management.Automation.Language.Parser.UnaryExpressionRule()
2020-06-19T06:25:10.8374694Z    at System.Management.Automation.Language.Parser.ArrayLiteralRule()
2020-06-19T06:25:10.8375086Z    at System.Management.Automation.Language.Parser.ExpressionRule()
2020-06-19T06:25:10.8375426Z    at System.Management.Automation.Language.Parser.PipelineRule()
2020-06-19T06:25:10.8375765Z    at System.Management.Automation.Language.Parser.StatementRule()
2020-06-19T06:25:10.8376175Z    at System.Management.Automation.Language.Parser.StatementListRule(List`1 statements, List`1 traps)
2020-06-19T06:25:10.8376609Z    at System.Management.Automation.Language.Parser.SubExpressionRule(Token firstToken)
2020-06-19T06:25:10.8377041Z    at System.Management.Automation.Language.Parser.PrimaryExpressionRule(Boolean withMemberAccess)
2020-06-19T06:25:10.8377534Z    at System.Management.Automation.Language.Parser.ParseNestedExpressions(StringExpandableToken expandableStringToken)
2020-06-19T06:25:10.8378047Z    at System.Management.Automation.Language.Parser.ExpandableStringRule(StringExpandableToken strToken)
2020-06-19T06:25:10.8378461Z    at System.Management.Automation.Language.Parser.ScanString(String str)
2020-06-19T06:25:10.8378826Z    at System.Management.Automation.AutomationEngine.Expand(String s)
2020-06-19T06:25:10.8379159Z    at CallSite.Target(Closure , CallSite , Object , String )
2020-06-19T06:25:10.8379452Z    --- End of inner exception stack trace ---
2020-06-19T06:25:10.8379978Z    at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
2020-06-19T06:25:10.8380507Z    at CallSite.Target(Closure , CallSite , Object , String )
2020-06-19T06:25:10.8380895Z    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
2020-06-19T06:25:10.8382229Z    at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
2020-06-19T06:25:10.8384681Z    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2020-06-19T06:25:12.2659389Z Disabling Scheduled Task '\DEMO Alarms App' on computer 'ECOSCADA-TEST'.
2020-06-19T06:25:12.2770292Z MSFT_ScheduledTask (TaskName = "DEMO Alarms App", TaskPath = "\")
2020-06-19T06:25:12.3149823Z ##[error]PowerShell script completed with 1 errors.
2020-06-19T06:25:12.3160380Z ##[section]Finishing: Disable Windows Scheduled Task 'DEMO Alarms App'

I tried various combinations:

I always get some kind of errors. Is there any example of correct configuration and if something should be set up additionally on a target machine?

My configuration:

Thanks!

deadlydog commented 4 years ago

Thanks for reporting the issue and giving such detailed info @Episodex . Can you run the pipeline one more time with Debug mode enabled and paste the script output here please.

To initiate debug mode for an entire release, add a variable named System.Debug with the value true to the Variables tab of a release pipeline.

deadlydog commented 4 years ago

After some Googling, I suspect this may actually be a bug with PowerShell 4. Are you able to update PowerShell on that Windows Server to 5.1?

Episodex commented 4 years ago

Thank you for very fast reply! I enabled the debug and learned from the log that despite the error the step does what it's meant to do. So it's disabling the task. Also then I added Stop and Enable steps later and they also work despite throwing same errors, so I set them to Continue on error and whole pipeline works. The downside is that it's marked as "Partially succeeded".

There are a lot of PS scripts used on this server and updating PS there could be risky. Below is complete debug log from Disable task step:

2020-06-19T08:42:25.3341502Z ##[debug]Evaluating condition for step: 'Disable Windows Scheduled Task 'DEMO Alarms App''
2020-06-19T08:42:25.3345339Z ##[debug]Evaluating: succeeded()
2020-06-19T08:42:25.3346307Z ##[debug]Evaluating succeeded:
2020-06-19T08:42:25.3348755Z ##[debug]=> True
2020-06-19T08:42:25.3349580Z ##[debug]Result: True
2020-06-19T08:42:25.3350727Z ##[section]Starting: Disable Windows Scheduled Task 'DEMO Alarms App'
2020-06-19T08:42:25.3493196Z ==============================================================================
2020-06-19T08:42:25.3493608Z Task         : Windows Scheduled Task - Disable
2020-06-19T08:42:25.3493920Z Description  : Disables a Windows Scheduled Task
2020-06-19T08:42:25.3494169Z Version      : 2.0.22
2020-06-19T08:42:25.3494418Z Author       : Daniel Schroeder
2020-06-19T08:42:25.3496061Z Help         : [More Information](https://github.com/deadlydog/AzureDevOps.WindowsScheduledTasks)
2020-06-19T08:42:25.3496526Z ==============================================================================
2020-06-19T08:42:25.3574834Z ##[warning]Task 'DisableWindowsScheduledTask' (2.0.22) is using deprecated task execution handler. The task should use the supported task-lib: https://aka.ms/tasklib
2020-06-19T08:42:25.3594684Z Preparing task execution handler.
2020-06-19T08:42:25.9727153Z Executing the powershell script: C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22/Code/Disable-WindowsScheduledTask-TaskEntryPoint.ps1
2020-06-19T08:42:25.9729129Z ##[debug]PowerShellHandler.Execute - AddCommand(C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22/Code/Disable-WindowsScheduledTask-TaskEntryPoint.ps1)
2020-06-19T08:42:25.9730267Z ##[debug]PowerShellHandler.Execute - Add inputParameters
2020-06-19T08:42:25.9731564Z ##[debug]PowerShellHandler.Execute - AddParameter(ScheduledTaskFullName=DEMO Alarms App)
2020-06-19T08:42:25.9732536Z ##[debug]PowerShellHandler.Execute - AddParameter(ComputerNames=TESTSERVER)
2020-06-19T08:42:25.9733472Z ##[debug]PowerShellHandler.Execute - AddParameter(Username=TESTSERVER\Administrator)
2020-06-19T08:42:25.9735386Z ##[debug]PowerShellHandler.Execute - AddParameter(Password=***)
2020-06-19T08:42:25.9736361Z ##[debug]PowerShellHandler.Execute - AddParameter(AuthenticationOptions=Default)
2020-06-19T08:42:25.9737349Z ##[debug]PowerShellHandler.Execute - AddParameter(ProtocolOptions=HTTP)
2020-06-19T08:42:25.9738220Z ##[debug]PowerShellHandler.Execute - AddParameter(ProtocolSkipCaCheckString=false)
2020-06-19T08:42:25.9739320Z ##[debug]PowerShellHandler.Execute - AddParameter(ProtocolSkipCnCheckString=false)
2020-06-19T08:42:25.9740199Z ##[debug]PowerShellHandler.Execute - AddParameter(ProtocolSkipRevocationCheckString=false)
2020-06-19T08:42:26.1851898Z ##[debug]PowerShellHandler.Execute - Invoke
2020-06-19T08:42:26.2836610Z ##[debug]Running on operating system 'Microsoft Windows NT 6.2.9200.0' and PowerShell version '4.0'.
2020-06-19T08:42:26.2915905Z ##[debug]Importing module 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Shared\Utilities.psm1'.
2020-06-19T08:42:26.2958394Z ##[debug]Loading module from path 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Shared\Utilities.psm1'.
2020-06-19T08:42:26.2981273Z ##[debug]Exporting function 'Get-BoolValueFromString'.
2020-06-19T08:42:26.2983667Z ##[debug]Exporting function 'Get-XmlStringFromFile'.
2020-06-19T08:42:26.2988562Z ##[debug]Importing function 'Get-BoolValueFromString'.
2020-06-19T08:42:26.2989050Z ##[debug]Importing function 'Get-XmlStringFromFile'.
2020-06-19T08:42:26.2996107Z ##[debug]Importing module 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Shared\UserInputToScheduledTaskMapper.psm1'.
2020-06-19T08:42:26.3000446Z ##[debug]Loading module from path 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Shared\UserInputToScheduledTaskMapper.psm1'.
2020-06-19T08:42:26.3027747Z ##[debug]Exporting function 'Get-ScheduledTaskNameAndPath'.
2020-06-19T08:42:26.3028751Z ##[debug]Exporting function 'Get-AccountCredentialsToRunScheduledTaskAs'.
2020-06-19T08:42:26.3029792Z ##[debug]Exporting function 'Get-WorkingDirectory'.
2020-06-19T08:42:26.3032142Z ##[debug]Exporting function 'Get-ScheduledTaskAction'.
2020-06-19T08:42:26.3033630Z ##[debug]Exporting function 'Get-ScheduledTaskTrigger'.
2020-06-19T08:42:26.3035365Z ##[debug]Exporting function 'Get-ScheduledTaskSettings'.
2020-06-19T08:42:26.3176511Z ##[debug]Exporting function 'Get-ScheduledTaskRunLevel'.
2020-06-19T08:42:26.3177635Z ##[debug]Importing function 'Get-AccountCredentialsToRunScheduledTaskAs'.
2020-06-19T08:42:26.3178913Z ##[debug]Importing function 'Get-ScheduledTaskAction'.
2020-06-19T08:42:26.3182796Z ##[debug]Importing function 'Get-ScheduledTaskNameAndPath'.
2020-06-19T08:42:26.3183448Z ##[debug]Importing function 'Get-ScheduledTaskRunLevel'.
2020-06-19T08:42:26.3184114Z ##[debug]Importing function 'Get-ScheduledTaskSettings'.
2020-06-19T08:42:26.3184741Z ##[debug]Importing function 'Get-ScheduledTaskTrigger'.
2020-06-19T08:42:26.3185342Z ##[debug]Importing function 'Get-WorkingDirectory'.
2020-06-19T08:42:26.3189645Z ##[debug]Importing module 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Shared\UserInputToWinRmSettingsMapper.psm1'.
2020-06-19T08:42:26.3196723Z ##[debug]Loading module from path 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Shared\UserInputToWinRmSettingsMapper.psm1'.
2020-06-19T08:42:26.3217754Z ##[debug]Exporting function 'Get-ComputersToConnectToOrNull'.
2020-06-19T08:42:26.3219441Z ##[debug]Exporting function 'Convert-UsernameAndPasswordToCredentialsOrNull'.
2020-06-19T08:42:26.3220267Z ##[debug]Exporting function 'Get-WinRmSettings'.
2020-06-19T08:42:26.3222046Z ##[debug]Importing function 'Convert-UsernameAndPasswordToCredentialsOrNull'.
2020-06-19T08:42:26.3222835Z ##[debug]Importing function 'Get-ComputersToConnectToOrNull'.
2020-06-19T08:42:26.3223478Z ##[debug]Importing function 'Get-WinRmSettings'.
2020-06-19T08:42:26.3234528Z ##[debug]Importing module 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Disable-WindowsScheduledTask.psm1'.
2020-06-19T08:42:26.8546381Z ##[debug]Loading module from path 'C:\agent\_work\_tasks\DisableWindowsScheduledTask_1f049c80-a651-1293-96d1-171f053c2144\2.0.22\Code\Disable-WindowsScheduledTask.psm1'.
2020-06-19T08:42:26.8563552Z ##[debug]Exporting function 'Disable-WindowsScheduledTask'.
2020-06-19T08:42:26.8565819Z ##[debug]Importing function 'Disable-WindowsScheduledTask'.
2020-06-19T08:42:26.8566754Z ##[debug]Will attempt to disable Windows Scheduled Task 'DEMO Alarms App' on 'TESTSERVER'.
2020-06-19T08:42:26.8569773Z ##[debug]About to expand the string 'Invoke-Command -ScriptBlock $disableScheduledTaskScriptBlock -ArgumentList $scheduledTaskSettings -Verbose -ComputerName $($winRmSettings.Computers) -Authentication $($winRmSettings.AuthenticationMechanism) -SessionOption $($winRmSettings.PsSessionOptions) -Credential $($winRmSettings.Credential)' to retrieve the expression in invoke.
2020-06-19T08:42:26.8614321Z ##[error]System.Management.Automation.MethodInvocationException: Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object." ---> System.NullReferenceException: Object reference not set to an instance of an object.
2020-06-19T08:42:26.8617618Z ##[debug]Processed: ##vso[task.logissue type=error;]System.Management.Automation.MethodInvocationException: Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object." ---> System.NullReferenceException: Object reference not set to an instance of an object.
2020-06-19T08:42:26.8619238Z    at System.Management.Automation.Language.InternalScriptExtent.get_Text()
2020-06-19T08:42:26.8620743Z    at System.Management.Automation.Language.Parser.SimpleNameRule()
2020-06-19T08:42:26.8621312Z    at System.Management.Automation.Language.Parser.MemberNameRule()
2020-06-19T08:42:26.8621978Z    at System.Management.Automation.Language.Parser.MemberAccessRule(ExpressionAst targetExpr, Token operatorToken)
2020-06-19T08:42:26.8622790Z    at System.Management.Automation.Language.Parser.CheckPostPrimaryExpressionOperators(Token token, ExpressionAst expr)
2020-06-19T08:42:26.8623480Z    at System.Management.Automation.Language.Parser.UnaryExpressionRule()
2020-06-19T08:42:26.8624040Z    at System.Management.Automation.Language.Parser.ArrayLiteralRule()
2020-06-19T08:42:26.8624597Z    at System.Management.Automation.Language.Parser.ExpressionRule()
2020-06-19T08:42:26.8625134Z    at System.Management.Automation.Language.Parser.PipelineRule()
2020-06-19T08:42:26.8625663Z    at System.Management.Automation.Language.Parser.StatementRule()
2020-06-19T08:42:26.8626304Z    at System.Management.Automation.Language.Parser.StatementListRule(List`1 statements, List`1 traps)
2020-06-19T08:42:26.8627004Z    at System.Management.Automation.Language.Parser.SubExpressionRule(Token firstToken)
2020-06-19T08:42:26.8627677Z    at System.Management.Automation.Language.Parser.PrimaryExpressionRule(Boolean withMemberAccess)
2020-06-19T08:42:26.8628446Z    at System.Management.Automation.Language.Parser.ParseNestedExpressions(StringExpandableToken expandableStringToken)
2020-06-19T08:42:26.8629217Z    at System.Management.Automation.Language.Parser.ExpandableStringRule(StringExpandableToken strToken)
2020-06-19T08:42:26.8629871Z    at System.Management.Automation.Language.Parser.ScanString(String str)
2020-06-19T08:42:26.8630443Z    at System.Management.Automation.AutomationEngine.Expand(String s)
2020-06-19T08:42:26.8630975Z    at CallSite.Target(Closure , CallSite , Object , String )
2020-06-19T08:42:26.8631446Z    --- End of inner exception stack trace ---
2020-06-19T08:42:26.8632249Z    at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
2020-06-19T08:42:26.8633108Z    at CallSite.Target(Closure , CallSite , Object , String )
2020-06-19T08:42:26.8633726Z    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
2020-06-19T08:42:26.8634439Z    at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
2020-06-19T08:42:26.8635155Z    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
2020-06-19T08:42:26.8636016Z ##[debug]About to invoke expression ''.
2020-06-19T08:42:27.6389229Z ##[debug]Connected to computer 'TESTSERVER' as user 'TESTSERVER\Administrator'. It is running operating system 'Microsoft Windows NT 6.3.9600.0' and PowerShell version '4.0'.
2020-06-19T08:42:27.6390490Z ##[debug]Searching for a Scheduled Task with the path '\' and name 'DEMO Alarms App'.
2020-06-19T08:42:28.4398630Z ##[debug]Processed: ##vso[task.setprogress value=100;]1/1 completed
2020-06-19T08:42:28.4399386Z Disabling Scheduled Task '\DEMO Alarms App' on computer 'TESTSERVER'.
2020-06-19T08:42:28.4624632Z MSFT_ScheduledTask (TaskName = "DEMO Alarms App", TaskPath = "\")
2020-06-19T08:42:28.4671442Z ##[debug]Processed: ##vso[task.setprogress value=100;]1/1 completed
2020-06-19T08:42:28.5028517Z ##[error]PowerShell script completed with 1 errors.
2020-06-19T08:42:28.5048337Z ##[section]Finishing: Disable Windows Scheduled Task 'DEMO Alarms App'
deadlydog commented 4 years ago

I looked over the code and it seems I might not even need to call that ExpandString method at all, so I'm going to look at just removing it. Unfortunately all of my unit tests are broken because Pester released v5 and it has a ton of breaking changes, so I'll need to get that fixed up first before I'm confident in removing the ExpandString call. I'll report back once I've been able to properly test out the changes.

deadlydog commented 4 years ago

I've just pushed out v2.0.23 of the extension which I believe should correct this issue. Let me know if you still see the error or not. Thanks :)

Episodex commented 4 years ago

I've just tested it and indeed now the pipeline succeeds :). Thanks for great support!