Powershell output redirection not working - powershell

I'm trying to call docker from a powershell script, but for some reason powershell prints a NativeCommandError everytime I do, even though I'm redirecting output:
PS> docker build -f .\Proj\Dockerfile . 2>&1
docker : #2 [internal] load .dockerignore
At line:1 char:1
+ docker build -f .\Proj\Dockerfile . 2>&1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (#2 [internal] load .dockerignore:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Why redirection is not working? Running powershell 5.1.

You are redirecting the error stream to the success stream (2>&1), but the success stream will still be printed to console, and error records printed as errors. To actually discard the streams, redirect to null:
docker build -f .\Proj\Dockerfile . 2>&1 | Out-Null
# or
docker build -f .\Proj\Dockerfile . 2>&1 > $null

Related

why am I getting this error in windows powershell?

I was trying these power forensics command from this website the 1st two commands worked perfectly while the next 3 commands gave errors, I am sharing one of the error below. Please tell me the solution to get rid of this error.
Command - PS C:> Get-ForensicFileRecord -Path C:\Windows\system32\cmd.exe
Error:
Get-ForensicFileRecord : Path C:\Windows\system32\cmd.exe not found.
At line:1 char:1
Get-ForensicFileRecord -Path C:\Windows\system32\cmd.exe
+ CategoryInfo : NotSpecified: (:) [Get-ForensicFileRecord], Exception
+ FullyQualifiedErrorId : System.Exception,PowerForensics.Cmdlets.GetFileRecordCommand
The link which I followed for this tutorial:
PowerForensics - PowerShell Digital Forensics | Get-ForensicFileRecord

Execute string in same terminal

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?

Windows cmdlet over powershell passes but gives access denied over winrm for Cluster aware updating

I am executing Invoke-CACURun for windows cluster aware updating on domain controller as an administrator. While that works on powershell, it is failing over ansible with ntlm transport and same credentials.
The command executed
ansible domain_controller -i ansible_hosts -m win_shell -a 'whoami;Invoke-CauRun -ClusterName "xyzWFC
The error I get is
Invoke-CauRun : Could not open cluster "xyzWFC": (Win32Exception) Access is denied
At line:1 char:72
+ ... .UTF8Encoding $false; whoami;Invoke-CauRun -ClusterName "xyzWFC";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Invoke-CauRun], CauAccessDeniedException
+ FullyQualifiedErrorId : OpenClusterAccessDenied,Microsoft.ClusterAwareUpdating.Commands.InvokeCauRunCommandnon-zero return code
Any help on this?

Parameter interups docker build

I've created a dockerfile and now I want to build it, but I need to run a powershell command with a parameter inside. Running the command outside the docker build, then it works fine. Unfortionatly not when I run it inside the docker build.
Dockerfile:
FROM microsoft/nanoserver COPY wrapperfiles /wrapperfiles
RUN powershell start-process -filepath "C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf"
EXPOSE 80
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
Now when I want to build it, then it comes with the following error:
Sending build context to Docker daemon 1.664GB Step 1/5 : FROM microsoft/nanoserver ---> e2c314f76df6 Step 2/5 : COPY wrapperfiles /wrapperfiles ---> Using cache ---> 6047da8320f0 Step 3/5 : RUN powershell start-process -filepath "C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf" ---> Running in 0d6b47577b35 Start-Process : A parameter cannot be found that matches parameter name 's'. At line:1 char:75
+ ... filepath C:\wrapperfiles\wrapper.exe -s C:\wrapper ...
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterB indingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm ands.StartProcessCommand
The command 'cmd /S /C powershell start-process -filepath "C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf"' returned a non-zero code: 1
How can I get the -s parameter included in the filepath command?
How can I get the -s parameter included in the filepath command?
-s cannot be part of a filepath, unless you have a file literally called C:\wrapperfiles\wrapper.exe -s C:\wrapperfiles\tomcat-wrapper-default.conf.
Probably you want to include -s and C:\wrapperfiles\tomcat-wrapper-default.conf as arguments, so try:
RUN powershell start-process -filepath "C:\wrapperfiles\wrapper.exe" -ArgumentList "-s","C:\wrapperfiles\tomcat-wrapper-default.conf"

Install .cer certificate in "Trusted Devices" folder via PowerShell

I need to install .cer certificate into "Trusted Devices" folder because I have PowerShell script which automatically installs software and only thing that is blocks the hole script is windows which asks about trustworthy of the dongle driver.
I use this cmdlet inside Vagrant
vagrant.exe powershell -c "Import-Certificate -Filepath C:\vagrant\Microcosm.cer -CertStoreLocation Cert:\Local Computer\TrustedDevices"
Here is error message output
vagrant.exe : The following WinRM command responded with a non-zero exit status.
At line:1 char:2
+ vagrant.exe powershell -c "Import-Certificate -Filepath C:\vagrant\Microcosm.ce ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The following W...ro exit status.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Vagrant assumes that this means the command failed!
Import-Certificate -Filepath C:\vagrant\Microcosm.cer -CertStoreLocation Cert:\Local Computer\TrustedDevices
Stdout from the command:
Stderr from the command:
#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">Import-Certificate : A positional parameter cannot be found that accepts argument 'Computer\Trust
edDevices'._x000D__x000A_</S><S S="Error">At line:1 char:40_x000D__x000A_</S><S S="Error">+ ... lyContinue';Import-Certificate -Filepath C:\vagrant\Microcosm.cer -Ce ..._x000D__x000A_</S><S
S="Error">+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_x000D__x000A_</S><S S="Error"> + CategoryInfo : InvalidArgument: (:) [Import-Certificate], P
arameterBindingException_x000D__x000A_</S><S S="Error"> + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.CertificateServices.Commands.ImportCertificateComm _x000D__x000A_</
S><S S="Error"> and_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>
Cert:\Local Computer\TrustedDevices has a space in it. Try this with escaped quotes around your path:
vagrant.exe powershell -c "Import-Certificate -Filepath C:\vagrant\Microcosm.cer -CertStoreLocation `"Cert:\Local Computer\TrustedDevices`""