Welcome › Forums › General PowerShell Q&A › Help/Advice on adding data to Report (remote Get-ChildItem)
- This topic has 4 replies, 4 voices, and was last updated 1 month, 1 week ago by
Participant.
-
AuthorPosts
-
-
December 8, 2020 at 7:42 am #277521
Hi Folks,
Hoping for some advice – another set of eyes to tackle an issue. I have a text/CSV file containing detailsPowerShell123Server,MasterID,WebsiteName,RootfolderABCD,12234,mysite.com,e:\websites\mysiteEFGH,12331,another.com,e:\websites\anotherThis file is pulled down to a Automation Server /Powershell server so it can be used to hold details on which remote servers to connect to and get some info about files/folders.
PowerShell12345678$Alldata = Import-CSV -Path $Fileforeach ($item in $allData) {$session = New-PSSession -ComputerName $item.Server -Credential $creds$output += Invoke-Command -Session $session `{Get-ChildItem -File -Path $ite.rootfolder -Recurse| Select-Object FullName,@{N='LastUpdated';Expression={Get-Date -Date $_.LastWriteTime -Uformat "%d-%m-%Y %R"}},@{N='MD5';Expression={(Get-FileHash $_.FullName).Hash}} }So this gives me something like:
PowerShell123456FullName LastUpdated MD5 PSComputerName RunspaceId-------- ----------- --- -------------- ----------E:\websites\mysite\www\default.aspx 15-07-2020 14:54 EF165CB4 ABCD dced3d40E:\websites\mysite\www\other.aspx 15-07-2020 14:55 EF165CB1 ABCD dced3d40E:\websites\another\www\default.aspx 24-07-2020 07:16 4F361B43 EFGH 0d731a90E:\websites\another\www\and.aspx 24-07-2020 07:16 4F361B42 EFGH 0d731a90Which is great, BUT – I am wondering how can I get values from the original CSV file added to that?
What I mean is – I need to also have the values from the CSV (ie. Server, MasterId, Websitename added to each beginning line of the output and get rid of the PsComputerName and RunSpaceId)So the end result I need would be:
PowerShell123456Server MasterID FullName LastUpdated MD5------ -------- -------- ----------- ---ABCD 12334 E:\websites\mysite\www\default.aspx 15-07-2020 14:54 EF165CB4ABCD 12334 E:\websites\mysite\www\other.aspx 15-07-2020 14:54 EF165CB1EFGH 12331 E:\websites\another\www\default.aspx 24-07-2020 07:16 4F361B43EFGH 12331 E:\websites\another\www\and.aspx 24-07-2020 07:16 4F361B42The aim is to create a text file on the automation server that has that output.
Any ideas/thoughts on best way to approach this?
-
December 8, 2020 at 11:50 am #277641
Give this a try
PowerShell1234567891011121314151617$Alldata = Import-CSV -Path $File$output = foreach ($item in $allData){$session = New-PSSession -ComputerName $item.Server -Credential $credsInvoke-Command -Session $session -ScriptBlock {Get-ChildItem -File -Path $using:item.rootfolder -Recurse |Select-Object FullName,@{N='LastUpdated';E={Get-Date -Date $_.LastWriteTime -Uformat "%d-%m-%Y %R"}},@{N='MD5';E={(Get-FileHash $_.FullName).Hash}}} -HideComputerName |Select-Object @{N='Server';E={$item.Server}},@{N='MasterID';E={$item.masterid}},FullName,LastUpdated,MD5} -
December 8, 2020 at 12:11 pm #277647
Another method to try:
PowerShell123456789101112131415161718$results = foreach ($item in $allData) {$session = New-PSSession -ComputerName $item.Server -Credential $creds$masterId = $item.MasterId$sb = {Get-ChildItem -File -Path $ite.rootfolder -Recurse |Select-Object @{Name='ComputerName';Expression={$env:COMPUTERNAME}},@{Name='MasterId';Expression={$using:masterId}},FullName,@{Name='LastUpdated';Expression={Get-Date -Date $_.LastWriteTime -Uformat "%d-%m-%Y %R"}},@{Name='MD5';Expression={(Get-FileHash $_.FullName).Hash}}}Invoke-Command -Session $session $sb}$results -
December 8, 2020 at 12:26 pm #277656
Only thing I would add is the default algorithm for Get-FileHash is SHA-256. If you need MD5, add -Algorithm MD5 to the Get-FileHash command.
-
December 8, 2020 at 12:31 pm #277659
Wow! Once again, this forum comes to the rescue. I am always humbled by the willingness of folks to help out ! This internet-thing ain’t all bad ! 😉
Thank you all for your replies – all is working wonderfully.
-
-
AuthorPosts
- You must be logged in to reply to this topic.