Schedule PowerShell commands - windows

I want to use the follwing powershell commands to automatically record a stream with vlc:
$rd = get-date -format "yyyyMMdd"
C:\"Program Files"\VideoLan\vlc\vlc.exe https://someurl/playlist.m3u --sout "#file{dst=C:\users\me\desktop\$rd.mp3}" --run-time=15 vlc://quit
That does work well if I save it as a .ps1 script and execute it with "Right Click > Run with PowerShell".
It also works if I paste those commands to the powershell and execute them in one line, like this:
$rd = get-date -format "yyyyMMdd"; C:\"Program Files"\VideoLan\vlc\vlc.exe https://someurl/playlist.m3u --sout "#file{dst=C:\users\me\desktop\$rd.mp3}" --run-time=15 vlc://quit
It even works if I execute this from within the Windows Terminal:
powershell -command {$rd = get-date -format "yyyyMMdd"; C:\"Program Files"\VideoLan\vlc\vlc.exe https://someurl/playlist.m3u --sout "#file{dst=C:\users\me\desktop\$rd.mp3}" --run-time=15 vlc://quit}
So I created a task in the Windows Task Scheduler to automate the execution.
This is the XML of the "Actions" tab:
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>-noexit -command {$rd = get-date -format "yyyyMMdd"; C:\"Program Files"\VideoLan\vlc\vlc.exe https://someurl/playlist.m3u --sout "#file{dst=C:\users\me\desktop\$rd.mp3}" --run-time=15 vlc://quit}</Arguments>
</Exec>
</Actions>
Now, once the task is triggered, the opened PowerShell shows this error:
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
What am I missing?

Related

Windows bat file to open new window and launch script [duplicate]

This question already has an answer here:
Escaping the and (&) sign in Powershell
(1 answer)
Closed 5 months ago.
Tools : Windows 7 & powershell & Rsync
Goal : Create a bat file to launch rsync in such a way that the output is scrolling in the new window. Once command terminates the window shall remain open for inspection.
Tried this code :
start powershell \k C:\Program Files2\Git\usr\bin\rsync.exe -av /d/Images/Dia_scans /f/Shiva_D/Images
When double click on the bat file there seems to be a short popup. But it closes and then nothing happens.
What's missing?
UPDATE:
Following works in cmd:
start cmd /K "C:\Program Files2\Git\usr\bin\rsync.exe" -av /d/Images/Dia_scans /f/Shiva_D/Images
But desire is to use powershell:
start powershell -NoExit "C:\Program Files2\Git\usr\bin\rsync.exe" -av /d/Images/Dia_scans /f/Shiva_D/Images
Throws this error:
The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:11
+ C:\Program <<<< Files2\Git\usr\bin\rsync.exe -av /d/Images/Dia_scans /f/Shiva_D/Images
+ CategoryInfo : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
What is proper quotation rule for powershell vs cmd when path / argument has space char?
UPDATE2:
Proposed code:
start powershell -NoExit Start-Process -NoNewWindow "C:\Program Files2\Git\usr\bin\rsync.exe -av --delete /d/Images/Dia_scans /f/Shiva_D/Images"
Gives popup window with this error.
Start-Process : A parameter cannot be found that matches parameter name 'av'.
At line:1 char:71
+ Start-Process -NoNewWindow C:\Program Files2\Git\usr\bin\rsync.exe -av <<<<
--delete /d/Images/Dia_scans /f/Shiva_D/Images
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
UPDATE3/CLOSING
It's easier to user cmd. I am abandoning powershell for now. Using this code in cmd works fine and is reasonably intuitive.
start cmd /K "C:\Program Files2\Git\usr\bin\rsync.exe" -av --delete /d/Images/Dia_scans /f/Shiva_D/Images
Try -noexit rather than /k
For testing run your command in an already open cmd window so you'll see any errors.
You need to escape the spaces.
For example:
start powershell -NoExit & "C:\Program Files2\Git\usr\bin\rsync.exe" -av /d/Images/Dia_scans /f/Shiva_D/Images
EDIT:
Forgot about the goal... Try to use -NoNewWindow
start powershell -NoExit Start-Process -NoNewWindow "C:\Program Files2\Git\usr\bin\rsync.exe" -av /d/Images/Dia_scans /f/Shiva_D/Images
EDIT2::
start powershell -NoExit Start-Process -NoNewWindow "C:\Program Files2\Git\usr\bin\rsync.exe" -ArgumentList '-av /d/Images/Dia_scans /f/Shiva_D/Images'

How can I launch a download from the internet using command prompt?

I want create a program than launch a download from the internet using a command of command-prompt.
Do you know this command ?
use the command : powershell -command "& { iwr http://upload.wikimedia.org/wikipedia/it/8/83/Fanta-Logo.png -OutFile logo.jpg }"

Powershell Write Bitlocker status to text file

I am trying to write the bitlocker status to a text file via powershell by invoking a cmd shell but it doesn't seem to work. Any ideas?
Here is what i have tried so far
#doesn't work
cmd /c manage-bde.txt>c:\bitlockerstatus.txt
# makes an empty file
$oProcess = Start-Process cmd.exe -ArgumentList "manage-bde>c:\bitlockerstatus.txt" -wait -NoNewWindow -PassThru
$oProcess.HasExited
$oProcess.ExitCode
#doesn't work
[Diagnostics.Process]::Start("cmd.exe","/c manage-bde>c:\bitlockerstatus.txt")
why don't you call the exe directly from powershell using the & operator ?
& manage-bde.exe -status > c:\temp\bl.txt

Execute a batch script as admin from inside powershell

I am trying to schedule a weekly task that takes a backup of some important data (Eventually, I want to run the PowerShell script from Windows task manager). The software provider already has a batch script for this (backup.bat). I have written a powershell script that invokes this batch script. But invoking backupdb from powershell fails throwing a "Permission denied" error message.
I tried the below, which did not work:
start-process $BackupCmd -verb runas -ArgumentList "$Flags `"$BackupFile`""
After looking at several posts on SO and other forums, I was able to find answers for running a powershell script from inside a batch script as admin and not the other way round.
how to run as admin powershell.ps1 file calling in batch file, Run a powershell script in batch file as administrator and How to run a PowerShell script from a batch file
EDIT 1:
1.I run the batch script and the PowerShell script as the same user.
2.I tried elevating the PowerShell using "-verb runas", but did not work. Running the PowerShell script from the same elevated window as the batch script does not work.
3.Pasting the PowerShell script below:
$CurrentDate = get-date -format yyyyMMdd
$BackupStartDate = (get-date).AddDays(-7).ToString("yyyyMMdd")
$BackupDir = "<directory path>"
$BackupFile = $BackupDir + "Backup-" + $BackupStartDate + "-to-" + $CurrentDate + ".txt"
$BackupCmd = "C:\Progra~1\bin\backup"
$Verbose = " -v "
$ArchiveStart = " -S " + $BackupStartDate
$Flags = $Verbose + $ArchiveStart
# Both commands below do not work
start-process $BackupCmd -verb runas -ArgumentList "$Flags `"$BackupFile`""
& $BackupCmd $Flags `"$BackupFile`"
4.Error:
backup.bat : Error writing to the debug log! <type 'exceptions.IOError'> [Errno 13]
Permission denied: 'C:\\Program Files\\tmp\\debug.log'
(2014/06/05 12:42:01.07) [8764] --> Exception encountered. <Unable to load config file!>
Error writing to the debug log! <type 'exceptions.IOError'> [Errno 13] Permission denied:
Thanks.
I have encountered problems using start-process with -verb runas on batch scripts.
Try using start-process on powershell instead, passing your batch file as the first argument:
$CurrentDate = get-date -format yyyyMMdd
$BackupStartDate = (get-date).AddDays(-7).ToString("yyyyMMdd")
$BackupDir = "C:\"
$BackupFile = $BackupDir + "Backup-" + $BackupStartDate + "-to-" + $CurrentDate + ".txt"
$BackupCmd = "C:\Progra~1\bin\backup.bat"
$Verbose = "-v"
$ArchiveStart = "-S $BackupStartDate"
$Flags = "$Verbose $ArchiveStart"
$Args = "$BackupCmd $Flags `"$BackupFile`""
start-process powershell -verb runas -ArgumentList $Args

Executing a simple powershell command on the command line

I try to run a simple powershell command by setting a variable and printing it.
This is what I want to do:
powershell -command "& {$name=\"hi\"; echo $name}"
But it fails with:
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
The invoke operator (&) works fine with commands such as:
powershell -command "& {&echo hi}"
I read about the invoking operator and how to execute commands with -command option and executing scripts with -File option etc. They are working as expected. But my attempts to do the same for setting a variable and printing it as above doesn't work. I suspect -command works with only commands. Any idea how to achieve what I do above?
from a DOS shell this works:
powershell -command "& {$name='hi'; echo $name}"
but also your code works.
From a Powershell console use this:
powershell -command {$name='hi'; echo $name}

Resources