Why is this script not running from powershell

Bill Prew made a VBscript what i want to run from powershell but its not working,
If i run it from the prompt it works perfect.
The command i use in a unrestricted powershell environment
cscript.exe C:\temp\test.vbs

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28648240.html#a40704369

Thanks for helping

Danny
LVL 6
Danny KonAsked:
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.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Is that all you use? Because it should not make any difference whether you run the VBS from a cmd or powershell prompt ... Doesn't it perform anything, are you getting an error message, or what happens?

Anyway, I would use a PS script here. You should have insisted on one in your prior question ;-).
This should do the same as the VBS code, it is using almost the same logic:
$FileIn = 'test.txt'
if (!(Test-Path $FileIn))
{
  Write-Error "ERROR: Input file `"$FileIn`" does not exist."
  return
}

$header = Get-Content $FileIn | select -First 2
Get-Content $FileIn | Select -Skip 2 | ? { $_ } |
  % {
    $line = $_ -replace "0:00:00", "       " 
    $Client = $line.SubString(20,50).Trim()
    if (!(Test-Path $client+'.txt')) { $line = "$header`r`n$line" }
    $line | Out-File -Append $client+'.txt'
  }

Open in new window

0
Danny KonAuthor Commented:
Olemo,

Because i tested you script i see what is the problem, i expected the output will go to c:\temp but the output files are in c:\windows\system32

There is some difference between the script from Bill Brew and yours
your output shows a + sign after the name eg Cafe Engel+, Cafe Tetra+ and there is a file
called ----------+.txt and is empty .
Can you also make that i can adjust the output directory?

Thanks

Danny
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
None of the scripts (natively) supplies a path, so the result files will be in the current working directory you call the script in.

One approach is to first change the current directory to the one you want the files to be created in, using set-location c:\temp (the cmdlet alias is cd).

Another approach is that we use the path info from the input file, and create the files in that path. I'll take this one:
$FileIn = 'C:\Temp\test.txt'
if (!(Test-Path $FileIn))
{
  Write-Error "ERROR: Input file `"$FileIn`" does not exist."
  return
}

$path = split-path -parent $FileIn 
$header = Get-Content $FileIn | ? { $_ } | select -First 2
Get-Content $FileIn | ? { $_ } | Select -Skip 2 | 
  % {
    $line   = $_ -replace "0:00:00", "       " 
    $FileOut= $path + $line.SubString(20,50).Trim() + '.txt'
    if (!(Test-Path $FileOut)) { $line = "$header`r`n$line" }
    $line | Out-File -Append $FileOut
  }

Open in new window

This also resolves the issue with '-------' and the added plus in the file names. (I hate PowerShell trying to do smart things with strings while I don't want it to.)
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Danny KonAuthor Commented:
Olemo,

I don't know what i am doing wrong here? I don't get any output but also no error

 Thanks

Danny
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The files are created in the Temp folder (where the input file is located). Did you check that?
0
Danny KonAuthor Commented:
Olemo

Yes i did but nothing ??

Danny
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Ooops, a backslash was missing. The files are created in the folder above the input file, i.e. C:\ here.
This one is tested ;-).
$FileIn = 'C:\Temp\EE\test.txt'
if (!(Test-Path $FileIn))
{
  Write-Error "ERROR: Input file `"$FileIn`" does not exist."
  return
}

$path = split-path -parent $FileIn 
$header = Get-Content $FileIn | ? { $_ } | select -First 2
Get-Content $FileIn | ? { $_ } | Select -Skip 2 | 
  % {
    $line   = $_ -replace "0:00:00", "       " 
    $FileOut= $path + '\' + $line.SubString(20,50).Trim() + '.txt'
    if (!(Test-Path $FileOut)) { $line = @($header, $line) }
    $line | Out-File -Append $FileOut
  }

Open in new window

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
Danny KonAuthor Commented:
Olemo,

Works perfect thanks so much

Danny
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
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.