我有很長的腳本需要添加到創建的文件,問題是它的腳本,它包含大量的特殊字符。在powershell中添加腳本到文件
而且我收到很多錯誤,我已經把腳本放在''中,但它沒有按照我的預期工作。
有沒有一種簡單的方法來做到這一點,比如拿一個文本,然後將它添加到文件,以某種方式與特殊的字符?
powershell.exe Add-Content C:\Testing\Powershell\PageFeature.ps1 - 'Function Press-Button
{
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
}
Function Resize-Window
{
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 300
$newsize.width = 128
$pswindow.buffersize = $newsize
$newsize = $pswindow.windowsize
$newsize.height = 5
$newsize.width = 128
$pswindow.windowsize = $newsize
}
Function Run-Tool
{
$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = "C:\Testing\bin.exe"
$ps.StartInfo.RedirectStandardInput = $true
$ps.StartInfo.UseShellExecute = $false
$ps.start()
while (! $ps.HasExited) {
Start-Sleep -s 5
write-host "I will press button now..."
Press-Button
}
Write-Output "Default key was pressed"
Write-Output "exit code: $($ps.ExitCode)"
}
Resize-Window
Run-Tool'
由於您在代碼中使用單引號,因此不能只是簡單地使用它們來包含整個事物。如果在PowerShell中執行此操作,請使用[herestring](https://technet.microsoft.com/en-us/library/ee692792.aspx)''@''nThext和stuff'n「@''。你似乎也有一個浮動連字符。你爲什麼試圖完全做到這一點。你從哪裏做這個? – Matt