Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Get-content (Joint path) problem

$
0
0

Hello,

i am trying to write simple script which for some reason returns an error to me,
here is the code that i have:

param (
    [string]$LogFile = (read-host "which file would you like to walk through"),
    [string]$String = (read-host "what text string do you like to show")
    )
   

$folder = gci "E:\testcopy\" | where-object { $_.PSIsContainer} | sort CreationTime -desc | select -f 1
Get-Content (join-path $folder $Logfile) | select-string  -pattern $string | out-file "E:\testcopy\string.txt"

 

The problem is that when i run it i got the following error:

Get-Content : Cannot find path 'C:\Users\Administrator\2014.02.10 08.54.23\testfile.txt' because it does not exist.
At E:\testcopy\tool.ps1:9 char:12
+ Get-Content <<<<  (join-path $folder $Logfile) | select-string  -pattern $string | out-file "E:\testcopy\string.txt"
    + CategoryInfo          : ObjectNotFound: (C:\Users\Admini...23\testfile.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

 

If i set the $folder with absolute path like this:
$folder = "E:\testcopy\2014.03.19 08.24.33"
the script is working perfectly and output file is created with the input inside, but if i am using $folder as shown above, i got that error and file is also created but is empty.

I have placed the script in the root folder E:\testcopy but nothing changed.

What i am trying to do is that i have many sub folders  in E:\testcopy each one named with the date-time that its created, and i want to fetch some information from log files inside from the last created sub folder. 

Can someone tell me what do i do wrong.

 


Viewing all articles
Browse latest Browse all 10624

Trending Articles