batch file - Running .cmd/.bat script in Powershell -


i'm trying write , execute .cmd script in powershell. code have is:

$script = @' @echo off setlocal  call here '@  invoke-expression -command: $script 

this based off this link explains here string in powershell. it's @ bottom of link. here's the related msdn.

here's another related link trying same thing.

i keep getting error has including '@' operator within string:

invoke-expression : @ line:1 char:7 + @echo off +       ~~~ unexpected token 'off' in expression or statement. @ line:1 char:1 + @echo off + ~~~~~ splatting operator '@' cannot used reference variables in expression. '@echo' can used argument command. reference variables in expression use '$echo'. 

i've tried escaping '@' symbol, , plethora of other things. i'd know why seemed work them in third link, throws error in case.

edit: writing .bat file running bat file resulted in same error:

$batchfilecontent = @' @echo off c:\windows\system32\ntbackup.exe backup "c:\documents , settings\administrator\local settings\application data\microsoft\windows nt\ntbackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 @ 09:36" /d "set created 06/09/2013 @ 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\exchange backups$\1file.bkf" '@  $batchfilecontent | out-file -literalpath:"$env:temp\backup.cmd" -force  invoke-expression -command:"$env:temp\backup.cmd"  remove-item -literalpath:"$env:temp\backup.cmd" -force 

as bill stewart pointed out, should write content of .cmd script in powershell.

edit:

$script = @' cmd.exe /c "@echo off" cmd.exe /c "setlocal"  cmd.exe /c "call here" '@  invoke-expression -command: $script 

seems work.

this happens because invoke-expression interprets string powershell. powershell allows run shell commands, interprets things powershell first. @ character splatting operator in powershell.

you should save commands in batch file, , execute that.

or can execute single line commands shelling out cmd.exe:

invoke-expression "cmd.exe /c @echo something" 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -