Get-content remote machine multiple drives

rated by 0 users
This post has 8 Replies | 3 Followers

Top 150 Contributor
Posts 7
dmc2084 Posted: 04-28-2009 10:52 PM

I am going to be getting data from a file 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

 $a = get-content list_of_computers
$b = get-content list_of_drive_paths.txt
string to connect to $a get-content $b something.dat

tryed many diffrent ways and I am not getting close

Any suggestions on how to do this via ps?

Top 25 Contributor
Posts 209
Microsoft MVP
Top Contributor

On PowerShell V1, you would need to map drive letters using net use similar to this:

net use x: \\servername\c$
get-content x:\whatever.txt
net use x: /DELETE

On V2, you can use remoting like this:

invoke-command { get-content [localpath to file] } -computername server12345

If this works for you, we can discuss optimizing this using sessions and querying multiple systems in one line.

 

Top 10 Contributor
Posts 248
Microsoft MVP
Top Contributor

Actually, Get-Content cmdlet supports UNC path (even in PowerShell V1), so you don't need to map a drive first. If you have rights to access admin shares, following will work:

Get-Content \\servername\c$\whatever.txt

 

Btw, Set-Location (alias cd) also supports UNC path:

cd \\servername\c$

dir

 

 

Top 150 Contributor
Posts 7

This is working out pretty good on single target.

Now I need to get it to get the content of that file on all the drives on 30 hosts that have 12 drives each some of those drives are mount points  in one go if that is possible

Top 150 Contributor
Posts 7

Thank you fine gentlemen so very much for the help.

 

Using V 1

So I have been trying something like this from a .ps1

$a = get-content "list of 30 hosts.txt"

$b = get-content "volume paths listed in the order of hosts on $a.txt". Paths look like c:\something.txt  d:\something.txt G:\extream\something.txt and so on

get -content \\$a\$b\something.txt

But no luck.

Top 25 Contributor
Posts 209
Microsoft MVP
Top Contributor

This is what you need:

  • a file called "hostlist.txt" located in your home folder $home with each server per line
  • for each server in hostlist.txt, a separate file with the local file paths for that server, called like the server plus extension ".txt", again located in the folder $home

Then, you can use the following script to read all files from all servers:

Note the function "ChangePath". It converts a local file path into a UNC path using admin shares, so it replaces for example "c:\autoexec.bat" with "c$\autoexec.bat"
Hope this helps
Tobias

 

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 $_)"
   }
}
 
Top 150 Contributor
Posts 7

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Tobias Weltner:

This is what you need:

  • a file called "hostlist.txt" located in your home folder $home with each server per line
  • for each server in hostlist.txt, a separate file with the local file paths for that server, called like the server plus extension ".txt", again located in the folder $home

Then, you can use the following script to read all files from all servers:

Note the function "ChangePath". It converts a local file path into a UNC path using admin shares, so it replaces for example "c:\autoexec.bat" with "c$\autoexec.bat"
Hope this helps
Tobias

 

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 $_)"
   }
}
 

 

 

This is working out great!  Two more things I need to do are have the output show the path to the file along with the contents of the file. Displaying the contents of the file is working out great now.

 Creating a text file for each host drive paths might become time consuming since my list of hosts is going to be growing. Maybe there is a way to use something like

get-wmiobject -computer computername win32_volume| select-object name  from a list of hosts  and create a text file where drive paths are outputted  into  and the text file is named after the host automatically for each server?

 

Top 150 Contributor
Posts 7

dmc2084:

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Tobias Weltner:

This is what you need:

  • a file called "hostlist.txt" located in your home folder $home with each server per line
  • for each server in hostlist.txt, a separate file with the local file paths for that server, called like the server plus extension ".txt", again located in the folder $home

Then, you can use the following script to read all files from all servers:

Note the function "ChangePath". It converts a local file path into a UNC path using admin shares, so it replaces for example "c:\autoexec.bat" with "c$\autoexec.bat"
Hope this helps
Tobias

 

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 $_)"
   }
}
 

 

 

This is working out great!  Two more things I need to do are have the output show the path to the file along with the contents of the file. Displaying the contents of the file is working out great now.

 Creating a text file for each host drive paths might become time consuming since my list of hosts is going to be growing. Maybe there is a way to use something like

get-wmiobject -computer computername win32_volume| select-object name  from a list of hosts  and create a text file where drive paths are outputted  into  and the text file is named after the host automatically for each server?

 

 

 

So it looks like I got the first part down that I was asking above


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
    }


Now all I need to do is get the final output to show host name, drive path, and the file content

 

Top 150 Contributor
Posts 7

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)

(b)

(c)

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)

host 1 drive g (b)

host 2 drive f (c)

Page 1 of 1 (9 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.