Rakhann
Rakhann

Reputation: 3

Result to variable in a batch file

I turn in circles and after much research, I seek your expertise on this small case. I can send the result of a PS1 script to a text file, but not to a batch file variable.

the script

    Param(
[string]$Fic
)

$EmplacementFichier = [string]

$EmplacementFichier = "$Fic"
$MonFichier = get-content -totalcount 1 $EmplacementFichier
$Resultat = $MonFichier.SubString(92,12)

$RnmFic = "EXANTE_$resultat.REPRESTI.txt"
rename-item $EmplacementFichier -newname $RnmFic

Write-Output $RnmFic

Launched from a batch file:

powershell D:\Rnm-Exante.ps1 -fic "%NOMFIC%" > %Fichier%

It creates a file "%Fichier%" at the location of the script, but does not provide the batch variable.

Upvotes: 0

Views: 141

Answers (1)

Stephan
Stephan

Reputation: 56155

from batch, you can read the file:

<file.ext set /p "var="`

or you can get the output of your powershell script directly (do not redirect to a file in this case):

for /f "delims=" %%a in ('powershell D:\Rnm-Exante.ps1 -fic "%NOMFIC%"') do set "var=%%a"

Upvotes: 1

Related Questions