2

I am trying to create a PowerShell script by another PowerShell script. I have something like: >

    $scriptBlock = {
    write-host "this is the body of the script that I want to add to another PS script"
    if($true){
     write-host "so that I can execute this automatically created script somewhere "
        }
    }
    $scriptBlock | Out-String -Width 4096 | Out-File "c:\test.ps1"

AtRunUnattended.exe "$($Dict.Get_Item("MASTER_DIRECTORY_PATH"))\MEDIA_FILE\" /S /noreboot /L logfile="%TEMP%\AT $($Dict.Get_Item("PRODUCT_NAME")) V8.4.log" altsource="C:\temp\baf\mediafile"

However, when I have some long single-line scripts like the one shown above, it gets automatically wrapped so the out-put .ps1 file would not work as if I had invoked the script directly from the parent script. So in the .ps1 file that the parent .ps1 script created, the code then looks like this:

ElseIf($str.StartsWith("DRIVER_DATE=")){$str = "DRIVER_DATE=$(Get-Date 
-f MM-dd-yyyy))"}

which will not run properly if it is run.

So does anybody know how to text-format scriptblocks so that they can be properly written to another child script file for further execution? I did some research and I think that it might have something to do with PS's internal text buffer width or something. I tried other out-file methods as well like [System.IO.StreamWriter], however all of them look the same--wrapped and limited to a certain width per line.

Please help me, thank you!

The entire purpose of this thing is to generate some scripts automatically and remotely execute these created scripts on other machines.

FrozenLand
  • 259
  • 1
  • 4
  • 14
  • Have you tried to use the -Width operator with the Out-File command? – Musaab Al-Okaidi Sep 23 '13 at 15:23
  • @Musaab Thank you! I tried and it worked. Furthermore, what unit is this width in? Number of characters? Bytes? – FrozenLand Sep 23 '13 at 15:38
  • I'll post as the answer and I appreciate if you could select it as the correct answer. As for the Width operator, yes it's the number of characters as you can see here: http://technet.microsoft.com/en-us/library/hh849882.aspx – Musaab Al-Okaidi Sep 23 '13 at 15:43

2 Answers2

2

Use the -Width parameter with the Out-File cmdlet as follows:

Out-File -FilePath "c:\test.ps1"  -Width 4096
Musaab Al-Okaidi
  • 3,734
  • 22
  • 21
  • Is there an upper limit to this? Is 4096 a safe value as a parameter to -width? I dont have crazy code but i want to make sure that a line of maybe 500 char long can fit. – FrozenLand Sep 23 '13 at 15:50
  • Good question, but I'm not too sure what the upper limit would be. To make sure always give the right number, you can set the width size to the length of the script block characters as follows: -Width $ScriptBlock.ToString().Length – Musaab Al-Okaidi Sep 23 '13 at 15:56
  • The width is not the same as the length right? Each line of my script block might be long but I know they wont be longer than at most 1000. However the whole block might be indefinitely long, so I hope the width has nothing to do with length. – FrozenLand Sep 23 '13 at 16:25
  • Yes you're right. What I said only applies if you're using one line script blocks. – Musaab Al-Okaidi Sep 24 '13 at 10:27
0

the variable will not be expanded in single quote. May be you need a sample like this:

$scripts=@'
$date=get-date
"Hello,now is $date"
'@

$scripts | Out-File second.ps1
./second.ps1 

Output is:

Hello,now is 09/23/2013 23:41:18
Mosser Lee
  • 48
  • 1
  • 8
  • Thanks. However the problem I am having here is that when I have something like: > dostuff.exe -option1 sss -option2 sss .... all the way to option99 (just an example), if each option and its argument is relatively long and take up about the length of the internal line buffer, then all the options, in the resulting .ps1 file, will be written to separate lines, making the script unexecutable. – FrozenLand Sep 23 '13 at 15:47