|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, May 29, 2013 1:01 PM
Points: 238,
Visits: 2,344
|
|
I am totally new to powershell(just 2 days) so please bear my ignorance.Here is the code I am trying to execute as a sql agent job that basically deletes files over 2 days old.
function LoopFolders([string]$path,[string]$logpath) { $fs = new-object -com scripting.filesystemobject $folder = $fs.getfolder($path) $today = get-date -uformat "%Y_%m_%d" $log = $logpath + "Deleted_" + $today + ".log" foreach ($i in $folder.files) { if($folder.path -contains "Master" -or $folder.path -contains "Model" -or $folder.path -contains "Certificates") {break} else { $dt = ((Get-Date) - $i.DateLastModified).Days if ($dt -gt 2 -and $i.PsISContainer -ne $True ) { $deleted = "Deleting - " + $i.path + " - Last Write Time - " + $i.DateLastModified add-content $log -value $deleted remove-item $i.path } } } foreach ($i in $folder.subfolders) { LoopFolders($i.path) } } LoopFolders "C:\DatabaseBackUps" "C:\DatabaseBackUps\BackUpDeleteLogs" LoopFolders "\\10.1.1.10\DatabaseBackups" "\\10.1.1.10\Database\BackUpDeleteLogs" The problem here is that the code works fine for local path but errors out for network path with the following message.
The corresponding line is 'remove-item $i.path'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Invalid Path: '\\10.1.1.10\database\LogBackUp\PRODUCTION_ALLIED\PRODUCTION_ALLIED_backup_2012_06_01_093000_6722391.trn'. '. Process Exit Code -1. The step failed.
The same question has been asked here
http://social.technet.microsoft.com/Forums/da-DK/winserverpowershell/thread/a1a1ba21-0bd5-4b53-8838-5930f9599024
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 6,826,
Visits: 11,951
|
|
Try something like this. Running it with the -WhatIf will only report what would be deleted by this command. Remove the -WhatIf when you're ready for it to do work.
################################################# #init variables
$directory = "\\servername\sharename" $retentionDate = (Get-Date).AddDays(-2)
################################################# # nothing below here needs to change
Get-ChildItem $directory |? {($_.PSIsContainer -eq $false) -and ($_.LastWriteTime -lt $retentionDate)} | Remove-Item -WhatIf
Add -Recurse to the Get-ChildItem call to also delete old files from sub-directories:
################################################# #init variables
$directory = "\\servername\sharename" $retentionDate = (Get-Date).AddDays(-2)
################################################# # nothing below here needs to change
Get-ChildItem $directory -Recurse |? {($_.PSIsContainer -eq $false) -and ($_.LastWriteTime -lt $retentionDate)} | Remove-Item -WhatIf
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 11:06 AM
Points: 14,
Visits: 47
|
|
| Same problem for me - did you ever get this figured out?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 6,826,
Visits: 11,951
|
|
bwperrin (8/17/2012) Same problem for me - did you ever get this figured out? Did you try the code I provided?
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 11:06 AM
Points: 14,
Visits: 47
|
|
Yes, I did the following from a SQL Agent PowerShell job step: $src = "\\serverpath\filename" $dst = "e:\path\filename" Copy-item $src -destination $dst
and still get the SQL Agent error: The error information returned by PowerShell is: 'Invalid Path: '\\serverpath\filename'. '. Process Exit Code -1. The step failed.
The following from PowerShell on the server box succeeds: Copy-item "\\serverpath\filename" -destination "e:\path\filename"
Must be some permission issue, but I'm not smart enough to figure it out. Thx
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 4:28 PM
Points: 335,
Visits: 844
|
|
| Does the account the SQL Server Agent service runs under have permissions to that path?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 6,826,
Visits: 11,951
|
|
bwperrin (8/20/2012) Yes, I did the following from a SQL Agent PowerShell job step: $src = "\\serverpath\filename" $dst = "e:\path\filename" Copy-item $src -destination $dst
and still get the SQL Agent error: The error information returned by PowerShell is: 'Invalid Path: '\\serverpath\filename'. '. Process Exit Code -1. The step failed.
The following from PowerShell on the server box succeeds: Copy-item "\\serverpath\filename" -destination "e:\path\filename"
Must be some permission issue, but I'm not smart enough to figure it out. Thx When running it via SQL Agent it will be running as the SQL Agent service account, i.e. whatever account the service is running as. If your SQL Agent is running as a built-in account such as NetworkService, LocalSystem or LocalService you may have trouble since those accounts likely do not have rights to access the network share you're looking to access, whereas when you rin the same PowerShell locally under your own security context you will have access.
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 11:06 AM
Points: 14,
Visits: 47
|
|
Finally figured out how to get it working. SQL uses the sqlps utility, which is a Windows PowerShell mini-shell, and it has some restrictions such as mentioned above. The SQL Agent solution is to use a job step type of not PowerShell, but Operating system (CmdExec). In the code window do: powershell.exe -command "your powershell script"
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 6,826,
Visits: 11,951
|
|
bwperrin (8/28/2012)
Finally figured out how to get it working. SQL uses the sqlps utility, which is a Windows PowerShell mini-shell, and it has some restrictions such as mentioned above. The SQL Agent solution is to use a job step type of not PowerShell, but Operating system (CmdExec). In the code window do: powershell.exe -command "your powershell script" What are you referring too specifically?
You are right that the PowerShell step type runs code in a sqlps mini-shell, however Copy-Item is not restricted in the mini-shell. I just tested it again to be sure. I am happy you got it working via the command-line call but you did not need to do that. Something else was wrong in your previous attempts. Does a sysadmin own your job? Are you using a proxy?
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 9:29 AM
Points: 2,
Visits: 733
|
|
You need to change the provider when the job runs. It's running under the sqlserver:/ provider. Just do a cd c: at the top of your script and it will now accept the unc path.
Scott
|
|
|
|