none
Powershell Scheduled Task "The string is missing the terminator"

    Question

  • Im trying to pull this off in one line, and it works fine if i just execute it in powershell.  If i execute it from the run command like a scheduled task would though, i get the error "The string is missing the terminator".

    Heres what works from within powershell:

    $H=hostname;export-dhcpserver ("c:\DHCP_BACKUP\"+$H+".xml") -leases -force

    The gist is that I am declaring $H as the product of the "hostname" tool, then using it to create the filename.  Unfortunately the name of the folder is "DHCP_BACKUP$" with the dollar sign but i tried using another folder with the same result.

    Here is what fails.  I left the -noexit command on for troubleshooting and execute the command from "Run"

    powershell -noexit -command &{$H=hostname;export-dhcpserver ("c:\DHCP_BACKUP$\"+$H+".xml") -leases -force}


    • Edited by jkuhn Tuesday, December 17, 2013 7:47 PM
    Tuesday, December 17, 2013 7:38 PM

Answers

  • I'm not sure what's giving you trouble here, but one feature I'd like to point out is PowerShell.exe's EncodedCommand parameter. It's designed for exactly this kind of situation; calling a command with complicated quoting / braces / etc which can be annoying in whatever system you're using to call the command. EncodedCommand takes a base64-encoded representation of the command, which you can generate within PowerShell itself:

    #
    
    $command = '&{$H=hostname;export-dhcpserver ("c:\DHCP_BACKUP$\"+$H+".xml") -leases -force}'
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    
    [System.Convert]::ToBase64String($bytes)
    
    # JgB7ACQASAA9AGgAbwBzAHQAbgBhAG0AZQA7AGUAeABwAG8AcgB0AC0AZABoAGMAcABzAGUAcgB2AGUAcgAgACgAIgBjADoAXABEAEgAQwBQAF8AQgBBAEMASwBVAFAAJABcACIAKwAkAEgAKwAiAC4AeABtAGwAIgApACAALQBsAGUAYQBzAGUAcwAgAC0AZgBvAHIAYwBlAH0A
    
    # From the task scheduler:
    
    powershell.exe -EncodedCommand JgB7ACQASAA9AGgAbwBzAHQAbgBhAG0AZQA7AGUAeABwAG8AcgB0AC0AZABoAGMAcABzAGUAcgB2AGUAcgAgACgAIgBjADoAXABEAEgAQwBQAF8AQgBBAEMASwBVAFAAJABcACIAKwAkAEgAKwAiAC4AeABtAGwAIgApACAALQBsAGUAYQBzAGUAcwAgAC0AZgBvAHIAYwBlAH0A
    
    #

    • Proposed as answer by Mike Laughlin Tuesday, December 17, 2013 8:07 PM
    • Marked as answer by jkuhn Thursday, December 19, 2013 9:32 PM
    Tuesday, December 17, 2013 7:56 PM

All replies

  • I'm not sure what's giving you trouble here, but one feature I'd like to point out is PowerShell.exe's EncodedCommand parameter. It's designed for exactly this kind of situation; calling a command with complicated quoting / braces / etc which can be annoying in whatever system you're using to call the command. EncodedCommand takes a base64-encoded representation of the command, which you can generate within PowerShell itself:

    #
    
    $command = '&{$H=hostname;export-dhcpserver ("c:\DHCP_BACKUP$\"+$H+".xml") -leases -force}'
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    
    [System.Convert]::ToBase64String($bytes)
    
    # JgB7ACQASAA9AGgAbwBzAHQAbgBhAG0AZQA7AGUAeABwAG8AcgB0AC0AZABoAGMAcABzAGUAcgB2AGUAcgAgACgAIgBjADoAXABEAEgAQwBQAF8AQgBBAEMASwBVAFAAJABcACIAKwAkAEgAKwAiAC4AeABtAGwAIgApACAALQBsAGUAYQBzAGUAcwAgAC0AZgBvAHIAYwBlAH0A
    
    # From the task scheduler:
    
    powershell.exe -EncodedCommand JgB7ACQASAA9AGgAbwBzAHQAbgBhAG0AZQA7AGUAeABwAG8AcgB0AC0AZABoAGMAcABzAGUAcgB2AGUAcgAgACgAIgBjADoAXABEAEgAQwBQAF8AQgBBAEMASwBVAFAAJABcACIAKwAkAEgAKwAiAC4AeABtAGwAIgApACAALQBsAGUAYQBzAGUAcwAgAC0AZgBvAHIAYwBlAH0A
    
    #

    • Proposed as answer by Mike Laughlin Tuesday, December 17, 2013 8:07 PM
    • Marked as answer by jkuhn Thursday, December 19, 2013 9:32 PM
    Tuesday, December 17, 2013 7:56 PM
  • Another alternative is to save your working script to disk and then use the -File parameter instead of -Command.

    Don't retire TechNet! - (Don't give up yet - 12,420+ strong and growing)

    Tuesday, December 17, 2013 8:07 PM
  • Thanks.  I was trying to keep in it one line so as to not need accompanying files.  I plan on deploying the scheduled task via group policy and while I could also include a script file, it would be nice to have this all self contained.

    That encoded command works, but im curious why i couldnt make it work the way i had it, i assumed there was a space or quote problem somewhere in there

    Tuesday, December 17, 2013 11:08 PM
  • You probably can make it work, but you'd have to figure out how the system you were calling powershell.exe from treats things like quotation marks, parentheses and braces. Here's the form I've had the most consistent results with:

    PowerShell.exe -Command "<Your entire command inside the double quotes>"

    If your command contains embedded double quotes, you may have to fiddle with that a bit to see whether they need to be escaped.  EncodedCommand avoids all that trial-and-error, which makes it nice for this sort of thing.

    Wednesday, December 18, 2013 1:21 AM