Powershell list recursively files attributes to csv - powershell

I am trying to fill a csv file with all the attributes of the files contained within a folder. I have troubles recovering the fileversion & assembly
$arr = #()
gci C:\Temp -recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-Item $_.Length/1MB)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.FileVersion)
$obj | Add-Member NoteProperty Assembly ($_.AssemblyVersion)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\temp\File\report.csv"

try this
$arr = #()
gci C:\Temp -recurse -File -Filter *.dll | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-Item $_.Length/1MB)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)
$obj | Add-Member NoteProperty Assembly ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\temp\File\report.csv"
you can simplify your code like this
gci C:\Temp -recurse -File -Filter *.dll | % {
New-Object PSObject -Property #{
Directory= $_.DirectoryName
Name= $_.Name
Size= $_.Length/1MB
Owner= ((Get-ACL $_.FullName).Owner)
LastAccess= $_.LastAccessTime
Extension= $_.Extension
Creation= $_.CreationTime
LastWrite= $_.LastWriteTime
ReadOnly= $_.IsReadOnly
FullName= $_.FullName
Date= (Get-Date -format "yyyy-MM-d HH:mm")
Version= ($_.VersionInfo.FileVersion)
Assembly= ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
}
} | Export-CSV -notypeinformation "c:\temp\File\report.csv"

1)The issue what you are facing is because of permission. Try running the script in elevated mode(run as administrator).
2)You should not try to save anything under C:\temp folder. Better to create a folder in D:\ or E:\ and put it over there as D:\temp_dump\report.csv
3)if you are putting get-item on the size, then that is not a valid one since you have to pick whatever inside the folder.
Below Script is working fine.
##########################################################
$arr = #()
$Folder_path="E:\PS"
gci $Folder_path | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-ChildItem $Folder_path | Measure-Object -property length -sum)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.FileVersion)
$obj | Add-Member NoteProperty Assembly ($_.AssemblyVersion)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "E:\report.csv"
#

In order to get fileversion you have to use this:
$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)
You cannot get AssemblyVersion from VersionInfo

Related

Summing an array of Objects in Powershell

I have an array of objects that are holding integer values.
$row = new-Object PSObject # create a new object to hold its data
$row | Add-Member -Name "sheet_number" -MemberType NoteProperty -Value 1
$row | Add-Member -Name "frame_number" -MemberType NoteProperty -Value 2
$row | Add-Member -Name "sheet_height" -MemberType NoteProperty -Value 1200
$row | Add-Member -Name "frame_height" -MemberType NoteProperty -Value 1200
$row | Add-Member -Name "frame_width" -MemberType NoteProperty -Value 3300
$row | Add-Member -Name "orientation" -MemberType NoteProperty -Value 0
$frames += $row
this is in a for loop intended to iterate through several times. But the sheet_number property should only have a couple values. what I need is to sum up the values in frame_width where the sheet_number is the same.
Pseudo Code:
sheet_width = sum of frame_width where sheet number = 1
Use a combination of Where-Object, ForEach-Object, and Measure-Object:
$sum = (
$frames |
Where-Object sheet_number -eq 1 |
ForEach-Object frame_width |
Measure-Object -Sum
).Sum
To do it for all sheet numbers, additionally use Group-Object:
$arrayOfSums =
$frames |
Group-Object sheet_number |
ForEach-Object {
($_.Group.frame_width | Measure-Object -Sum).Sum
}

Powershell script calendar size issue

Trying to run the following script to get calendar size in Exchange 2016:
# Get-MailboxFolderSize.ps1 edit as required for folder you need stats on.
#$mailboxes = #(Get-Mailbox -ResultSize Unlimited)
$mailboxes = #(Get-MailboxServer | where-object {$_.AdminDisplayVersion.Major -eq 15} | Get-Mailbox -ResultSize Unlimited)
#$mailboxes = #(Get-MailboxServer | where-object {$_.AdminDisplayVersion.Major -eq 15} | Get-Mailbox)
#$mailboxes = #(Get-Mailbox hatfiemh)
$report = #()
foreach ($mailbox in $mailboxes)
{
$inboxstats = Get-MailboxFolderStatistics $mailbox -FolderScope Calendar | Where {$_.FolderPath -eq "/Calendar"}
$mbObj = New-Object PSObject
$mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
$mbObj | Add-Member -MemberType NoteProperty -Name "Calendar Size (gb)" -Value $inboxstats.FolderandSubFolderSize.ToGB()
$mbObj | Add-Member -MemberType NoteProperty -Name "Calendar Items" -Value $inboxstats.ItemsinFolderandSubfolders
$report += $mbObj
}
$report
I get the following error message:
You cannot call a method on a Null-valued expression.
at c:\Get-MailboxFolderSize.psi:13 char:5
$mbObj | Add-Member -MemberType NoteProperty -Name "inbox Size

Adding data from a second Invoke-WebRequest import into an existing array

I am fairly new to powershell and am having a problem with my script adding data from a second Invoke-WebRequest into an existing array $arr1.
The second import into variable $annotations works fine. I then need to match where $vm.Name = $annotations.Name in order for final ForEach($vm in $vms) to work and pull in the annotations data as well but am stuck.
My code is as follows. Please help
$StorageSystemName = "storageName"
$StoragePoolName = "storagepool"
$ReportName = "~\Reports\ServerList_$((Get-Date).ToString('yyyy-MM-dd')).xlsx"
Invoke-WebRequest -Uri http://srv1/location/Report_volume_storage.csv -OutFile .\Report_volume_storage.csv
$luns = Import-Csv .\Report_volume_storage.csv -Delimiter ";" |
Where-Object {$_.'Storage System name' -eq $StorageSystemName -and $_.'Storage Pool Name' -eq $StoragePoolName -and $_.'Volume Name'} |
Sort-Object "Storage Pool Name", "Volume Name"
Invoke-WebRequest -Uri http://srv2/addmdata/addmdata.csv -OutFile .\addmdata.csv
$annotations = Import-Csv .\addmdata.csv -Delimiter "," |
Select #{n='Name';e={$_.name.Split('.')[0]}}, * -Exclude Name,
#{n="Annotationserverdescription";e={$_.'Server Description'}},
#{n="Annotationapowner";e={$_.'Annotationapowner (Annotationappowner)'}},
#{n="Annotationclient";e={$_.'Client'}}
Sort-Object Name
$arr1 = #()
ForEach($lun in $luns)
{
$dsnaa = $lun.'LUN UID'
$dsnaa = "*$dsnaa*"
$datastore = Get-Datastore |
Where {($_.ExtensionData.Info.Vmfs.Extent).DiskName -like $dsnaa}
$VMs = #()
$datastore | ForEach-Object {
$dstore = $_.name
$VMs += get-VM -datastore $dstore | Where {$_.PowerState -eq "PoweredOn"} | Select #{n="Name";e={$_.name}}, #{n="PowerState";e={$_.PowerState}}, #{n="Datastore_Name";e={$dstore}}
}
ForEach($vm in $vms)
{
$data = New-Object System.Object
$data | Add-Member -MemberType NoteProperty -Name "Name" -Value $vm.Name
$data | Add-Member -MemberType NoteProperty -Name "PowerState" -Value $vm.PowerState
$data | Add-Member -MemberType NoteProperty -Name "Annotationserverdescription" -Value $annotation.'Server Description'
$data | Add-Member -MemberType NoteProperty -Name "Annotationapowner" -Value $annotation.'Annotationapowner (Annotationappowner)'
$data | Add-Member -MemberType NoteProperty -Name "Annotationclient" -Value $annotation.Client
$data | Add-Member -MemberType NoteProperty -Name "Volume Name" -Value $lun.'Volume Name'
$data | Add-Member -MemberType NoteProperty -Name "LUN UID" -Value $lun.'LUN UID'
$data | Add-Member -MemberType NoteProperty -Name "Capacity (GiB)" -Value $lun.'Capacity (GiB)'
$data | Add-Member -MemberType NoteProperty -Name "Storage Pool Name" -Value $lun.'Storage Pool Name'
$data | Add-Member -MemberType NoteProperty -Name "Storage System name" -Value $lun.'Storage System name'
$data | Add-Member -MemberType NoteProperty -Name "Storage Tier" -Value $lun.'Storage Tier'
$arr1 += $data
}
}
$arr1 | Export-Excel $ReportName
You could do something like the following:
$StorageSystemName = "storageName"
$StoragePoolName = "storagepool"
$ReportName = "~\Reports\ServerList_$((Get-Date).ToString('yyyy-MM-dd')).xlsx"
Invoke-WebRequest -Uri http://srv1/location/Report_volume_storage.csv -OutFile .\Report_volume_storage.csv
$luns = Import-Csv .\Report_volume_storage.csv -Delimiter ";" |
Where-Object {$_.'Storage System name' -eq $StorageSystemName -and $_.'Storage Pool Name' -eq $StoragePoolName -and $_.'Volume Name'} |
Sort-Object "Storage Pool Name", "Volume Name"
Invoke-WebRequest -Uri http://srv2/addmdata/addmdata.csv -OutFile .\addmdata.csv
$annotations = Import-Csv .\addmdata.csv -Delimiter "," |
Select #{n='Name';e={$_.name.Split('.')[0]}}, * -Exclude Name,
#{n="Annotationserverdescription";e={$_.'Server Description'}},
#{n="Annotationapowner";e={$_.'Annotationapowner (Annotationappowner)'}},
#{n="Annotationclient";e={$_.'Client'}}
Sort-Object Name
$arr1 = #()
ForEach($lun in $luns)
{
$dsnaa = $lun.'LUN UID'
$dsnaa = "*$dsnaa*"
$datastore = Get-Datastore |
Where {($_.ExtensionData.Info.Vmfs.Extent).DiskName -like $dsnaa}
$VMs = #()
$datastore | ForEach-Object {
$dstore = $_.name
$VMs += get-VM -datastore $dstore | Where {$_.PowerState -eq "PoweredOn"} | Select #{n="Name";e={$_.name}}, #{n="PowerState";e={$_.PowerState}}, #{n="Datastore_Name";e={$dstore}}
}
ForEach($vm in $vms)
{
$ActiveAnnotation = $null
$ActiveAnnotation = $annotations | where-object {$_.name -eq $vm.name}
$data = New-Object System.Object
$data | Add-Member -MemberType NoteProperty -Name "Name" -Value $vm.Name
$data | Add-Member -MemberType NoteProperty -Name "PowerState" -Value $vm.PowerState
$data | Add-Member -MemberType NoteProperty -Name "Annotationserverdescription" -Value $ActiveAnnotation.'Server Description'
$data | Add-Member -MemberType NoteProperty -Name "Annotationapowner" -Value $ActiveAnnotation.'Annotationapowner (Annotationappowner)'
$data | Add-Member -MemberType NoteProperty -Name "Annotationclient" -Value $ActiveAnnotation.Client
$data | Add-Member -MemberType NoteProperty -Name "Volume Name" -Value $lun.'Volume Name'
$data | Add-Member -MemberType NoteProperty -Name "LUN UID" -Value $lun.'LUN UID'
$data | Add-Member -MemberType NoteProperty -Name "Capacity (GiB)" -Value $lun.'Capacity (GiB)'
$data | Add-Member -MemberType NoteProperty -Name "Storage Pool Name" -Value $lun.'Storage Pool Name'
$data | Add-Member -MemberType NoteProperty -Name "Storage System name" -Value $lun.'Storage System name'
$data | Add-Member -MemberType NoteProperty -Name "Storage Tier" -Value $lun.'Storage Tier'
$arr1 += $data
}
}
$arr1 | Export-Excel $ReportName
I added the $ActiveAnnotation variable inside of your VMs loop to find the annotation match each time through the loop.

Powershell: File properties - Bytes to Mb

Currently have the following script that outputs some file data to a report. The file length is in bytes though and wondering how I can convert that to MB before outputting to the array.
$arr = #()
gci C:\stuff -recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Length $_.Length
$obj | Add-Member NoteProperty created $_.creationtime
$obj | Add-Member NoteProperty Access $_.LastAccessTime
$obj | Add-Member NoteProperty LastWritten $_.LastWriteTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\files.csv"
When converting into MB, just put brackets around it:
$obj | Add-Member NoteProperty Length ($_.Length/1MB)
or maybe more useful:
$obj | Add-Member NoteProperty MB ("{0:N3}" -f ($_.Length/1MB))
to only show the first three digits after the point.

Odd PowerShell behavior and Add-Member

Can someone help me understand why the $_ works differently for the Add-Member function than it seems to for other PowerShell functions? There must be some nuance that I am missing here.
Using the sample CSV file below, the first 3 examples work fine. The fourth, which seems pretty straightforward, does not. The error Powershell returns is:
The variable '$_' cannot be retrieved because it has not been set.
Is this "normal" or have I miscoded something?
Thanks in advance.
Test.csv
Column1,Column2, Column3
Rock,1,abc
Paper,2,efg
Scissors,3,hij
$obj = Import-CSV "C:\test.csv"
(1) $obj | Add-Member -MemberType NoteProperty -Name IdNumber -value "ROCK" -PassThru| Format-Table
(2) $obj | ForEach-Object { $_ | Add-Member -MemberType NoteProperty -Name IdNumber -value $_.Column1 ; $_} | Format-Table
(3) $obj | ForEach-Object { Add-Member -InputObject $_ -MemberType NoteProperty -Name IdNumber -value $_.Column1 ; $_ } | Format-Table
(4) $obj | Add-Member -MemberType NoteProperty -Name IdNumber -value $_.Column1 -PassThru| Format-Table
When you do this:
$obj | Add-Member -MemberType NoteProperty -Name IdNumber -value $_.Column1 -PassThru
You are accessing $_ outside of a pipeline bound context e.g. inside a foreach-object expression or within a scriptblock value specified for a pipeline bound parameter. The Value parameter is not pipeline bound. If it were you would be able to do this:
$obj | Add-Member -MemberType NoteProperty -Name IdNumber -value {$_.Column1} -PassThru
As it stands, the easiest way to do what you want is:
$obj | ForEach-Object { Add-Member -Inp $_ NoteProperty -Name IdNumber -Value $_.Column1 -PassThru } | Format-Table