Oct 29 2018 12:52 PM
Oct 29 2018 03:15 PM
Most of the errors that you're encountering are caused by one part of your script failing, and therefore you have a number of null values being encountered. To be honest you could probably simplify the script a lot
# SET UP
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
CLS
# PULL IN THE CSV
$filelist = Import-Csv -Path "<PATH TO THE CSV>"
foreach($file in $filelist)
{
Write-Host "Processing $($file.File)"
# Figure out the URL of the site
$arysiteurl = $file.File.Split('/')
$url = ""
for($x=0; $x -lt $arysiteurl.Length-2; $x++)
{
$url = $url + $arysiteurl[$x] + "/"
}
# Get the web
$web = Get-SPWeb $url
# Get the document based on the URL
$file = $web.GetFile($file.File)
$file.Delete()
}
Oct 30 2018 07:11 AM
thank you so much for your reply @Matt Weston. looks simplified now.
I ran the following script:
# SET UP
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
CLS
# PULL IN THE CSV
$filelist = Import-Csv -Path "C:\temp"
foreach($file in $filelist)
{
Write-Host "Processing $($file.File)"
# Figure out the URL of the site
$arysiteurl = $file.File.Split('/')
$url = ""
for($x=0; $x -lt $arysiteurl.Length-2; $x++)
{
$url = $url + $arysiteurl[$x] + "/"
}
# Get the web
$web = Get-SPWeb $url
# Get the document based on the URL
$file = $web.GetFile($file.File)
$file.Delete()
}
I get the following error:
Processing
You cannot call a method on a null-valued expression.
At line:16 char:2
+ $arysiteurl = $file.File.Split('/')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Get-SPWeb : Cannot find an SPSite object that contains the following Id or Url: Empty or Null.
At line:24 char:9
+ $web = Get-SPWeb $url
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPipeBindException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb
You cannot call a method on a null-valued expression.
At line:27 char:2
+ $file = $web.GetFile($file.File)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'Delete'.
At line:28 char:2
+ $file.Delete()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Delete:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Processing
You cannot call a method on a null-valued expression.
At line:16 char:2
+ $arysiteurl = $file.File.Split('/')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Get-SPWeb : Cannot find an SPSite object that contains the following Id or Url: Empty or Null.
At line:24 char:9
+ $web = Get-SPWeb $url
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPipeBindException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb
You cannot call a method on a null-valued expression.
At line:27 char:2
+ $file = $web.GetFile($file.File)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'Delete'.
At line:28 char:2
+ $file.Delete()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Delete:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Processing
You cannot call a method on a null-valued expression.
At line:16 char:2
+ $arysiteurl = $file.File.Split('/')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Get-SPWeb : Cannot find an SPSite object that contains the following Id or Url: Empty or Null.
At line:24 char:9
+ $web = Get-SPWeb $url
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPipeBindException
+ FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb
You cannot call a method on a null-valued expression.
At line:27 char:2
+ $file = $web.GetFile($file.File)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'Delete'.
At line:28 char:2
+ $file.Delete()
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Delete:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I am not sure what I am missing. Can you please help me out on this one?
My website URL: http:/contoso/subsitename
My Document library name: test
My csv file: sha.csv
Oct 30 2018 01:28 PM
SolutionHi @Test SharePoint the way those errors are cascading, I think it's purely down to the entries that are coming from the CSV, and that all of the other errors are as a result of single issue. Do you have a header in your CSV? In mine, I had a header of File, so my CSV looked like this:
File
https://contoso.sharepoint.com/sites/SiteA/Documents/File1.docx
https://contoso.sharepoint.com/sites/SiteA/Documents/File1 Copy.docx
Oct 31 2018 11:44 AM
Thank you so much @Matt Weston!! I was missing the header. You made my day!!
I was cramming my head on this for 3 days.
Oct 30 2018 01:28 PM
SolutionHi @Test SharePoint the way those errors are cascading, I think it's purely down to the entries that are coming from the CSV, and that all of the other errors are as a result of single issue. Do you have a header in your CSV? In mine, I had a header of File, so my CSV looked like this:
File
https://contoso.sharepoint.com/sites/SiteA/Documents/File1.docx
https://contoso.sharepoint.com/sites/SiteA/Documents/File1 Copy.docx