#1 06 Dec 2011 15:05

theretard
Member
Registered: 01 Mar 2011
Posts: 33

merge

merge (append)

%temp%\1log.txt

to

%trmp%\2log.txt

to

%trmp%\3log.txt

to

%trmp%\4log.txt

to

%trmp%\5log.txt

anyone know how?, + the code to call the (merger).ps1 from a .cmd minimized while waiting for the .ps1 to terminate before continuing on with the .cmd script?

Offline

#2 06 Dec 2011 15:25

carltonjacobson
Member
Registered: 22 Nov 2011
Posts: 7

Re: merge

You can run a powershell script from the command prompt like any other command.  The script will terminate before the batch script continues.  You may want to include in your script enabling running of powershell scripts because it is disabled by default.

- Enable powershell scripts on the machine:
powershell "Set-ExecutionPolicy RemoteSigned"
- Run a script
powershell U:\scripts\script.ps1

To append to files, use Get-Content and Add-Content:

Append 1log to the end of 2log:

Add-Content 2log.txt -value (Get-Content 1log.txt)

Offline

#3 06 Dec 2011 15:33

theretard
Member
Registered: 01 Mar 2011
Posts: 33

Re: merge

carltonjacobson wrote:

You can run a powershell script from the command prompt like any other command.  The script will terminate before the batch script continues.  You may want to include in your script enabling running of powershell scripts because it is disabled by default.

so I dont need to create a .ps1 file then run it?

- Enable powershell scripts on the machine:
powershell "Set-ExecutionPolicy RemoteSigned"
- Run a script
powershell U:\scripts\script.ps1

using this in my .cmd;

REG ADD HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell /v ExecutionPolicy /t REG_SZ  /d "RemoteSigned" /f >NUL
-do stuff
REG DELETE HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell /v "ExecutionPolicy" /f >NUL

To append to files, use Get-Content and Add-Content:

Append 1log to the end of 2log:

Add-Content 2log.txt -value (Get-Content 1log.txt)

$file2 = Get-Content "%temp$\Alog.txt"Add-Content "%temp%Xlog.txt" $file2

thats what I have tried but it just fails, how would I script your method?

Offline

#4 06 Dec 2011 15:46

carltonjacobson
Member
Registered: 22 Nov 2011
Posts: 7

Re: merge

You do need to create a .ps1 file and call it from command line or alternately pass all the arguments to powershell using the -command switch.  I think creating a file will be easier unless you have special circumstances.

Call .ps1 file:

powershell U:\scripts\script.ps1

Your script will not work because powershell variables do not have the same syntax as command shell variables.  A powershell variable beings with a '$' while variables used in command shell are encased with "%_%".  You either design your .ps1 script to accept arguments from the .bat file or design your powershell script to create the variables.

Offline

#5 06 Dec 2011 16:05

theretard
Member
Registered: 01 Mar 2011
Posts: 33

Re: merge

thanks, the .cmd was creating the .ps1 and it outputed this;

$file2 = Get-Content "C:\Users\user\AppData\Local\Temp\Alog.txt"Add-Content "C:\Users\user\AppData\Local\TempXlog.txt" $file2

as the .ps1, when called the cmd window does change but the red error text is like really small so I dont know, any idea what's wrong with it?

Offline

#6 06 Dec 2011 19:50

carltonjacobson
Member
Registered: 22 Nov 2011
Posts: 7

Re: merge

It's probably just that you need to have a line break between the get-content command and the add-content command, otherwise it looks like it would work. Also there might be incorrect file name or something being passed to it.  But for testing why not just open up a powershell window and run the script?

Offline

#7 07 Dec 2011 05:21

theretard
Member
Registered: 01 Mar 2011
Posts: 33

Re: merge

carltonjacobson wrote:

It's probably just that you need to have a line break between the get-content command and the add-content command, otherwise it looks like it would work. Also there might be incorrect file name or something being passed to it.  But for testing why not just open up a powershell window and run the script?


I got it, thanks, creating the .ps1 from the batch did not function even though the batch wrote the %temp%\Xlog.txt on to the .ps1 as the actual location, changing the batch to write $env:temp\Xlog.txt instead fixed the issue.

Offline

Board footer

Powered by FluxBB