The following VBScript gives the error:
failing : error invalid character line 5 column 54
My VBScript:
dim myobject :
set myobject = createobject ( "wscript.shell" ) :
powershellcommand = "powershell.exe -Executionpolicy bypass -noprofile -windowstyle hidden -command Set-content -value (new-object System.net.webclient).downloaddata( https://website.com/folder/download.exe ) -encoding byte -Path $env:appdata\download.exe ; start $env:appdata\download.exe " :
myobject.run " & myobject.expandenvironmentstrings( "%systemroot%" ) & "\SYSTEM32\windowspowerShell\v1.0\powershell.exe" & " & ' ' & " & powershellcommand & " , 0 : set myobject = NOTHING
How do I fix it?
With regard to .run method: essentially, the string value indicating the command line you want to run should appear exactly as it would if you typed it at the command prompt, e.g. something like
powershell.exe -Executionpolicy bypass -noprofile -windowstyle hidden -command "Set-Content -value (new-object System.net.webclient).downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) -encoding byte -Path $env:appdata\download.exe; Start-Process $env:appdata\download.exe"
The above command runs as expected (unfailing in Windows 8.1/64, PSVersion 5.1.14409.1012). However, if you want to supply full path to powershell executable, then it could look as follows:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Executionpolicy bypass -noprofile -windowstyle hidden -command "Set-Content -value (new-object System.net.webclient).downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) -encoding byte -Path $env:appdata\download.exe; Start-Process $env:appdata\download.exe"
You need to properly build such string value in VBScript e.g. as in the following code snippet for the latter:
Option Explicit
Dim WshShell, powershellcommand, objFSO, sPSexePath
Set WshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
sPSexePath = objFSO.BuildPath( WshShell.expandenvironmentstrings( "%systemroot%" ) _
, "System32\WindowsPowerShell\v1.0\powershell.exe" )
powershellcommand = """" & sPSexePath & """ -Executionpolicy bypass -noprofile " _
& " -windowstyle hidden -command ""Set-Content -value " _
& " (new-object System.net.webclient)" _
& ".downloaddata( 'http://pspad.poradna.net/release/pspad462_setup.exe' ) " _
& " -encoding byte -Path $env:appdata\download.exe; " _
& " Start-Process $env:appdata\download.exe"""
WshShell.Run powershellcommand, 0
Note that in VBScript, a sequence of literal characters, enclosed in double quotes ("), is recognized as a string. A single quotation mark (') is allowed within a string. To insert a double quotation mark (") into a string, it should be duplicated as "".
Related
I try to install .msu package using Poweshell. My code:
$Args = #(('"' + $Path_Temp + '\' + $Hotfix[1] + '.msu' + '"'), '/norestart')
echo $Args
Start-Process -FilePath ($Env:SystemRoot + '\System32\wusa.exe') -ArgumentList $Args -Verb RunAs -Wait
echo outputs this:
"C:\Users\MyUser\AppData\Local\Temp\Windows6.1-KB2758857-x64.msu"
/norestart
All looks good, but in the end wusa warns me about incorrect usage. It works well when I pass only the first argument (which is the path to .msu file) to ArgumentList. My code does not work when I pass multiple arguments, why? I use Powershell v5.1.14409.1005
I am new to Powershell and trying to run a PS script using another PS script to run it as admin in Jenkins, the script gives its desired output but it also gives the error in the output.
Powershell : Start-Process : A positional parameter cannot be found
that accepts argument At line:4 char:1
+ Powershell -Command "Start-Process powershell ""cd "F:\RiskLink\RiskL ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Start-Process :...cepts argument :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Code:
Set-Location -Path F:\abd\abc
$path = ".\xyz.ps1"
Powershell -Command "Start-Process powershell ""cd "F:\abd\abc"; & $path"" -Verb RunAs"
Long ago, I have created a simple command line parser (scripts cliparser.ps1 and cli parser.ps1):
PS D:\PShell> Get-Content ".\cliparser.ps1"
if ( $args -ne $null ) { $aLen=$args.Count } else { $aLen=0 }
"$(Split-Path -Path $PSCommandPath -Leaf): $aLen parameters"
for ( $i = 0; $i -lt $aLen ; $i++ ) { "{0}. [{1}]" -f $i, $args[$i] }
Used instead of yours xyz.ps1, it shows that something must go wrong not only with Start-Process arguments but with quoting as well (shows supplied values -Verb and RunAs). Fixed with some little effort… Improved for space(s) in paths and file names, see the following script:
$cd = (Get-Location -PSProvider Filesystem).Path
$path = ".\cliparser.ps1"
'... original (known error)'
Write-Host Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
Powershell -Command "Start-Process powershell ""pushd "$cd"; & $path"" -Verb RunAs"
pause
'... fixed'
Write-Host Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
$path = ".\cli parser.ps1"
Powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd `'`'$cd`'`'; & `'`'$path`'`';' -Verb RunAs"
'done'
Note that PShell's parameters -NoProfile and -NoExit are here merely for debugging purposes.
Result:
PS D:\PShell> D:\PShell\SO\53638416.ps1
... original (known error)
Powershell -Command Start-Process powershell "pushd D:\PShell; & .\cliparser.ps1" -Verb RunAs
Start-Process : A positional parameter cannot be found that accepts argument 'D:\PShell'.
At line:1 char:1
+ Start-Process powershell pushd D:\PShell; & .\cliparser.ps1 -Verb Ru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.Start
ProcessCommand
cliparser.ps1: 2 parameters
0. [-Verb]
1. [RunAs]
Press Enter to continue...:
... fixed
Powershell -NoProfile -Command Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'';' -Verb RunAs
done
PS D:\PShell>
Called (elevated) Powershell window looks as follows (Pop-Location used to ensure that popd ran well):
To run from a cmd prompt and pass arguments to the called script:
powershell -NoProfile -Command "Start-Process powershell -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"
Works for pwsh instead of powershell as well:
pwsh -NoProfile -Command "Start-Process pwsh -ArgumentList '-NoProfile', '-noexit', '-command', 'pushd ''D:\PShell''; & ''.\cliparser.ps1'' a b c;' -Verb RunAs"
I am trying to execute an installation via start-process but i want it to execute as a job so i can execute a few other statements while also being able to check the status of the installation as it runs in the background.
Here is a section of the code i am trying to execute -
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"" + "$SetupPath\setup.exe" + "`""
$command = "{$SetupProcess=" + "Start-process" + " " + `
"$Setup" + " "+ "-ArgumentList" + " " + `
"/config config.xml" + " " + "-Wait -PassThru" + "}"
# The above command equals-> {$SetupProcess=Start-process "C:\Test Installs\setup.exe" -ArgumentList /config config.xml -Wait -PassThru}
#Change string to Scriptblock
$ScriptBlock = [Scriptblock]::Create($command)
$job1 = Start-Job -ScriptBlock $ScriptBlock -Name "SetupJob"
When I run this and try to get back the result via Receieve-Job i only get back the command string I passed via script block. It appears the Start-process command is not executing. Is there something i am missing?
Get rid of the {} in your $command definition. [ScriptBlock]::Create() expects some text in which it will wrap in a scriptblock. You can also simplify this:
$SetupPath = "C:\Test Installs"
# Enclose the path to setup.exe in quotes
$Setup = "`"$SetupPath\setup.exe`""
$command = "SetupProcess = Start-process `"$Setup`" -ArgumentList `"/config config.xml`" -Wait -PassThru"
I get this error:
Image
When i try to run this code:
strComputer = "server01"
strCommand = "powershell.exe -NoLogo -Command Start-Service -InputObject $(Get-Service -Computer " & strComputer & " -Name " & strService & ")"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objExec = WshShell.Run(strCommand,0,True)
I want to run strCommand in an hidden window. This works the code ran through and do what it should do but this error occure and this is really bad.
.Run() - as opposed to .Exec() - returns an error code (not an object). So you must not use
Set objExec = WshShell.Run(strCommand,0,True)
but
nErrorCode = WshShell.Run(strCommand,0,True)
You need to add a double quote before the command and at its end:
strCommand = "powershell.exe -NoExit -NoLogo -Command ""Start-Service -PassThru -InputObject (Get-Service -Computer " & strComputer & " -Name " & strService & ")"""
I have a command that I have build and stored in a variable in PowerShell. This command works if I do a Write-Host and copy and paste into a standard cmd.exe window.
How do I execute this command from inside my script?
I have tried several combination of Invoke-Command or Invoke-Expression with no luck.
This is how I built the variable:
$cmd1 = $arcprg + $arcdir + "\" + $site1 + "-" + $hst + "-" + $yesterday + ".zip " + $logpath1 + "u_ex" + $yesterday + ".log"
This is what the variable looks like if it is printed to the screen:
7z.exe a -tzip c:\arc_logs\site-host-at-web1-100827.zip c:\inetpub\logs\logfiles\w3svc1\u_ex100827.log
Here is yet another way without Invoke-Expression but with two variables
(command:string and parameters:array). It works fine for me. Assume
7z.exe is in the system path.
$cmd = '7z.exe'
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& $cmd $prm
If the command is known (7z.exe) and only parameters are variable then this will do
$prm = 'a', '-tzip', 'c:\temp\with space\test1.zip', 'C:\TEMP\with space\changelog'
& 7z.exe $prm
BTW, Invoke-Expression with one parameter works for me, too, e.g. this works
$cmd = '& 7z.exe a -tzip "c:\temp\with space\test2.zip" "C:\TEMP\with space\changelog"'
Invoke-Expression $cmd
P.S. I usually prefer the way with a parameter array because it is easier to
compose programmatically than to build an expression for Invoke-Expression.
Try invoking your command with Invoke-Expression:
Invoke-Expression $cmd1
Here is a working example on my machine:
$cmd = "& 'C:\Program Files\7-zip\7z.exe' a -tzip c:\temp\test.zip c:\temp\test.txt"
Invoke-Expression $cmd
iex is an alias for Invoke-Expression so you could do:
iex $cmd1
For a full list :
Visit https://ss64.com/ps/ for more Powershell stuff.
Good Luck...