Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

Powershell Get-content remote machines with multiple drives

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
dmc2084
Asked:
dmc2084
  • 6
  • 6
1 Solution
 
Chris DentPowerShell DeveloperCommented:

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
 
dmc2084Author Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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

Chris
0
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.

 
dmc2084Author Commented:
Why can I not edit posted questions
0
 
dmc2084Author Commented:
Please delete question asap I will ask again with proper information
0
 
dmc2084Author Commented:
Is there a way to at least edit my question if its not going to be deleted untill the 9th?
0
 
Chris DentPowerShell DeveloperCommented:

> 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
 
Chris DentPowerShell DeveloperCommented:

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
 
Chris DentPowerShell DeveloperCommented:

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
 
dmc2084Author Commented:
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
 
Chris DentPowerShell DeveloperCommented:

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
 
dmc2084Author Commented:
Thanks Chris you are wonderful!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now