EXEC xp_cmdshell 'Powershell.exe

  • HI,

        Have a problem not sure if its permission issue or something else, any suggestions ? 

     I am trying to run powershell through EXEC xp_cmdshell to delete scheduled task from ssms, when I use , and 
    I can run Unregister-ScheduledTask ,Get-ScheduledTask through power-shell it runs well no issues but when i run through Xp_cmdshell i face problem,

    EXEC xp_cmdshell 'Powershell.exe "Unregister-ScheduledTask ..........
    EXEC xp_cmdshell 'Powershell.exe "Get-ScheduledTask..............
    ends up with error like below 

    get-ScheduledTask : No MSFT_ScheduledTask objects found with property
    'TaskName' equal to ''. Verify the value of the property and retry.
    At line:1 char:1
    + get-ScheduledTask
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo    : ObjectNotFound: (:String) [Get-Schedul
     edTask], CimJobException
      + FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Get-Schedul
     edTask
    NULL

    where as
     Get-WmiObject Win32_Computersystem works
    EXEC xp_cmdshell 'Powershell.exe " Get-WmiObject Win32_Computersystem"'

  • Do you have a proxy account setup for XP_CMDSHELL?
    https://msdn.microsoft.com/en-us/library/ms190359.aspx
    This defines what windows account that XP_CMDSHELL will run as, so you can ensure it's an account that has permissions to work with scheduled OS tasks.

  • rajju_vini - Thursday, March 16, 2017 12:24 PM

    HI,

        Have a problem not sure if its permission issue or something else, any suggestions ? 

     I am trying to run powershell through EXEC xp_cmdshell to delete scheduled task from ssms, when I use , and 
    I can run Unregister-ScheduledTask ,Get-ScheduledTask through power-shell it runs well no issues but when i run through Xp_cmdshell i face problem,

    EXEC xp_cmdshell 'Powershell.exe "Unregister-ScheduledTask ..........
    EXEC xp_cmdshell 'Powershell.exe "Get-ScheduledTask..............
    ends up with error like below 

    get-ScheduledTask : No MSFT_ScheduledTask objects found with property
    'TaskName' equal to ''. Verify the value of the property and retry.
    At line:1 char:1
    + get-ScheduledTask
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo    : ObjectNotFound: (:String) [Get-Schedul
     edTask], CimJobException
      + FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Get-Schedul
     edTask
    NULL

    where as
     Get-WmiObject Win32_Computersystem works
    EXEC xp_cmdshell 'Powershell.exe " Get-WmiObject Win32_Computersystem"'

    The error tells you exactly what is wrong.  Without seeing the whole command, it is hard to diagnose the syntax error, but it looks to me like a syntax error.
    The error states that there is no scheduled task objects found that have the property "TaskName" with a value of "".  That is when it runs the get-ScheduledTask command.
    So it could be as Chris Harshman pointed out that you lack permissions to read the tasks, or it could be the syntax error.

    Is there a reason you need to use SSMS to delete a windows scheduled tasks though?  That feels like a lot of hoops to jump through and a lot of points of failure for a task you could probably have residing entirely inside a powershell script.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply