Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Powershell for network path Expand / Collapse
Author
Message
Posted Monday, June 4, 2012 11:58 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 3:09 AM
Points: 314, Visits: 2,530
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
Post #1310967
Posted Tuesday, June 5, 2012 1:23 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:58 PM
Points: 7,135, Visits: 12,748
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
Post #1311445
Posted Friday, August 17, 2012 12:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 7:37 AM
Points: 14, Visits: 55
Same problem for me - did you ever get this figured out?
Post #1346709
Posted Friday, August 17, 2012 12:56 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:58 PM
Points: 7,135, Visits: 12,748
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
Post #1346718
Posted Monday, August 20, 2012 10:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 7:37 AM
Points: 14, Visits: 55
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
Post #1347321
Posted Monday, August 20, 2012 10:57 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 8:13 PM
Points: 356, Visits: 921
Does the account the SQL Server Agent service runs under have permissions to that path?
Post #1347339
Posted Monday, August 20, 2012 11:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:58 PM
Points: 7,135, Visits: 12,748
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
Post #1347344
Posted Tuesday, August 28, 2012 10:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, November 25, 2013 7:37 AM
Points: 14, Visits: 55
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"
Post #1351115
Posted Tuesday, August 28, 2012 11:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:58 PM
Points: 7,135, Visits: 12,748
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
Post #1351125
Posted Wednesday, September 5, 2012 1:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 7:15 AM
Points: 4, Visits: 932
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
Post #1354840
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse