adb pull command in PowerShell works but throws an exception - powershell

When I use adb pull command to copy file from a device to pc in PowerShell, I get this:
PS>.\adb.exe pull '/sdcard/temp/screenshot.png'
.\adb.exe : 6040 KB/s (34027 bytes in 0.005s)
At line:1 char:1
+ .\adb.exe pull '/sdcard/temp/screenshot.png'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (6040 KB/s (34027 bytes in 0.005s):String) [
], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
The file is actually copied though.
The problem is that I want to use it in a loop, so when it throws an exception, script stops playing.
The same command in cmd works perfect.

It is common for exe's to send information down the error stream. I'm sure that is what you are seeing here as well. The "error" (read that like I used finger air quotes) is most likely coming from this line
.\adb.exe : 6040 KB/s (34027 bytes in 0.005s)
If you are familiar with psexec the initial banner of information is on the error stream.
Since that data is on the error stream PowerShell reports it as such. As you have done in comments redirecting the error stream to null is an acceptable was to deal with that. 2>$null
You could also experiment with Start-Process if you don't need the output returned.
Start-Process adb.exe -ArgumentList "pull '/sdcard/temp/screenshot.png'"
You just have to be careful if there actually is an error in either case.

Related

Initialize google chrome by calling a variable

This is what I had in mind:
$chrome= (Start-Process "chrome.exe" "chrome:\\newtab")
$chrome
Pushing enter at the end of the first line loads chrome instantly.
But instead of typing in "Start-Process......." each time, there must be a way to get this simple code assigned to something that is faster to type.
When running the second line of the code it just does nothing.
Any ideas?
By the way, I read this and this questions on this site but I still don't understand how to code this properly. I'm a total newcomer to this.
This is an edit in response to the answer given by #vonPryz
PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\newtab"}
Get-Process : A positional parameter cannot be found that accepts argument 'Launch-Chrome'.
At line:1 char:1
+ PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\n ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
PS C:\>Launch-Chrome
PS : Cannot find a process with the name "C:\>Launch-Chrome". Verify the process name and call the cmdlet again.
At line:1 char:1
+ PS C:\>Launch-Chrome
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\>Launch-Chrome:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
Tried it in powershell 2 and 1 but still doesn't work sadly.
There are quite a few ways to achieve this.
The first one is to create a function that launches Chrome. Like so,
PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\>Launch-Chrome
# A new Chrome window appears!
Save the function in your Powershell profile so that it'll be included on each new session. This might be the simplest solution, so try it first.
Another one is to create a script file that contains the function. Load the script either by dot-sourcing it on a session, or in your Powershell profile. If you need complex scripts, it might be worthwhile to maintain the scripts on separate path and just use profile to load those.
Third one is to create a Powershell module that contains the function. This is the most complex alternative out of the three solutions.
I fixed it.
PS C:\function L-C {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\ L-C
# Chrome window opens

Command in atom terminal giving different results than command prompt

I'm attempting to create a file from my an embedded terminal in my text editor, Atom. The terminal is from the package "platformio-ide-terminal"
When I run a command to create a file in windows command prompt, I am able to create a file without issue, however, when I run the exact same code in my terminal thats embedded in atom it doesn't work.
The command that I'm running is type nul > lecture3.md since I'm trying to create a markdown file. As previously mentioned, this works fine in command prompt, but when I run it in my terminal in atom it seems to interpret the command like I'm trying to enter a folder called "nul", which obviously doesn't exist. The error that's being displayed is as follows:
At line:1 char:1
+ type nul > lecture3.md
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (pathtocurrentfolder\nul:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
I would like to know why this error is happening from my terminal in atom, and not in command prompt, and how I can rectify this issue.
I had a similar exception working on visual code. Most IDE have costume handling exceptions. Probably the IDE is trying to check if nul exist even thou the terminal is passing the command just fine.
I received a similar message on Visual Code for type nul > "yourfile.txt":
> type : Cannot find path 'C:\nul' because it does not exist.
At line:1 char:1
+ type nul > "yourfile.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\nul:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Even that I got the error the command worked and the file was created. You could check the folder to confirm the creation of the file from the terminal inside de IDE with the dir command if your working directory is set on the directory where you created the file.

All commands failing in powershell (4.0.) Even "c:" and "cd" and "dir"

I'm not sure when this started... whenever I open PowerShell, I can't execute even the most basic commands. I always get the following error message:
PS C:\> dir
dir : Cannot find drive. A drive with the name '.' does not exist.
At line:1 char:1
+ dir
+ ~~~
+ CategoryInfo : ObjectNotFound: (.:String) [], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound
---
This had been working fine but now all of a sudden I can't do anything. I've tried removing PowerShell 4 and re-installing... no change. Sorry for the formatting here... It looks organized in my view above but the preview text looks horrid.
I don't remember ever having java installed, but looking at my regular %path% I see that %java_home% is in there, but the value is never used or defined anywhere. Once I removed that from my path, the error above went away.
Strange that it would even be an issue for powershell. You'd think it would just skip past a non-existent directory in my path and move onto the next one when resolving a command.

Why is Powershell ISE showing errors that Powershell console does not show?

I'm running exactly the same script.ps1 file in a Powershell ISE (manually loading the script and pressing F5) and in a Powershell console (executing the script file). They both work but ISE shows errors that the console does not. Why?
The code is:
git push origin master
Write-Host "lastExitCode: $lastExitCode Last command was successful: $?"
This code output this error in the ISE:
git.cmd : Initializing to normal mode
At E:\script.ps1:28 char:4
+ git <<<< push origin master
+ CategoryInfo : NotSpecified: (Initializing to normal mode:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Initializing to normal mode
Everything up-to-date
lastExitCode: 0 Last command was successful: False
And this in the console:
Everything up-to-date
lastExitCode: 0 Last command was successful: True
You can see that the success status is not the same also.
I don't know why they output differently, but the message that we see from git push is coming over stderr. This means that they are both showing errors, although the ISE is making them much louder, and converting it into error objects.
Consider this output from the PowerShell prompt:
PS> git push
Everything up-to-date
PS> git push 2> $null # Redirect stderr to $null
PS> $LastExitCode
1
PS>
and compare it to the ISE:
PS> git push
git : Everything up-to-date
At line:1 char:1
+ git push
+ ~~~~~~~~
+ CategoryInfo : NotSpecified: (Everything up-to-date:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PS> git push 2> $null
PS> $LastExitCode
1
PS>
Except for the extra output from the error object being displayed, the output is the same. The ISE has converted the stderr string to a NativeCommandError object, and it does even display the error message if you look past the red.
as shown in this Stackoverflow answer to prevent git push to print to STDERR the solution is to call the command witn --porcelain option.
then, calling
git push origin master --porcelain
output goes all to STDOUT
So, the example below case have any error , this command -q 2>&1 | %{ "$_" }` will nullifying the result of errors.
A solution and use: git push -q 2>&1 | %{ "$_" }
Hmmm, the only major difference I can think of right off the top of my head is that the ISE uses single-threaded apartment (STA) mode in v2, and the console uses multi-threaded apartment (MTA). Have you tried running powershell.exe with the -STA argument, or powershell_ise.exe with -MTA, and trying the script again?
It looks like the error is coming from the Git command you're trying to run, FWIW.

msbuild calling powershell script under windows 7 gets signing error

We have a powershell build script that calls out to other scripts that is giving me trouble under Windows 7 64 bit. It works fine under XP. I've run set-executionpolicy to RemoteSigned (and also tried it with Bypass and Unrestricted). I can pull the command line from the error message and run it myself and it all runs fine.
Build errors look terrible here; but I'm including them anyway for completeness.
Project "C:\dev7\Source\DashPortal\DashboardGenerator\DashboardGenerator.csproj" (2) is building "C:\dev7\Source\DashPo
rtal\DashboardController\DashboardController.csproj" (15) on node 0 (default targets).
File C:\Users\pmckinney.NA\Documents\WindowsPowerShell\profile.ps1 cannot be lo
aded because the execution of scripts is disabled on this system. Please see "g
et-help about_signing" for more details.
At line:1 char:2
+ . <<<< 'C:\Users\pmckinney.NA\Documents\WindowsPowerShell\profile.ps1'
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
File C:\dev7\Source\DashPortal\DashboardController\..\..\..\Build\createReportI
nstallScript.ps1 cannot be loaded because the execution of scripts is disabled
on this system. Please see "get-help about_signing" for more details.
At line:1 char:87
+ C:\dev7\Source\DashPortal\DashboardController\..\..\..\Build\createReportInst
allScript <<<< -t C:\dev7\Source\DashPortal\DashboardController\..\..\..\Sourc
e\DashPortal\DashboardReports\ReportSetupScriptTemplate.sql -l C:\dev7\Source\D
ashPortal\DashboardController\..\..\..\Source\DashPortal\DashboardReports\Repor
tSetupReportList.xml -c C:\dev7\Source\DashPortal\DashboardController\..\..\..\
Source\DashPortal\Config
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
C:\dev7\Source\DashPortal\DashboardController\DashboardController.csproj(265,5): error MSB3073: The command "powershell
C:\dev7\Source\DashPortal\DashboardController\..\..\..\Build\createReportInstallScript -t C:\dev7\Source\DashPortal\Da
shboardController\..\..\..\Source\DashPortal\DashboardReports\ReportSetupScriptTemplate.sql -l C:\dev7\Source\DashPorta
l\DashboardController\..\..\..\Source\DashPortal\DashboardReports\ReportSetupReportList.xml -c C:\dev7\Source\DashPorta
l\DashboardController\..\..\..\Source\DashPortal\Config" exited with code 1.
Just guessing: You did run Set-ExecutionPolicy in your x64 PowerShell and the build runs the x86 PowerShell?
Maybe try Get-ExcutionPolicy in both shells.
Edit: oops, I see you already enabled scripts. It's possibly something to do with the context under which MSBuild is running. Are you running MSBuild as a local service account of some kind?
Edit2: It's clear the error message is deceptive. There's something else going on and the execution policy error is being thrown but not for the expected reason. Perhaps as a way to isolate, try creating another MSBuild task that runs the same line but with shorter, hardcoded paths instead of the /../../../ bonanza above?