Solved

Powershell Get-content remote machines with multiple drives

Posted on 2009-05-04
12
849 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
Comment Utility

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
Comment Utility
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
Comment Utility

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

Chris
0
 

Author Comment

by:dmc2084
Comment Utility
Why can I not edit posted questions
0
 

Author Comment

by:dmc2084
Comment Utility
Please delete question asap I will ask again with proper information
0
 

Author Comment

by:dmc2084
Comment Utility
Is there a way to at least edit my question if its not going to be deleted untill the 9th?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

> 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
Comment Utility

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
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
Thanks Chris you are wonderful!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now