powershell grep to query txt files on remote hosts

Have a need to query txt files on remote hosts using powershell.

can someone point me in the right direction?

looks like wmi can query remote files....and maybe select-string?
LVL 2
bnditAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mark_gaoCommented:
#remote host must enable powershell remote management
#you can run Enable-PSRemoting -force to enable powershell remote

# for single host
$cren = Get-Credential
invoke-command -computer server  -Credential $cren -ScriptBlock {Select-String c:\2.text}

$ for many single host
$comp = get-content c:\computers.txt
$s = new-pssession -computername $comp
invoke-command -session $s -ScriptBlock  {Select-String c:\2.text}
0
bnditAuthor Commented:
thx...so your suggestion works if the remote host has powershell installed. How about if powershell isnt installed? Is there a way then? Similar to the WMI technology...you can do Get-ChildItem on the local computer, but Get-ChildItem wouldn't work on the remote host. However, you could accomplish the same result of Get-ChildItem using WMI queries on the remote host.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Why do you think PS cannot access remote files? You only need to allow for admin shares (c$ and the like), and appropriate credentials to use them. Get-Content does not care whether the "file" is remote, local or a registry hive.
0
What were the top attacks of Q1 2018?

The Threat Lab team analyzes data from WatchGuard’s Firebox Feed, internal and partner threat intelligence, and a research honeynet, to provide insightful analysis about the top threats on the Internet. Check out our Q1 2018 report for smart, practical security advice today!

bnditAuthor Commented:
I was under the assumption it wouldnt work. Do you have a sample script I could try?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you do not need to provide credentials, then it is as easy as
  get-content \\remotePC\c$\temp\example.txt
0
bnditAuthor Commented:
Ok lets assume credentials are needed
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
$cred = Get-Credential
New-PSDrive -Name RemoteDrive -PSProvider FileSystem -Root \\remotePC\c$\temp -Credentials $cred
get-content RemoteDrive:\example.txt
Remove-PSDrive RemoteDrive

Open in new window

0
bnditAuthor Commented:
Tried it...

Get-Content: Cannot find drive. A drive with the name 'RemoteDrive' does not exist.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
New-PSDrive did not report an error? I tried all of above, but without credentials, and it worked fine for me.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bnditAuthor Commented:
Screenshots of what I'm getting

There was a type on the "-Credential" parameter....fixed now, but still not working. I must pass the credentials via the script vs. Get-Credential. This works for my other scripts.


relative path: X:\utils\logs
absolute path: \\MCCLANE\c$\utils\logs
1.png
2.png
3.png
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Looks like the provider FileSystem does not like any -credentials parameter. Of course this is the only feature I did not check. Sigh.

We will have to use COM methods then:
$password = Get-Content "C:\scripts\password.txt" | ConvertTo-SecureString
$drive = new-object -com wscript.network
$drive.MapNetworkDrive("X:", "\\MCCLANE\c$\utils\logs", $false, "domain\administrator", $password)
get-content X:\example.txt
$drive.RemoveNetworkDrive("X:")

Open in new window

For an explanation of the MapNetworkDrive method see e.g. http://ss64.com/vb/drivemap.html .
I don't know whether you need the ConvertTo-SecureString here. That depends on your text file.

0
bnditAuthor Commented:
I only changed X: for Y:...and here's what I get.



Exception calling "MapNetworkDrive" with "5" argument(s): "Type mismatch.
"
At C:\Users\ramon.alvarez\AppData\Local\Temp\5193195e-c50d-4cb1-82b2-31f15aa11697.ps1:3 char:23
+ $drive.MapNetworkDrive <<<< ("Y:", "\\MCCLANE\c$\utils\logs", $false, "domain\administrator", $password)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation
 
Get-Content : Cannot find path 'Y:\bytecount.txt' because it does not exist.
At C:\Users\ramon.alvarez\AppData\Local\Temp\5193195e-c50d-4cb1-82b2-31f15aa11697.ps1:4 char:12
+ get-content <<<<  Y:\bytecount.txt
    + CategoryInfo          : ObjectNotFound: (Y:\bytecount.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Try to use the plain password instead of the SecureString one.
0
bnditAuthor Commented:
This ended up being the best way for me to map a network drive.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.