For some reason, I can't seem to get output and redirection to work over ssh from linux to Windows. I have a powershell script that only outputs a message on each stream.
From the powershell terminal this looks like:
$ ssh me#windows.box.com
PS C:\Users\me> C:/"Program Files"/App3/bin/script.ps1 2> $null
This is output
This is to host
WARNING: This is a warning!
PS C:\Users\me> C:/"Program Files"/App3/bin/script.ps1 *>&1 2> $null
This is output
This is to host
WARNING: This is a warning!
C:\"Program Files"\App3\bin\script.ps1 : This is an error
At line:1 char:1
+ C:/"Program Files"/App3/bin/script.ps1 *>&1 2> $null
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,script.ps1
But for some reason, from ssh commands, I can't get the other streams redirected to stdout:
$ ssh me#windows.box.com "C:/\\\"Program Files\\\"/App3/bin/script.ps1" 2>/dev/null
This is output
This is to host
WARNING: This is a warning!
$ ssh me#windows.box.com "C:/\\\"Program Files\\\"/App3/bin/script.ps1 *>&1" 2>/dev/null
This is output
This is to host
WARNING: This is a warning!
And to clarify that this seems to be a script specific issue:
$ ssh me#windows.box.com "Write-Host \\\"This is to host\\\""
This is to host
$ ssh me#windows.box.com "Write-Output \\\"This is output\\\""
This is output
$ ssh me#windows.box.com "Write-Warning \\\"This is a warning\!\\\""
WARNING: This is a warning\!
$ ssh me#windows.box.com "Write-Error \\\"This is an error\\\""
Write-Error "This is an error" : This is an error
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
Any ideas on how to get the script output redirected?
I did run into another issue with getting output when the script is run is different directories, but that's a battle for another day, another Stack Overflow post. I'm running RHEL, and the script content is:
Write-Output "This is output"
Write-Host "This is to host"
Write-Verbose "This message is definitely more verbose"
Write-Debug "This is marked debug"
Write-Warning "This is a warning!"
Write-Error "This is an error"
I can't quickly recreate your environment (linux host / windows remote). I tried at least to imitate cross platform environment ( powershell on linux VM, ssh client on Windows). Here is some notes that might be helpful, at least if you are planning get those streams on the file
Unlike you, I got same output on my terminal when running this
ssh mike#Ubuntu "/snap/bin/pwsh ./test.ps1 *>&1" (on local)
pwsh ./test.ps1 *>&1 (on remote)
Although I got similar issue when trying to redirect output to the file over ssh (but it works if called from powershell session)
ssh mike#Ubuntu "/snap/bin/pwsh ./test.ps1 *>&1 test.log"
#adds only original success to the file, and prints error on terminal
So it looks like stream to stream redirection is not working if invoked through the ssh. Although somehow stream to file seemed to work
ssh mike#Ubuntu "/snap/bin/pwsh ./test.ps1 >test.log 2>>test.log"
ssh mike#Ubuntu "/snap/bin/pwsh ./test.ps1" &> test.log #redirection works again on local
So might be some weirdo bug of ssh/powershell interaction. You might try to use standard ps remoting protocol vs ssh (like Invoke-Command {echo 'hi'} -ComputerName YourMachine -Credentials $creds )
Related
Hope you can help me out here. I've been playing around with some SSH authentication. I have no idea why this is happening, but now when I run a simple ssh-keygen command I get a response saying Access is denied. in addition to a pop-up that reads "This app can't run on your PC. To find a version for your PC, check with the software publisher".
I then tried running the ssh-keygen command in Powershell (as admin and as regular user) and got the following:
Program 'ssh-keygen.exe' failed to run: The specified executable is not a valid application for this OS platform.At
line:1 char:1
+ ssh-keygen
+ ~~~~~~~~~~.
At line:1 char:1
+ ssh-keygen
+ ~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
However, using ssh -V shows that it is installed:
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
I can still SSH fine into other devices, if that's of any relevant.
Any help would be very much appreciated here!
I have an installed Windows Server 2016 Technical Preview 4 on VMware
Warkstation.
It has 2 GB RAM and 60 GB disk space.
I try to install Windows Server Host for Docker according to official Microsoft instruction.
Instruction in general in the picture
I don't need a Hyper-V Containers technology so for this guide i don't have to make steps marked with a one asterisk '*'
So we have six steps that we have to do for install Windows Server Host and Docker:
Install the Container Feature
Create Virtual Switch
Configure NAT
Configure MAC Address Spoofing
Install Container OS Images
Install Docker **
I have a problem in step two: Creat Virtual Switch while typing next cmdlet in PowerShell:
PS C:\> New-VMSwitch -Name "Virtual Switch" -SwitchType NAT -NATSubnetAddress 172.16.0.0/12
New-VMSwitch : Failed while adding virtual Ethernet switch connections.
Internal miniport create failed, name = '8A407781-1BF5-4BB0-8538-35CFF056C598', friendly name = 'vEthernet (Virtual Switch)', MAC = 'DYNAMIC': One or more arguments are invalid
(0x80070057).
At line:1 char:1
+ New-VMSwitch -Name "Virtual Switch" -SwitchType NAT -NATSubnetAddress ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-VMSwitch], VirtualizationException
+ FullyQualifiedErrorId : InvalidParameter,Microsoft.HyperV.PowerShell.Commands.NewVMSwitch
What should I do in this case?
I was able to install docker using Microsoft guide about auto-install instead of using manual guide:
# Auto-install instruction.
PS C:\> powershell.exe
PS C:\> start-process powershell -Verb runas
PS C:\> wget -uri https://aka.ms/tp4/Install-ContainerHost -OutFile C:\Install-ContainerHost.ps1
PS C:\> C:\Install-ContainerHost.ps1 -HyperV
# Wrong!
But there is an error in the last line in key -HyperV.
We need to ingore it, so the last line looks like:
PS C:\> powershell.exe
PS C:\> start-process powershell -Verb runas
PS C:\> wget -uri https://aka.ms/tp4/Install-ContainerHost -OutFile C:\Install-ContainerHost.ps1
PS C:\> C:\Install-ContainerHost.ps1
# Currect!
Using correct instruction i was able to install docker.
I had the same error on a Nano Server the first time I ran:
PS C:\> New-VMSwitch -Name "Virtual Switch" -SwitchType NAT -NATSubnetAddress 172.16.0.0/12
SwitchType NAT is definitely correct because the command doesn't accept the NATSubnetAddress parameter for anything other then a NAT switchtype.
Run it a second time and then it worked on my Nano Server.
I was testing some nodejs server code and wanted to test the urls from windows power shell using the curl command. Some things to note here:
1. I have mingw and git bash installed in my system, and curl works fine from normal command prompt and git bash prompt.
2. Additionally I have downloaded the latest version of cURL from curl official web site and added the path of bin directory to the system's PATH variable.
But it seems like it is no good for power shell. However, powershell still recognizes cURL command regardless of additional cURL program or git bash present in the system or not. But it does not work as I expected it to be, for example I have tried the following command and:
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.
PS C:\Users\Zobayer Hasan> curl -X GET -i localhost:8080
Invoke-WebRequest : A parameter cannot be found that matches parameter name 'X'.
At line:1 char:6
+ curl -X GET -i localhost:8080
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\Users\Zobayer Hasan> curl GET -i localhost:8080
curl : Cannot find drive. A drive with the name 'localhost' does not exist.
At line:1 char:1
+ curl GET -i localhost:8080
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (localhost:String) [Invoke-WebRequest], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\Users\Zobayer Hasan> curl GET -i http://localhost:8080
curl : Cannot find drive. A drive with the name 'http' does not exist.
At line:1 char:1
+ curl GET -i http://localhost:8080
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (http:String) [Invoke-WebRequest], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
PS C:\Users\Zobayer Hasan>
But when I try it in normal command prompt, it works fine:
C:\Users\Zobayer Hasan>curl -X GET -i localhost:8080
HTTP/1.1 200 OK
Date: Wed, 31 Dec 2014 09:35:31 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Server connection was called
C:\Users\Zobayer Hasan>
I also get the same output when I try it from git bash.
My question here is what do I need to do if I want everything to run smooth in windows power shell as well? I have done a little bit of searching on the net, and could not find much helpful resources. Also I have not any prior experience with windows powershell. Usually use linux as development environment.
It looks like you already have curl as an alias to Ivoke-WebRequest:
Invoke-WebRequest : A parameter cannot be found that matches parameter
name 'X'.
Try running remove-item alias:\curl and see if you now get the right curl being invoked. Alternatively, try by specifying the absolute path, i.e. c:\curl\curl.exe ....
Since I updated to PowerShell 4.0, an Alias was created to point to Invoke-WebRequest with the name of curl, so what I've done is I added my own alias to the curl.exe executable I want to use instead:
set-alias mcurl "C:\cygwin\bin\curl.exe"
Powershell 4.0
Get-Alias curl
CommandType Name ModuleName
----------- ---- ----------
Alias curl -> Invoke-WebRequest
My new Alias:
Get-Alias mcurl
CommandType Name ModuleName
----------- ---- ----------
Alias mcurl -> curl.exe
I have written a powershell script which creates a powershell script or a batch script depending on the remote host on which this script should be started remotely (either via powershell invoke-command or psexec).
The script creates either a powershell or a batch script, because not all hosts on which these created scripts should be run support WinRM (Windows Remote Management).
The created scripts include some psexec lines which executes a command on another remote host. The created batch script works as expected, but from the created powershell script i get the following errors from the psexec calls.
NotSpecified: (:String) [], RemoteException
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : <HOSTNAME REPLACED>
PsExec v2.1 - Execute processes remotely
Copyright (C) 2001-2013 Mark Russinovich
Sysinternals - www.sysinternals.com
The handle is invalid.
Access is denied.
Connecting to 10.XXX.XXX.127...Couldn't access 10.XXX.XXX.127:
Starting PSEXESVC service on 10.XXX.XXX.127...Could not start PSEXESVC service on 10.XXX.XXX.127:
Connecting to 10.XXX.XXX.127...Starting PSEXESVC service on 10.XXX.XXX.127...
Here is a small overview of what is done:
Powershell script on deployment host creates the rollout script (ps1 or batch) depending on the destination host
Powershell script on deployment host starts the created rollout script on the destination host
Powershell will be started via Invoke-Command -ComputerName <destination host> -FilePath <path to created script> -Authentication default
Batch will be started via & psexec.exe \\<destination host> -n 60 -accepteula -c -f <path to created script>
Created rollout script on the destination host executes a psexec command to start a service on a third host (this fails only if the rollout script is powershell)
This is the psexec command which fails if executed via a remotely started powershell script.
& psexec.exe \\<destination host> -n 60 -accepteula -u <user> -p <password> net stop <servicename>
The same command psexec.exe \\<destination host> -n 60 -accepteula -u <user> -p <password> net stop <servicename> executed via a remotely started batch script works without problems.
Update #1
Also if i connect to the destination host with rdp and start a powershell shell and paste the command it works without issues. The problem only occurs if started remotely.
I already tried the following variants of starting psexec, all without luck!
& psexec.exe \\<destination host> -n 60 -accepteula -s -u <user> -p <password> net stop <servicename>
Start-Process cmd.exe -Credential "<domain>\<user>" -WorkingDirectory $env:systemdrive -ArgumentList "/C psexec.exe \\<destination host> -n 60 -accepteula -u <user> -p <password> net stop <servicename>"
Start-Process powershell.exe -WorkingDirectory $env:systemdrive -Verb Runas -ArgumentList "cmd.exe /C psexec.exe \\<destination host> -n 60 -accepteula -u <user> -p <password> net stop <servicename>"
can you try to replace your psexec command, and use cmd.exe like thisn also add -s to psexec so it will run as system user. My testing command :
icm -cn computer1 -ScriptBlock{ cmd.exe "/c psexec -s -u <login> -p <password> /accepteula /n 10 \\computer2 net start audiosrv"}
You are running into the Second Hop problem:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/14/enable-powershell-quot-second-hop-quot-functionality-with-credssp.aspx
The answer is to enable the appropriate relationship between the two computers involved (see previous link or just search google for 'powershell' and 'second hop') and to add to Invoke-Command:
-Authentication Credssp -Credential
What worked for me was:
Start-Process -FilePath .\PsExec.exe -ArgumentList "/acceptEula /s \$Computer program.exe /arg"
I'm running into a weird issue here.
I have a Windows server setup to allow SSH access via Cygwin.
The idea behind this is to allow me to remotely execute Powershell commands from one of my Linux boxes. Now, in the hopes of being secure while still allowing for automation, I did your typical ssh-copy-id to my designated account on that box. I then tried logging in via SSH keys... success! Now here's the weird part. I run my Powershell command:
powershell 'C:/cygwin64/scripts/add_email_address_to_distribution_list.ps1'
And get the following error message:
$ powershell
'C:/cygwin64/scripts/add_email_address_to_distribution_list.ps1'
Add-DistributionGroupMember : Value cannot be null. Parameter name:
serverSettings At
C:\cygwin64\scripts\add_email_address_to_distribution_list.ps1:2
char:28
+ Add-DistributionGroupMember <<<< -Identity some_distribution_list -Member user# domain.com
+ CategoryInfo : NotSpecified: (:) [Add-DistributionGroupMember],
ArgumentNullException
+ FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.
Management.RecipientTasks.AddDistributionGroupMember
Now if I remove the pubkey from my account's .authorized_keys file, login with my usual password, and retry... the script works!
I'm stumped as to why authentication via password over SSH allows for the script to run successfully , while authentication via keys over SSH doesn't.
Any ideas?
EDIT:
Here's the script that's being run:
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Add-DistributionGroupMember -Identity some_distribution_list -Member user#domain.com