Results 1 to 3 of 3
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts

    Powershell has be all Googled Out!

    Hey Y'all,

    I'm at my wits end (and I only started with half of one!) over getting PowerShell to pass a quoted string to the Register-ScheduledTask cmd-let.

    Here's my code:
    Code:
    <#+-----------------------------------------------------------------+
      | Create Task to get around UAC Prompt                            |
      | Programmed by: RetiredGeek (WSL) aka: ComputerMentor            |
      | Created      : 21 Nov 2014                                      |
      | Last Updated :                                                  |
      | Current Vers : 1.0                                              |
      +-----------------------------------------------------------------+
    #>
    
    function TestIt {
    param(
         [Parameter(Mandatory=$true)]
            [String]$TaskName,
         [Parameter(Mandatory=$true)]
            [String]$TaskCommand,
         [Parameter(Mandatory=$false)]
            [String]$TaskArg,
         [Parameter(Mandatory=$false)]
            [String]$TaskDesc="No Description Supplied"
    )
    
    "Name: $TaskName"
    "CMD : $TaskCommand"
    "Args: $TaskArg"
    
    $TaskAction = New-ScheduledTaskAction -Execute "$TaskCommand" `
                                          -Argument "$TaskArg"
    
    Register-ScheduledTask -Action $TaskAction  `
                           -TaskName "$TaskName" `
                           -User "DellXPS8700\Bruce" `
                           -RunLevel Highest `
                           -Description "$TaskDesc"
    }
    
    Testit -TaskCommand "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE" `
           -TaskArg "G:\BEKDocs\Customizations\Auto-Image Backup Log.xlsm" `
           -TaskName "UACByPass"
    Here's the Output in PS:
    Code:
    Name: UACByPass
    CMD : C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE
    Args: G:\BEKDocs\Customizations\Auto-Image Backup Log.xlsm
    
    TaskPath                                       TaskName                        
    --------                                       --------                        
    \                                              UACByPass
    Here's what shows up in the TaskScheduler:
    TaskSchActions.JPG
    You'll notice that there are no quotes around either of the items!

    I've tried appending a second set of double quotes to the string (TaskScheduler doesn't like single quotes). as follows:

    [noparse]$TaskArgsq = '"'+$TaskArgs+'"'

    But the PS throws off errors about the variable type not matching the data type for the object $TaskActions.

    I've also tried using escaping sequences to no avail.

    HELP! headbang.gif
    Last edited by RetiredGeek; 2014-11-21 at 15:05.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    RG,

    Did you try escaping the double-quotes for the $TaskCommand and $TaskArg variables?


    Code:
    $TaskAction = New-ScheduledTaskAction -Execute `"$TaskCommand`" `
                                          -Argument `"$TaskArg`"
    There may be other/better ways of doing it, but using the backtick seems to work.

    HTH

  3. The Following User Says Thank You to Cliff.H For This Useful Post:

    RetiredGeek (2014-11-21)

  4. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Cliff,



    I tried that the Hard (WRONG) Way! "`"$TaskCommand`"" and of course that didn't work. Thanks so much!
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •