Problem with CmdExec in SQLAgent

  • I'm setting up a maintenance plan on a development server and I have to create a cleanup step.

    The bak files get created on the DEVSQL02 E:\ drive and then a cmdexec step moves them to a network drive - \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL\

    That part is all happening fine and dandy. The problem I'm having is figuring out how to clean up \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL.

    I want to create a step that will delete all .bak files older than 10 days. I can't use t-sql. I have to run as a proxy.

    I tried the following:

    Get-ChildItem \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL | Where {$_.LastWriteTime -lt (Get-Date).AddDays(-5)} | Remove-Item -Force

    The error says "The system cannot find the file specified", but I'm also have windows explorer open to \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL and it does really exist.

    Any ideas?

  • robin.pryor (1/6/2014)


    I'm setting up a maintenance plan on a development server and I have to create a cleanup step.

    The bak files get created on the DEVSQL02 E:\ drive and then a cmdexec step moves them to a network drive - \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL

    That part is all happening fine and dandy. The problem I'm having is figuring out how to clean up \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL.

    I want to create a step that will delete all .bak files older than 10 days. I can't use t-sql. I have to run as a proxy.

    I tried the following:

    Get-ChildItem \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL | Where {$_.LastWriteTime -lt (Get-Date).AddDays(-5)} | Remove-Item -Force

    The error says "The system cannot find the file specified", but I'm also have windows explorer open to \\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL and it does really exist.

    Any ideas?

    Permission issue?

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Yes - permission issue. The dev boxes can't access hqdat directly. Only via a proxy that's set up with one of the sys admin's credentials.

    For example, when the backup is created, it's written to the sql server box's E drive. Then in a separate step, set up as an Operating System (cmdExec) operation and run as that proxy, the bak files are all moved to hqdat:

    IF EXIST "E:\BACKUPS\Full\*.bak" (move E:\BACKUPS\Full\*.bak "\\hqdatdom01.ecotoh.net\Dev Backups\DEVSQL02\FULL\")

    So, I also need a separate step to clean up hqdat. I just don't know the syntax to use.

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

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