我正在尝试创建一个独立的批处理文件,以自动设置游戏“Killing Floor 2”的服务器。为此,我需要创建然后执行PowerShell脚本以访问我需要的资源。到目前为止我有这个
@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
但是,在编写PowerShell脚本期间,我收到以下错误
.DownloadFile($url was unexpected at this time.
关于如何使这项工作或更好的方法来实现这一目标的任何想法?
答案 0 :(得分:3)
没有必要(将回声字符串括在括号中)
尝试
(
echo string1
echo string2
)>filename
但问题是)
正在关闭(echo
- 第二个(
作为特殊字符无效 - 它被视为标准字符,所以您需要使用插入符号)
将任何文字echo
转义为^)
(无论您使用的是方法还是我的方法)。