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?
can someone point me in the right direction?
looks like wmi can query remote files....and maybe select-string?
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.
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.
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
get-content \\remotePC\c$\temp\example
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
ASKER
Tried it...
Get-Content: Cannot find drive. A drive with the name 'RemoteDrive' does not exist.
Get-Content: Cannot find drive. A drive with the name 'RemoteDrive' does not exist.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:
I don't know whether you need the ConvertTo-SecureString here. That depends on your text file.
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:")
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.
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\App Data\Local \Temp\5193 195e-c50d- 4cb1-82b2- 31f15aa116 97.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\App Data\Local \Temp\5193 195e-c50d- 4cb1-82b2- 31f15aa116 97.ps1:4 char:12
+ get-content <<<< Y:\bytecount.txt
+ CategoryInfo : ObjectNotFound: (Y:\bytecount.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.Pow erShell.Co mmands.Get ContentCom mand
Exception calling "MapNetworkDrive" with "5" argument(s): "Type mismatch.
"
At C:\Users\ramon.alvarez\App
+ $drive.MapNetworkDrive <<<< ("Y:", "\\MCCLANE\c$\utils\logs",
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
Get-Content : Cannot find path 'Y:\bytecount.txt' because it does not exist.
At C:\Users\ramon.alvarez\App
+ get-content <<<< Y:\bytecount.txt
+ CategoryInfo : ObjectNotFound: (Y:\bytecount.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.Pow
Try to use the plain password instead of the SecureString one.
ASKER
This ended up being the best way for me to map a network drive.
#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}