Sharepoint - Export all data to csv with powershell - powershell

I am attempting to use a script provided by Thriggle See His Answer Here and am having some issues with it. It works almost flawlessly for what I am doing - EXCEPT - it doesn't export the Created By, Created Date, Modified By, and Modified Date.
Is there a way to add those fields into the script?
Here is his script:
$url = "$url"
$listName = "$list"
$path ="c:\ColumnsOfList.csv"
$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$fields = $list.ContentTypes | %{ $_.FieldLinks } | select Name, DisplayName
$items = #() #array to store objects representing list items
$list.items | %{
$item = $_;
$hash = #{}; #hash table to store field name-value pairs
$fields | %{ $hash[$_.DisplayName] = $item[$_.Name] };
$items += new-object psobject -Property $hash }
$items | Export-Csv -Path $path

You could try this one.
Here is my test code.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web = Get-SPWeb -identity "http://sp:12001"
#Get the Target List
$list = $web.Lists["OrderDetails"]
#Array to Hold Result - PSObjects
$ListItemCollection = #()
#Get All List items where Status is "In Progress"
$list.Items | foreach {
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -name "Title" -value $_["Title"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "OrderDate" -value $_["OrderDate"]
$ExportItem | Add-Member -MemberType NoteProperty -name "CreatedBy" -value $_["Author"]
$ExportItem | Add-Member -MemberType NoteProperty -name "Created" -value $_["Created"]
#Add the object with property to an Array
$ListItemCollection += $ExportItem
}
#Export the result Array to CSV file
$ListItemCollection | Export-CSV "C:\Lee\ListData.csv" -NoTypeInformation
#Dispose the web Object
$web.Dispose()

Related

Splitting an array to different variables

I have an XML file with multiple same-name fields like this:
<XMLDAT>
<Interpret>Crow Jonathan</Interpret>
<Interpret>Mcnabney Douglas</Interpret>
<Interpret>Haimovitz Matt</Interpret>
<Interpret>Sitkovetski Dmitri</Interpret>
</XMLDAT>
I'm trying to split these into a separate variable for each Interpret so I can be able to export it as a CSV file. Ex.
[xml]$XML = Get-Content -Path C:\TestFile.xml
$Interpret = $XML.XMLDAT.Interpret
$interpret1 = ""
$interpret2 = ""
$interpret3 = ""
$interpret4 = ""
$DATACOLLECTION = #()
$DATA = New-Object PSObject
Add-Member -inputObject $DATA -memberType NoteProperty -name "Interpret1" -value $interpret1
Add-Member -inputObject $DATA -memberType NoteProperty -name "Interpret2" -value $interpret2
Add-Member -inputObject $DATA -memberType NoteProperty -name "Interpret3" -value $interpret3
Add-Member -inputObject $DATA -memberType NoteProperty -name "Interpret4" -value $interpret4
$DATACOLLECTION += $DATA
$DATACOLLECTION | Export-Csv -append -path C:\test.csv -NoTypeInformation
I'm not sure how to proceed into splitting these into their own variables.
PowerShell supports multi-target variable assignments:
[xml]$XML = Get-Content -Path C:\TestFile.xml
$interpret1, $interpret2, $interpret3, $interpret4 = $XML.XMLDAT.Interpret
But you don't really need all those variables :)
You could construct the final object by dynamically adding all the "Interpret" node values to a hashtable and then convert that to an object:
[xml]$XML = Get-Content -Path C:\TestFile.xml
$properties = [ordered]#{}
$XML.XMLDAT.Interpret |ForEach-Object -Begin {$number = 1} -Process {
$properties["Interpret$($number++)"] = "$_"
}
#( [pscustomobject]$properties ) |Export-Csv -Append -Path C:\test.csv -NoTypeInformation
Was able to get desired result using this ForEach loop:
ForEach ($Interprets in $Interpret){
$interpret1 = $Interpret[0]
$interpret2 = $Interpret[1]
$interpret3 = $Interpret[2]
$interpret4 = $Interpret[3]
}
I would just do one column with four rows:
$xml.xmldat.interpret | % { [pscustomobject]#{Interpret = $_} }
Interpret
---------
Crow Jonathan
Mcnabney Douglas
Haimovitz Matt
Sitkovetski Dmitri

Powershell Script to get Count from List with filter

Powershell script to get Count from SharePoint List
The following code works fine. It grabs data from a SharePoint list "Assignment Status" and display the results in csv file export. I want it to display the count. I was the output to show 2 columns: AssignedToPerson and the Count.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web = Get-SPWeb -identity "https://dev1.spr.com/sites/tech/"
#Get the Target List
$list = $web.Lists["Pending Assignments"]
#Array to Hold Result - PSObjects
$ListItemCollection = #()
$list.Items | Where-Object { $_["Assignment Status"] -eq "Pending"} | foreach {
$ExportItem = New-Object PSObject
#Read more: http://www.sharepointdiary.com/2013/04/export-sharepoint-list-items-to-csv-using-powershell.html#ixzz5uQEppUXH
$ExportItem | Add-Member -MemberType NoteProperty -Name "AssignedPerson" -value $_["AssignedPerson"]
$ExportItem | Add-Member -MemberType NoteProperty -name "TotalCountofPendingItems" -value $_["Count"]
#Add the object with property to an Array
$ListItemCollection += $ExportItem
}
#Export the result Array to CSV file
$ListItemCollection | Export-CSV "c:\dev1scripts\Exportfile.csv" -NoTypeInformation
#Dispose the web Object
$web.Dispose()
#Read more: http://www.sharepointdiary.com/2013/04/export-sharepoint-list-items-to-csv-using-powershell.html#ixzz5uQ8WDlpQ
Results should be like the following:
"Jonathan Adams", 3
"Roger Smith", 5
"Candice Matthews", 19
and so on...
Modify the PowerShell as below.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web = Get-SPWeb -identity "https://dev1.spr.com/sites/tech/"
#Get the Target List
$list = $web.Lists["Pending Assignments"]
#Array to Hold Result - PSObjects
$ListItemCollection = #()
$ItemCount=0;
$TotalCount=0;
$list.Items | Where-Object { $_["Assignment Status"] -eq "Pending"} | foreach {
$ItemCount++
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -Name "AssignedPerson" -value $_["AssignedPerson"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "TotalCountofPendingItems" -value $_["Count"]
$TotalCount+=$_["Count"]
#Add the object with property to an Array
$ListItemCollection += $ExportItem
}
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -Name "AssignedPerson" -value "Total:$ItemCount"
$ExportItem | Add-Member -MemberType NoteProperty -Name "TotalCountofPendingItems" -value "Total:$TotalCount"
$ListItemCollection += $ExportItem
#Export the result Array to CSV file
$ListItemCollection | Export-CSV "c:\dev1scripts\Exportfile.csv" -NoTypeInformation
#Dispose the web Object
$web.Dispose()
The results:

Powershell custom append object to csv file

I'm trying to output a custom object to a csv formatted text file as I loop through a for each. One object per line.
But nothing is written to the file.
Is it something with types to be converted ?
$rechten = Get-ADGroupMember -Identity $v -Recursive -ERRORACTION silentlycontinue | Get-ADUser -Property DisplayName -ERRORACTION silentlycontinue | Select-Object Name
Write-Host -ForegroundColor Yellow "ADgroup $v wordt uitgevlooid."
foreach ($rechtenhouder in $rechten) {
$objResults = New-Object PSObject
$objResults | Add-Member -MemberType NoteProperty -Name DirectoryPath -Value $objPath
$objResults | Add-Member -MemberType NoteProperty -Name Identity -Value $rechtenhouder.name
$objResults | Add-Member -MemberType NoteProperty -Name Systemrights -Value $accessRight.FileSystemRights
$objResults | Add-Member -MemberType NoteProperty -Name systemrightstype -Value $accessRight.accesscontroltype
$objResults | Add-Member -MemberType NoteProperty -Name isinherited -Value $accessRight.isinherited
$objResults | Add-Member -MemberType NoteProperty -Name inheritanceflags -Value $accessRight.inheritanceflags
$objResults | Add-Member -MemberType NoteProperty -Name rulesprotected -Value $objACL.areaccessrulesprotected
$objResults | Add-Member -MemberType NoteProperty -Name Adtype -Value "User"
$arrResults += $objResults
Add-Content $exportpathtxtappend $objresults
}
For your specific use exporting all objects at once or in batches would be the most efficient, but there are times were it would make sense to export a record one at a time to a CSV file which is what led me to this question, so I want to post my solution.
Use Export-CSV -Append to continually add to the end of a csv file.
foreach ($rechtenhouder in $rechten) {
$objResults = New-Object PSObject -Property #{
DirectoryPath = $objPath;
Identity = $rechtenhouder.name;
Systemrights = $accessRight.FileSystemRights;
systemrightstype = $accessRight.accesscontroltype;
isinherited = $accessRight.isinherited;
inheritanceflags = $accessRight.inheritanceflags;
rulesprotected = $objACL.areaccessrulesprotected;
Adtype = "User";
}
$objResults | Export-CSV $csvPath -Append -NoTypeInformation
}
This is useful if you are continually polling at set time intervals, but less so if you are iterating over a collection of objects, just export them all at once. For example, I would use this method of exporting for a script like below:
while($true){
$procs = Get-Process | Select-Object Name,CPU
$procs | Add-Member -type NoteProperty -Name "Timestamp" -Value $(Get-Date)
$procs | Export-CSV $csvPath -Append -NoTypeInformation
sleep -Seconds 60
}
First, I suggest you to create your object in a decent smarter way:
foreach ($rechtenhouder in $rechten) {
$objResults = New-Object PSObject -Property #{
DirectoryPath = $objPath;
Identity = $rechtenhouder.name;
Systemrights = $accessRight.FileSystemRights;
systemrightstype = $accessRight.accesscontroltype;
isinherited = $accessRight.isinherited;
inheritanceflags = $accessRight.inheritanceflags;
rulesprotected = $objACL.areaccessrulesprotected;
Adtype = "User";
}
$arrResults += $objResults
}
With this done, your $arrResults now contains your objects. This can easily exported to CSV files with PowerShells builtin Export-CSV:
$arrResults | Export-Csv -Path "C:/temp/text.csv"
Using Add-Content on every loop iteration is IMHO ineffective regarding performance. If your script runs for a long time and you want to save your current state in intervals, you could e.g. start an asynchronous job - let's say every 10th iteration - exporting your current array:
$i = 0
foreach ($rechtenhouder in $rechten) {
$objResults = New-Object PSObject -Property #{
DirectoryPath = $objPath;
Identity = $rechtenhouder.name;
Systemrights = $accessRight.FileSystemRights;
systemrightstype = $accessRight.accesscontroltype;
isinherited = $accessRight.isinherited;
inheritanceflags = $accessRight.inheritanceflags;
rulesprotected = $objACL.areaccessrulesprotected;
Adtype = "User";
}
$arrResults += $objResults
if ($i % 10 -eq 0) {
Start-Job -ScriptBlock {
param($T, $Path)
$T | Export-Csv -Path $Path
} -ArgumentList #($arrTest, "Path/to/script")
}
$i++
}

Selecting objects from a Custom Object

I have a custom PS Object that is something like the below:
ID Folder
MyServer01 \\Server\Share\Share\MyServer01
MyServer02 \\Server\Share\Share\MyServer02
Naturally the object itself is rather large, with over 1000 entries. I need to be able to select a specific row of the object based on querying the ID.
I thought something like this would work but I'm not having much luck:
$obj | Select-Object | Where-Object ($_.ID -eq "MyServer01")
I need it to return the entire row, so the above (assuming it worked) would return:
MyServer01 \\Server\Share\Share\MyServer01
EDIT:
foreach ($mf in $Folders.Tables[0]) {
$Info = New-Object System.Object
$Info | Add-Member -Type NoteProperty -Name ID -Value $mf.ID
$Info | Add-Member -Type NoteProperty -Name Folder -Value $mf.Folder
$obj += $Info
}
Use a hashtable for storing your objects:
$obj = #{}
foreach ($mf in $Folders.Tables[0]) {
$Info = New-Object -Type System.Object
$Info | Add-Member -Type NoteProperty -Name ID -Value $mf.ID
$Info | Add-Member -Type NoteProperty -Name Folder -Value $mf.Folder
$obj[$mf.ID] = $Info
}
Don't append to an array in a loop, as that tends to perform poorly.
If your code doesn't depend on the objects being created explicitly as System.Object I'd also recommend to create them as custom objects:
$obj = #{}
foreach ($mf in $Folders.Tables[0]) {
$Info = New-Object -Type PSCustomObject -Property #{
'ID' = $mf.ID
'Folder' = $mf.Folder
}
$obj[$mf.ID] = $Info
}

Sum Columns Using Powershell

I have written the following PowerShell script for getting disk space information for servers in our environment.
$servers = Get-Content E:\POC.txt
$array = #()
foreach($server in $servers){
$sysinfo = Get-WmiObject Win32_Volume -ComputerName $server
for($i = 0;$i -lt $sysinfo.Count; $i++){
$sname = $sysinfo[$i].SystemName
$servername = $server
$label = $sysinfo[$i].Label
if(($label) -and (!($label.Contains("FILLER")))){
write-host "Processing $label from $server"
$name = $sysinfo[$i].Name
$capacity = [math]::round(($sysinfo[$i].Capacity/1GB),2)
$fspace = [math]::round(($sysinfo[$i].FreeSpace/1GB),2)
$sused = [math]::round((($sysinfo[$i].Capacity - $sysinfo[$i].FreeSpace)/1GB),2)
$fspacepercent = [math]::Round((($sysinfo[$i].FreeSpace*100)/$sysinfo[$i].Capacity),2)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "SystemName" -Value $sname
$obj | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $server
$obj | Add-Member -MemberType NoteProperty -Name "Label" -Value $label
$obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $name
$obj | Add-Member -MemberType NoteProperty -Name "Capacity(GB)" -Value $capacity
$obj | Add-Member -MemberType NoteProperty -Name "FreeSpace(GB)" -Value $fspace
$obj | Add-Member -MemberType NoteProperty -Name "Used(GB)" -Value $sused
$obj | Add-Member -MemberType NoteProperty -Name "FreeSpace%" -Value $fspacepercent
$array += $obj
}
}
$array += write-output " "
$totalSize = ($array | Measure-Object 'Capacity(GB)' -Sum).Sum
$array += $totalsize
$array += write-output " "
}
$filename = "E:\VolumeReport.csv"
$array | Export-CSV $filename -NoTypeInformation
One additional requirement here is to get the sum of the columns for Capacity, Size and Freespace for each server. I tried using Measure-Object but no success.
No values are getting outputted here. Just blank. Please look into this and kindly assist.
Let try this on for size shall we.
$servers = Get-Content E:\POC.txt
$propertyOrdered = "SystemName","ServerName","Label","Name","Capacity(GB)","FreeSpace(GB)","Used(GB)","FreeSpace%"
$filename = "C:\temp\VolumeReport.csv"
('"{0}"' -f ($propertyOrdered -join '","')) | Set-Content $filename
foreach($server in $servers){
$sysinfo = Get-WmiObject Win32_Volume -ComputerName $server
$serverDetails = #()
for($i = 0;$i -lt $sysinfo.Count; $i++){
$sname = $sysinfo[$i].SystemName
$servername = $server
$label = $sysinfo[$i].Label
if(($label) -and (!($label.Contains("FILLER")))){
write-host "Processing $label from $server"
$name = $sysinfo[$i].Name
$capacity = [math]::round(($sysinfo[$i].Capacity/1GB),2)
$fspace = [math]::round(($sysinfo[$i].FreeSpace/1GB),2)
$sused = [math]::round((($sysinfo[$i].Capacity - $sysinfo[$i].FreeSpace)/1GB),2)
$fspacepercent = [math]::Round((($sysinfo[$i].FreeSpace*100)/$sysinfo[$i].Capacity),2)
$props = #{
"SystemName" = $sname
"ServerName" = $server
"Label" = $label
"Name" = $name
"Capacity(GB)" = $capacity
"FreeSpace(GB)" = $fspace
"Used(GB)" = $sused
"FreeSpace%" = $fspacepercent
}
# Build this server object.
$serverDetails += New-Object PSObject -Property $props
}
}
# Output current details to file.
$serverDetails | Select $propertyOrdered | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Add-Content $filename
#Calculate Totals and append to file.
$totals = '"","","","Totals",{0},{1},{2},""' -f ($serverDetails | Measure-Object -Property "Capacity(GB)" -Sum).Sum,
($serverDetails | Measure-Object -Property "FreeSpace(GB)" -Sum).Sum,
($serverDetails | Measure-Object -Property "Used(GB)" -Sum).Sum
$totals | Add-Content $filename
}
Part of the issue here is that you were mixing object output and static string output which most likely would have been holding you back. I tidied up the object generation in a way that should be 2.0 compliant. Not that what you were going was wrong in anyway but this is a little more pleasing to the eye then all the Add-Members
I removed $array since it did not have a place anymore since the logic here is constantly output data to the output file as supposed to storing it temporarily.
For every $server we build an array of disk information in the variable $serverDetails. Once all the disks have been calculated (using your formulas still) we then create a totals line. You were not really clear on how you wanted your output so I guessed. The above code should net output like the following. (It looks a lot nicer in Excel or in a csv aware reader. )
"SystemName","ServerName","Label","Name","Capacity(GB)","FreeSpace(GB)","Used(GB)","FreeSpace%"
"server01","server01","System Reserved","\\?\Volume{24dbe945-3ea6-11e0-afbd-806e6f6e6963}\","0.1","0.07","0.03","71.85"
"","","","Totals",0.1,0.07,0.03,""
"server02","server02","System Reserved","\\?\Volume{24dbe945-3ea6-11e0-afbd-806e6f6e6963}\","0.1","0.07","0.03","69.27"
"server02","server02","images","I:\","1953.12","152.1","1801.02","7.79"
"server02","server02","Data","E:\","79.76","34.59","45.18","43.36"
"","","","Totals",2032.98,186.76,1846.23,""