Solved

Powershell Get-content remote machines with multiple drives

Posted on 2009-05-04
12
851 Views
Last Modified: 2013-11-25
I have been trying to get data from a file that is on multiple remote machines. Each machine has about 8 to 12 drives some of those drives being mount points with a variety of names The file is on each of those drives and has the same name but the content of the file on each drive is unique which is why I need it..
so I would be pulling from paths like this
c:\something.dat
d:\something.dat
G:\mount1\something.dat
G:\mount2\something.dat
E:\mountextream1\something.dat
E:\mountextream2\something.dat
etc

I have been trying something like this

function ChangePath($path) {
   $qualifier = Split-Path $path -qualifier
   $drive = $qualifier.substring(0,1)
   $noqualifier = Split-Path $path -noQualifier
   "$drive`$noqualifier"
}

get-content $home\hostlist.txt | Foreach-Object {
   $server = $_
   get-content "$home\$server.txt" | Foreach-Object {
      get-content "\\$server\$(ChangePath $_)"
   }
}

get-content path to hostlist.txt | Foreach-Object {
   $server = $_
    new-item -path path to \$server.txt -type file -force
    get-wmiobject -computer $server win32_volume | select-object name |  out-file  path to\$server.txt
    }

This is working out great,
 all I need to do is get the final output to show host name, drive path, and the file content
I have everything working now.  So now I have the content of something.txt from all drives of all hosts going into a single text file (newfile.txt) and it looks something like this
(a) content
(b) content
(c) content
I need to get it so that the final output in the file will show the host and the drive path the content came from when its inputted into newfile.txt so it will look like
Host 1, drive c, (a)content
host 1, drive g, (b)content
host 2, drive f, (c)content


0
Comment
Question by:dmc2084
  • 6
  • 6
12 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24303230

Your snippets are confusing. You would have to explain what you're doing in a bit more detail for me to be able to see why all those files are called.

At present I would think that this snippet is what you're looking for. But I confess that I'm not at all sure if that really is the case.

I can't test whether or not Win32_Volume returns a mount point in the Name property, I don't have any systems using Mount Points other than the default.

Chris
ForEach ($Server in (Get-Content HostList.txt)) {
  Get-WMIObject Win32_Volume -Computer $Server | `
    Select-Object @{ n='Host';e={ $Server }}, Name, `
      @{n='Content';e={ Get-Content "$($_.Name)something.dat" }} | `
    Export-CSV "$Server.txt"
}

Open in new window

0
 

Author Comment

by:dmc2084
ID: 24305071
Sure, I will try to break it down a little better.

Step 1 Looks at a list of hosts in hostlist.txt.
       It then connects to each host individually looks at their volume paths then creates a text file named after the host and inputs their drive paths in there.  

So if there is 4 hosts in hostlist.txt  named host 1 through 4 the end product will be  four text files host1.txt host2.txt host3.txt host4.txt each containing the volume paths of the host
The very last line is calling a 2nd ps1 script and sending the output to a text file called final.txt.

get-content c:\hostlist.txt | Foreach-Object {
   $server = $_
    new-item -path c:\volumepaths\$server.txt -type file -force
    get-wmiobject -computer $server win32_volume | select-object name |  out-file  e:\# removed by ModernMatt #\test\volumekey\volumepaths\$server.txt
    }
C:\part2.ps1 | Out-file "c:\final.txt"

Part 2       
      This Also looks at hostlist.txt and connects to each host within one by one. As it connects to each host it looks for a text file named after the host. ( these are the text files that were created in part one that has volume paths for each server). It is in those text files that it gets its volume path to use.  It then gets the content of something.dat.    Now all the data from something.dat from every host is being put into a single text file called final.txt and the content looks like

1145
5545
6645
6654

Its just the content there is no way of telling what drive path or host it came from. The end goal is to have it look like this. Host, drivepath, content


Host1 c:\ 1145
Host2 d:\ 5545
Host3 f:\ 6645
Host4 j:\ 6654



function ChangePath($path) {
   $qualifier = Split-Path $path -qualifier
   $drive = $qualifier.substring(0,1)
   $noqualifier = Split-Path $path -noQualifier
   "$drive`$$noqualifier"
}


get-content c:\hostlist.txt | Foreach-Object {
   $server = $_
   get-content "c:\volumepaths\$server.txt" | Foreach-Object {
      get-content "\\$server\$(ChangePath $_\something.dat)"
     
   }
     }
get-content c:\hostlist.txt | Foreach-Object {
   $server = $_
    new-item -path c:\volumepaths\$server.txt -type file -force
    get-wmiobject -computer $server win32_volume | select-object name |  out-file  e:\# removed by ModernMatt #\test\volumekey\volumepaths\$server.txt
    }
C:\part2.ps1 | Out-file "c:\final.txt"
 
 
function ChangePath($path) {
   $qualifier = Split-Path $path -qualifier
   $drive = $qualifier.substring(0,1)
   $noqualifier = Split-Path $path -noQualifier
   "$drive`$$noqualifier"
}
 
 
get-content c:\hostlist.txt | Foreach-Object {
   $server = $_
   get-content "c:\volumepaths\$server.txt" | Foreach-Object {
      get-content "\\$server\$(ChangePath $_\something.dat)"
      
   }
     }

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24305142

You don't need this anymore? Wondering why you've posted a deletion request.

Chris
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:dmc2084
ID: 24305160
Why can I not edit posted questions
0
 

Author Comment

by:dmc2084
ID: 24305243
Please delete question asap I will ask again with proper information
0
 

Author Comment

by:dmc2084
ID: 24305281
Is there a way to at least edit my question if its not going to be deleted untill the 9th?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24305282

> Why can I not edit posted questions

Used to be able to, but it caused problems long ago and it hasn't been allowed since.

> Please delete question asap I will ask again with proper information

There's no real need. The goal of this to help you, just keep feeding information along with comments and it can be refined.

Besides, I have a new version for you to try ;)

Chris
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24305298

Unless there's a real need for all those intermediate files I would do it all in one loop.

The code below does this:

1. Opens HostList.txt and begins to loop through each server.
2. Connects to the system and gets the volume list
3. Gets the content of the something.dat file from each drive
4. Writes the host name, local drive name and file content to "Files.csv" in this format:

HostName,VolumeName,FileContent

Which seems to be to be the same as above, but without the intermediate files?

Chris
$FileInfo = @()
ForEach ($Server in (Get-Content HostList.txt)) {
  Get-WMIObject Win32_Volume -Computer $Server | `
    $FileInfo += Select-Object @{ n='Host';e={ $Server }}, Name, `
      @{n='Content';e={ `
        Get-Content "\\$Server\$($_.Name -Replace ':', '$')something.dat" 
      }}
}
$FileInfo | Export-CSV "FileInfo.csv"

Open in new window

0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24305340

Sorry, there's a bit of an error above. Fixed here.

Chris
$FileInfo = @()
ForEach ($Server in (Get-Content HostList.txt)) {
  $FileInfo += Get-WMIObject Win32_Volume -Computer $Server | `
    Select-Object @{ n='Host';e={ $Server }}, Name, `
      @{n='Content';e={ `
        Get-Content "\\$Server\$($_.Name -Replace ':', '$')something.dat" 
      }}
}
$FileInfo | Export-CSV "FileInfo.csv"

Open in new window

0
 

Author Comment

by:dmc2084
ID: 24305381
I feel some of the info I posted may be a little sensitive and if I can not edit it deletion is the only option

Thank you Chris I will give it a shot.
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24305411

I don't see how, all we have is some arbitrary file names and numbers :)

But anyway, Moderators can edit if it's cause for concern. Hit the Request Attention link in your top post and let them know what you want taking out. Hopefully they'll be able to deal with it without deleting the question entirely.

We're only prevented from editing because information was frequently lost after multiple edits.

Chris
0
 

Author Comment

by:dmc2084
ID: 24305487
Thanks Chris you are wonderful!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AWS Glacier is Amazons cheapest storage option and is their answer to a ‘Cold’ storage service.  Customers primarily use this service for archival purposes and storage of infrastructure backups.  Its unlimited storage potential and low storage cost …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question