0

I am having long script that need to be added to created file, problem is its a script and it contains lots of special chars.

And I am getting a lot of error, I've put script in '' but it did not work as i expected.

Is there an easy way to do it, like taking a text and just adding it to file, somehow with special chars?

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'
1
  • Since you use single quotes in the code you cannot just simply use them to enclose the entire thing. If this is being executed in PowerShell use a herestring @"`nThext and stuff`n"@. You seem to have a floating hyphen as well. Why are you trying to do this exactly. Where are you doing this from? Commented Jul 5, 2017 at 13:23

2 Answers 2

1

You would want to use a Here String to define the block of text. You begin a here-string by using @" and end it by using "@.

The @" must be the last thing on the first line and the closing "@ must be the first two characters on the next line:

$a = @"
This is a here-string. I can type "anything" I want,
even carriage returns, and it will all be preserved.
No need to escape!
"@

Using it with your script would look like this:

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
"@
Sign up to request clarification or add additional context in comments.

1 Comment

This will only work 1) if you're already in PowerShell and 2) if you use @' '@ (single quotes), because substitution is still active inside @" "@ and that's almost certainly not what you want when the text is a script.
0

You should dot-source your external script. This effectively dumps the contents of that script into your working script. At the top (or wherever you want the code to be initialized):

. "\\Path\to\Script.ps1"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.