0

I am hoping someone can help me on this, I have a script that Removes and updates FSRM groups on windows server 2012-2019, I want to create a ps script that cycles through a text file, in each line it stores the value as a variable that can be used in the batch file.

here is my code below; I just can't think of where to go after this;

$BF = gc "C:\Installs\FSRM_SCRIPTS\FILESCREEN_EXPORT\FSRM_Groups.txt"
$env:FSRM_Group
 
$FS = Foreach($BT in $BF){

Start-Process "cmd.exe"  "/c C:\INSTALLS\FSRM_SCRIPTS\FILESCREEN_EXPORT\New_Exports\Batch_Import.bat"
}

$FS | out-file "C:\Installs\FSRM_SCRIPTS\FILESCREEN_EXPORT\New_Exports\Results\$env:COMPUTERNAME - Import.txt"
  • 4
    Where is the `$BT` variable used? – lit Dec 07 '20 at 16:27
  • 2
    If you want to pass the value of `$BT` to the batch file _as an argument_: `Start-Process "cmd.exe" "/c C:\INSTALLS\FSRM_SCRIPTS\FILESCREEN_EXPORT\New_Exports\Batch_Import.bat \`"$BT\`""`. If you know that the paths never contain spaces or other special chars., you can omit the embedded `\`"`. Are you using `Start-Process` deliberately, to run the batch files _asynchronously_, each in a _new window_? Note that `Start-Process` doesn't output the batch file's output, so `$FS` will receive no value, and the loop will finish quickly, _before_ the batch file runs do. – mklement0 Dec 07 '20 at 16:55
  • 1
    To synchronously execute console applications or batch files and receive their output, call them _directly_ (`c:\path\to\some.exe ...` or `& $exePath ...`), do _not_ use `Start-Process` - see [this answer](https://stackoverflow.com/a/51334633/45375). – mklement0 Dec 07 '20 at 16:56

1 Answers1

0

ok, so got it working as needed, but for reference, here is my code


$env:FSRM_Group

Foreach($BT in $BF){

$env:FSRM_Group = $BT

if($BT -match '([\w-?%s]+)'){
    #Write-host "Valid"
    $env:FSRM_Group2 = "Anti-Ransomware File Group"
}else{
    #Write-host "Invalid"
    $env:FSRM_Group2 = $BT
}
Start-Process "cmd.exe"  "/c C:\INSTALLS\FSRM_SCRIPTS\FILESCREEN_EXPORT\New_Exports\Batch_Import.bat"

Write-output "$BT Installed Successfully" |out-file "C:\Installs\FSRM_SCRIPTS\FILESCREEN_EXPORT\New_Exports\Results\$env:COMPUTERNAME - Import.txt" -append

}```