I'm coming from Linux with Bash and Zsh and now I'm working with Windows Powershell. I'm working on a JavaScript NodeJs project and I want to use command execution in Powershell, e.g. in Bash I can write
$(npm bin)/tsc -v
This will call npm bin (which returns something of the form node_modules/.bin), concatenate the result with /tsc and call the whole line, equivalent to
node_modules/.bin/tsc -v
I read about command execution in Powershell but it seems that this only works in strings. The line
$(npm bin)/tsc -v
causes the error
At line:1 char:12
+ $(npm bin)/tsc -v
+ ~
You must provide a value expression following the '/' operator.
At line:1 char:12
+ $(npm bin)/tsc -v
+ ~~~
Unexpected token 'tsc' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
and the line
$(npm bin)\tsc -v
causes the error
At line:1 char:11
+ $(npm bin)\tsc -v
+ ~~~~
Unexpected token '\tsc' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
So I created the string
"$(npm bin)/tsc"
and called it with
& "$(npm bin)/tsc" -v
But this opens a new terminal, calls the command with argument in the new terminal, closes the terminal and I can't see the result. How can I call this string in the same terminal? Is there a typical Powershell way to achieve this?
Related
Amateur computer science student here.
Trying to activate a virtual environment in powershell and I keep
getting the following error both when I start powershell or try to
activate the environment. The environment opens normally in CMD.
I've tried reinstalling to no avail. Not sure what to do so making a first post on StackOverflow.
Many thanks and kind regards,
Daniel
PS C:\Users\User> conda activate MyDjangoEnv
Invoke-Expression : At line:1 char:1342
... iles\PuTTY;"C:\Users\User\AppData\Local\Packages\PythonSoftwareFounda ...
Unexpected token 'C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\loc
al-packages\Python39\Scripts' in expression or statement.
At C:\Users\User\anaconda3\shell\condabin\Conda.psm1:76 char:9
+ Invoke-Expression -Command $activateCommand;
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand
When I run GCloud comannds via windows cmd prompt it works.
But when I run them via powershell it throws an error in red, but the command gets completed successfully.
Eg:
1) Auth
gcloud auth activate-service-account xxx#xxx-xxx.iam.gserviceaccount.com --key-file=C:\xxxxx\cred.json
Error:
python.exe : Activated service account credentials for: [xxxx#xxxx.iam.gserviceaccount.com]
At xxxxxxxxxxxx\gcloud.ps1:117 char:3
+ & "$cloudsdk_python" $run_args_array
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Activated servi...iceaccount.com]:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
2) Upload file to storage bucket
gsutil cp $outputFile gs://bucketxxx
Error:
gsutil : Copying file://N:\out\sqlserver\tag.csv [Content-Type=application/vnd.ms-excel]...
At xxxxxxx\RDS_SQLServer_ExportData_DataWarehouse.ps1:38 char:5
+ gsutil cp $outputFile gs://bucketxxx | out-null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Copying file://...nd.ms-excel]...:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
/ [0 files][ 0.0 B/ 2.8 MiB]
/ [1 files][ 2.8 MiB/ 2.8 MiB]
Operation completed over 1 objects/2.8 MiB.
I was not able comment, hence adding my inputs here.
First : The issue only seems to occur when this is run via Powershell ISE and not when executed via powershell prompt.
Second : This is not as such an error, for example when you see your own error you find the text after Not Specified as "Activated servi..." which means the action is sucessfully performed. Hope this helps to someone else stumbeling over here
python.exe : Activated service account credentials for: [xxxx#xxxx.iam.gserviceaccount.com]
At xxxxxxxxxxxx\gcloud.ps1:117 char:3
+ & "$cloudsdk_python" $run_args_array
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Activated servi...iceaccount.com]:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
I'm trying to execute the following NuGet add command via a PowerShell script:
Invoke-Expression "'C:\Program Files\NuGet\nuget.exe' add '\packageName.1.1.0.nupkg' -Source '\\file-server\My Nuget Packages'"
However, I keep running into the following errors:
Invoke-Expression : At line:1 char:36
+ 'C:\Program Files\NuGet\nuget.exe' add '\packageName.1.1.0.nupkg' -Source '\\fil ...
+ ~~~
Unexpected token 'add' in expression or statement.
At line:3 char:1
+ Invoke-Expression "'C:\Program Files\NuGet\nuget.exe' add '\packageName.1.1.0.nu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Is there a way to call the Nuget add script from within Powershell? If so, how would I do so?
Replace Invoke-Expression with & and remove the double quotes surrounding command line, and change single quotes to double quotes:
& "C:\Program Files\NuGet\nuget.exe" add "\packageName.1.1.0.nupkg" -Source "\\file-server\My Nuget Packages"
Although I suspect that \packageName.1.1.0.nupkg should really be .\packageName.1.1.0.nupkg
I am learning Git Hub. So I am a newbie.
Trying to remove git related files in project directory. I used this line:
rd .git /S/Q
However it gives me this error when I use VS Code:
Remove-Item : A positional parameter cannot be found that accepts argument '/q'.
At line:1 char:1
+ rd /s /q .git
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
and when I use CMD - command promt it gives me this error:
.git\refs\remotes - The directory is not empty.
What is going on.
This is where I get my information about this:
From stackoverflow
From YouTube
I am using Windows 10 btw.
EDIT:
I tried:
rd /S /Q .git
and I get error:
Remove-Item : A positional parameter cannot be found that accepts argument '/Q'.
At line:1 char:1
+ rd /S /Q .git
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
Tried:
rd /S/Q .git
Error:
Remove-Item : A positional parameter cannot be found that accepts argument '.git'.
At line:1 char:1
+ rd /S/Q .git
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
However I tried in command promt:
rd /S /Q .git
And I get :
.git\refs\remotes - The directory is not empty.
I try it second time right after that and it works...
So what am I missing here?
EDIT 2:
All of these comands work in command promt but only if I type them for the second time. Including the command - rd .git /S/Q.
The first time I get :
.git\refs\remotes - The directory is not empty.
and if I type it for the second time it works.
In VS Code it is not working no matter how many times I type them.
I try to start powershell as an external tool.
I want it to be run in the console windows of eclipse.
But it starts with empty console, and when I type and press return, I get:
Missing expression after unary opearator ‘-’.
At line:1 char:2
+ – <<<< Command Set-Location C:\Users\XXX
+ CategoryInfo : ParserError: (-:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
You got misplaced quotes in the Arguments section.
-NoExit -"Command Set-Location ${project_loc}"
Try moving the quote after Command switch like so,
-NoExit -Command "Set-Location ${project_loc}"