Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Out-File redirection is driving me crazy

Reply
 
Old 11-27-2007   #1 (permalink)
Cesar Garcia


 
 

Out-File redirection is driving me crazy

Hi everyone,
I'm taking a citrix exported user list and converting it to import into AD.
I have a app file for each application and want to save userlist in another
file.
My script runs like this:
PS D:\> $files= dir *.app
PS D:\> foreach ($file in $files) {Get-Content $file |
Select-String -SimpleMatch domainname
|% {$_ -replace '"domainname"/"','domainname\'}
|% {$_ -replace '";',';'}
|% {$_ -replace '".',';'}
| out-file -filepath "c:\$file" }

It reports Out-File : The given path's format is not supported.

If I just use $file it complains: Get-Content : The process cannot access
the file because it is being used by another process.

Well, I just found it while i was writing this. $file is full path, so I was
doing C:\d:\...

I could solve it using $file.Name (almost)



My System SpecsSystem Spec
Old 11-27-2007   #2 (permalink)
Keith Hill [MVP]


 
 

Re: Out-File redirection is driving me crazy

"Cesar Garcia" <CesarGarcia@xxxxxx> wrote in message
news:86B42B1F-3172-46E6-9B30-255CA207DD27@xxxxxx
Quote:

> Hi everyone,
> I'm taking a citrix exported user list and converting it to import into
> AD.
> I have a app file for each application and want to save userlist in
> another
> file.
> My script runs like this:
> PS D:\> $files= dir *.app
> PS D:\> foreach ($file in $files) {Get-Content $file |
> Select-String -SimpleMatch domainname
> |% {$_ -replace '"domainname"/"','domainname\'}
> |% {$_ -replace '";',';'}
> |% {$_ -replace '".',';'}
> | out-file -filepath "c:\$file" }
>
> It reports Out-File : The given path's format is not supported.
>
> If I just use $file it complains: Get-Content : The process cannot access
> the file because it is being used by another process.
>
> Well, I just found it while i was writing this. $file is full path, so I
> was
> doing C:\d:\...
>
> I could solve it using $file.Name (almost)
Since Get-Content is reading the file while you are trying to write back to
the same file you are going to run into problems with this approach so try
this:

PS D:\> $files= dir *.app
PS D:\> foreach ($file in $files) { (Get-Content $file) |
Select-String -SimpleMatch domainname
|% {$_ -replace '"domainname"/"','domainname\'}
|% {$_ -replace '";',';'}
|% {$_ -replace '".',';'}
| out-file $file }

Putting the () around the Get-Content will cause it to read the while file
in before it starts sending lines down the pipe. If your file is really
large, this approach isn't the best way to accomplish your goal. In that
case, use a temp file and when you are done copy the temp file over the
original file.

--
Keith

My System SpecsSystem Spec
Old 11-27-2007   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Out-File redirection is driving me crazy

"Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
news:6AD7E017-2592-4677-932A-D94CE4F4B265@xxxxxx
Quote:

> Putting the () around the Get-Content will cause it to read the while file
That should say "cause it to read the *whole* file".

--
Keith

My System SpecsSystem Spec
Old 11-28-2007   #4 (permalink)
Cesar Garcia


 
 

Re: Out-File redirection is driving me crazy

My problem was with $file in Out-File. Sometimes I was getting full path like
D:\path\to\file\excel.app and sometimes just file. I wasn't getting expected
results even using $file.Name so... I added an extesion and wrote following
code:

foreach ($file in $files) {Get-Content $file |
Select-String -SimpleMatch munimadrid
|% {$_ -replace '"munimadrid"/"','munimadrid\'}
|% {$_ -replace '";',';'}
|% {$_ -replace '".',';'}
|out-file ("$file" + '.list') }

And it just works. I'll try your approach Keith to see if it improves
performance.
My System SpecsSystem Spec
Old 11-28-2007   #5 (permalink)
Cesar Garcia


 
 

Re: Out-File redirection is driving me crazy

I was getting some problems with $file name. Sometimes I was getting full
path, sometimes just $files.Name. Anyway I was unable to get it working,
until I followed this approach.

PS D:> foreach ($file in $files) {Get-Content $file |
Select-String -SimpleMatch domainname
|% {$_ -replace '"domainname"/"','domainname\'}
|% {$_ -replace '";',';'}
|% {$_ -replace '".',';'}
|out-file ("$file" + '.list') }

I'll try your suggestion too Keith, to see if it improves performance
anyway. Thanks
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Driving me crazy ! Network & Sharing
WMI driving me crazy VB Script
This is ABSOLUTELY driving me crazy: file type associations/default programs Vista file management
Driving me crazy.... Vista mail
How do I do this - it's driving me crazy Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46