I keep getting following error. The code looks good but I guess it's not grabing the siteUrl to create a full url.

PS C:\sales> .\Import.ps1 Exception calling "Add" with "3" argument(s): "The URL 'Sales_Records/salesreport.pdf' is invalid. It may refer to a non existent file or folder, or refer to a valid file or folder that is not in the current Web." At C:\sales\Import.ps1:32 char:37 + $temp = $templateFolder.files.Add <<<< ($targetUrl,$stream,$true) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

PS C:\Powershell>

= = = here is the code = = =

Add-PSSnapin Microsoft.SharePoint.PowerShell
$SiteUrl = "http://portal.GrandParkway.com/Sales"
$web = Get-SPWeb $SiteUrl
$Library = "Sales_Records" #Internal Name. I tried Display Name but same error
$docLibrary = $web.Lists[$Library]
$rootFolder = $docLibrary.RootFolder
$templateFolder = $web.GetFolder("Sales Records")

$path = "c:\\sourcefile"
$Filename = "salesreport.pdf"
$filewithPath = $path + "\" + $filename
$strPathInfo = Get-Location 

$stream = [IO.File]::OpenRead($Filewithpath)
$targetUrl = $SiteUrl+"/$Library/"+$Filename.Replace(" ","")
$temp = $templateFolder.files.Add($targetUrl,$stream,$true)
  • I was unable to reproduce your problem. Try adding some debug output to your script, e.g.: "Write-host $targetUrl" to check if all variables are correct. – Dribbel Jun 22 '12 at 13:26
  • I ran the same code in different farm and it worked. So I am not sure where to look to fix this issue. I looked at the ULS and it shows the same error that I got from PowerShell. Also, above code worked in different farm but managed-metadata columns did not get updated. – Bill Baer Jun 23 '12 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.