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

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'

Related

Schedule PowerShell commands

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?

Error when running powrshell at prompt with TaskScheduler info - 'is not recognized as the name of a cmdlet, function, script file, or operable ..'

I'm debugging why my script isn't running right in the TaskScheduler by running it at the command prompt in powershell ISE on the server like it's set up in TaskScheduler:
PS Microsoft.PowerShell.Core\FileSystem::\\w5server\ksupport\C_Adjust> c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -noninteractive -ExecutionPolicy bypass -Command "& .\\w5server\ksupport\C_Adjust\RunScan_MoveFiles_Admin.ps1"
I get this error:
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe : & : The term '.\\w5server\ksupport\C_Adjust\RunScan_MoveFil
At line:1 char:1
+ c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -noninterac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (& : The term '....Scan_MoveFil:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
es_Admin.ps1' 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:3
+ &
.\\w5server\ksupport\C_Adjust\RunScan_MoveFiles_Admin.ps1
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\\w5server\k...Files_Admin.p
s1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I'm not sure why it's splitting the full command line info like that in the error message either; hopefully it's not part of the problem.
I tried removing the '&' before the .\w5server...scriptname.ps1 but got the same error message. I've never given a full path with this type of path before, but that's where the script is. Last time I did this it was in c:\Scripts\B... on that server.
This server is Windows Server 2012 R2. Previously, my server I was working on was 2008 R2, I'm not sure if that matters or not. Otherwise my syntax is the same, other than the script name and full path.
I saw this link is not recognized as the name of a cmdlet, but I think it's spelled correctly.
I was playing around with what I was using to run it at the command line, and this is working:
PS Microsoft.PowerShell.Core\FileSystem::\\w5server\ksupport\C_Adjust> c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -noninteractive -ExecutionPolicy bypass -Command \\w5server\ksupport\C_Adjust\RunScan_MoveFiles_Admin.ps1
So I got rid of the double quotes, '.' and '&' for the script location.

Start-Process powershell.exe with an argument

How do I start a new powershell instance and tell that instance to execute a certain command?
What I've tried:
(Assuming vim.exe and file.txt is in cwd)
Start-Process powershell.exe .\vim.exe .\file.txt
vim.exe is the parameter to powershell.exe
file.txt is the parameter to vim.exe
Error says:
Start-Process : A positional parameter cannot be found that accepts argument
'.\file.txt'.At line:1 char:1
+ Start-Process powershell.exe .\vim.exe .\file.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
I've also tried powershell.exe -ArgumentList {.\vim.exe .\file.txt} and no dice.
The Argument list is an array... say start-process doesn't know what to do with .\file.txt
Here is how I would write it
Start-Process powershell.exe -ArgumentList #('.\vim.exe', '.\file.txt')
The following will also work, but I like the above where you explicitly say you want an array
Start-Process powershell.exe -ArgumentList '.\vim.exe', '.\file.txt'

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}

Escape special chars in powershell commands

I am new to powershell, but I am facing a very basic problem. When I am running the following command powershell complains. It seems to have issues with the special chars: [*, =, &, <, >]. Any ideas how do I escape them ? This is powershell version 2. [I am using winexe to run the powershell command from a linux box. If I copy paste the ps command it seems to work fine, but remotely running it cause powershell to complain.]
winexe "cmd /c echo . | powershell Set-ExecutionPolicy bypass -Force -Scope CurrentUser;C:\test.ps1 -name 'B*=&<+>%N' -extra_logging '0' "
dos charset 'CP850' unavailable - using ASCII The string starting: At line:1 char:106 + Set-ExecutionPolicy bypass -Force -Scope CurrentUser;C:\test.ps1 -lun <<<< 'B*= is missing the terminator: '. At line:1 char:110 + Set-ExecutionPolicy bypass -Force -Scope CurrentUser;C:\test.ps1 -lun 'B*= <<<< + CategoryInfo : ParserError: (B*=:String) [], ParentContainsErro rRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
The system cannot find the file specified. The system cannot find the file specified.
The problem isn't powershell but the regular command shell. Making a reasonable assumption about what winexe does, the relevant part of the command is
cmd /c echo . | powershell Set-ExecutionPolicy bypass -Force -Scope CurrentUser;C:\test.ps1 -name 'B*=&<+>%N' -extra_logging '0'
which contains some special characters interpreted by the command shell. Content in single quotes is not considered to be quoted, so you'll need to explicitly quote them. Just to make life difficult, because you're using piping, the characters are processed twice so you'll need to double-quote:
cmd /c echo . | powershell Set-ExecutionPolicy bypass -Force -Scope CurrentUser;C:\test.ps1 -name 'B*=^^^&^^^<+^^^>^%N' -extra_logging '0'
The caret makes the command shell take the following character literally.
Or, if it so happens winexe passes the command it is given to the command shell rather than executing it directly, you might need to triple-quote, i.e., seven carets before each special character.
I could be wrong, but I believe it is the backwards apostrophe or the grave symbol, i.e. `

Resources