1

I'm trying to make a standalone batch file that automates the process of setting up a server for the game "Killing Floor 2." To do this, I need to create then execute a PowerShell script to gain access to the resources I need. So far I have this

@echo off
del script.ps1
(echo $url = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip") >> script.ps1
(echo $output = "$PSScriptRoot\steamcmd.zip") >> script.ps1
(echo $start_time = Get-Date) >> script.ps1
(echo $wc = New-Object System.Net.WebClient) >> script.ps1
(echo (New-Object System.Net.WebClient).DownloadFile($url, $output)) >> script.ps1
(echo Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)") >> script.ps1
(echo Add-Type -AssemblyName System.IO.Compression.FileSystem) >> script.ps1
(echo function Unzip) >> script.ps1
(echo {) >> script.ps1
(echo param([string]$zipfile, [string]$outpath)) >> script.ps1
(echo [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)) >> script.ps1
(echo }) >> script.ps1
(echo Unzip "$output" "$PSScriptRoot") >> script.ps1
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File script.ps1 -Verb RunAs'}"
timeout /t 5
SteamCmd +login anonymous +force_install_dir ./kf2_ds +app_update 232130 +exit
cd kf2_ds
start kf2server.bat

However, during the write of the PowerShell script, I get the following error

.DownloadFile($url was unexpected at this time.

Any ideas on how to make this work or a better way to accomplish this?

5
  • 1
    You might be interested in a Batch + PowerShell hybrid format, rather than trying to battle with a ton of echoes and creating a temporary .ps1 script. This post demonstrates my preferred method. On a side note, why are you defining $wc but never using it? Commented Nov 7, 2016 at 3:26
  • $wc was left over from a previous version of the script. Thanks for pointing it out so I remembered to remove it. Commented Nov 7, 2016 at 4:30
  • Without being critical but curiosity,, why not call directly a PowerShell script with argument? It would be easier to debug no? Commented Nov 7, 2016 at 7:08
  • @Esperento57 It's because I need to automate the process into one file Commented Nov 24, 2016 at 2:07
  • A script powershell that writes in a powershell script and execute powershell.exe it works, all in one file :) But that's your choice. Good day to you. Commented Nov 24, 2016 at 6:37

1 Answer 1

3

It's not necessary to (enclose echo string in parentheses)

Try

(
 echo string1
 echo string2
)>filename

BUT your problem is that the ) is closing the (echo - the second ( has no effect as a special character - it's treated as a standard character, so you need to escape any literal ) to be echoed with a caret ^) (regardless of whether you use your method or mine).

Sign up to request clarification or add additional context in comments.

Comments