SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Powershell for network path


Powershell for network path

Author
Message
Sachin Nandanwar
Sachin Nandanwar
Say Hey Kid
Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)Say Hey Kid (677 reputation)

Group: General Forum Members
Points: 677 Visits: 2633
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

--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14847 Visits: 14396
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
bwperrin
bwperrin
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 56
Same problem for me - did you ever get this figured out?
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14847 Visits: 14396
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
bwperrin
bwperrin
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 56
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
Scott D. Jacobson
Scott D. Jacobson
SSChasing Mays
SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)

Group: General Forum Members
Points: 620 Visits: 1017
Does the account the SQL Server Agent service runs under have permissions to that path?
Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14847 Visits: 14396
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
bwperrin
bwperrin
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 56
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"

Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14847 Visits: 14396
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
sgnewman
sgnewman
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 1153
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search