Link to home
Start Free TrialLog in
Avatar of bndit
bndit

asked on

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?
Avatar of mark_gao
mark_gao

#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}
Avatar of bndit

ASKER

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.
Avatar of Qlemo
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.
Avatar of bndit

ASKER

I was under the assumption it wouldnt work. Do you have a sample script I could try?
If you do not need to provide credentials, then it is as easy as
  get-content \\remotePC\c$\temp\example.txt
Avatar of bndit

ASKER

Ok lets assume credentials are needed
$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

Avatar of bndit

ASKER

Tried it...

Get-Content: Cannot find drive. A drive with the name 'RemoteDrive' does not exist.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bndit

ASKER

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

Avatar of bndit

ASKER

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
Try to use the plain password instead of the SecureString one.
Avatar of bndit

ASKER

This ended up being the best way for me to map a network drive.