Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Get-content remote machines with multiple drives

Posted on 2009-05-04
12
Medium Priority
?
863 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 71

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 71

Expert Comment

by:Chris Dent
ID: 24305142

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

Chris
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 71

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 71

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 71

Accepted Solution

by:
Chris Dent earned 2000 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 71

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

715 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