2

I am executing the ssh-keyscan command in a PowerShell script to update the SSH known hosts file on my Windows system.

Environment details:

PowerShell:5.1.18362.752
Operating system: Windows 10 x64 1903(OSBuild 18362.1082)
Language: English 

My code is as follows:

Start-Transcript -Path C:\Windows\temp\abc.log
$sshKnownHostFile = "$Env:USERPROFILE\.ssh\known_hosts" 
$cmd = "ssh-keyscan -p 7000 blah.net | Out-File -Encoding ASCII $sshKnownHostFile -Append"
Invoke-Expression $cmd -Verbose
Stop-Transcript

The problem I am experiencing is that, no matter what I try, the console output of ssh-keyscan is not captured in the transcript file. All that's captured in the transcript file is the following:

**********************
Windows PowerShell transcript start
Start time: 20201008201659
Username: DESKTOP-NIFKPBT\kiranh
RunAs User: DESKTOP-NIFKPBT\kiranh
Configuration Name: 
Machine: DESKTOP-NIFKPBT (Microsoft Windows NT 10.0.18362.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 1280
PSVersion: 5.1.18362.752
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752
BuildVersion: 10.0.18362.752
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\Windows\temp\abc.log
**********************
Windows PowerShell transcript end
End time: 20201008201707
**********************

I have attempted the following to resolve the issue:

  1. Invoke-Expression $cmd -Verbose | Out-Default This was as per the suggestion at: https://github.com/PowerShell/PowerShell/issues/10994#issuecomment-550528523

  2. Invoke-Expression $cmd -Verbose | Out-Host This was the suggestion at: https://github.com/PowerShell/PowerShell/issues/10994#issuecomment-596132121

  3. [System.Environment]::SetEnvironmentVariable("GIT_REDIRECT_STDERR", "2>&1", "User") This was as per this response. The above answer was for Git. Since ssh-keyscan.exe is from the Git family of utilities, I tried using the same to see if my issue would be resolved.

  4. Use of Start-Process cmdlet to invoke ssh-keyscan.exe

  5. Directly invoking ssh-keyscan.exe in the PowerShell script, just like an external program is executed on the command prompt.

None of the above have really helped. The output captured in the transcript log is the same in all of the above cases.

How can I get this working?

halfer
  • 19,824
  • 17
  • 99
  • 186
Kiran Hegde
  • 680
  • 4
  • 14
  • Just tested on 5.1.19041.1 win10 2004 and it captured everything fine. Does it capture the usage if you do `ssh-keyscan --h`? I ran ssh-keyscan.exe directly as a native executable – Doug Maurer Oct 08 '20 at 15:55
  • Hello @DougMaurer Thanks for responding back. I just tried the same on a Win 10 x64 2004 system(OS build: 19041.508), Powershell version: 5.1.19041.1). Same behavior. I had ssh-keyscan.exe in 3 differernt locations on my system:1)C:\Program Files\Git\usr\bin\ssh-keyscan.exe 2)C:\Windows\System32\OpenSSH 3)C:\Windows\WinSxS\amd64_openssh-client-components-onecore_31bf3856ad364e35_10.0.19041.1_none_b5ee49ccbbfbfddb. I executed ssh-keyscan.exe from all the 3 different locations. Yet the problem does not go away. What is the version of Git on your system and where is it being invoked from? – Kiran Hegde Oct 09 '20 at 09:31
  • Hello @DougMaurer Hope you are doing good. Did you get a chance to take a look at my previous comment? – Kiran Hegde Oct 18 '20 at 04:18
  • I didn’t see you changed the title to specify ssh-keyscan from git, I was using the one built into windows. – Doug Maurer Oct 18 '20 at 04:56

1 Answers1

0

As I commented, ssh-keyscan output shows up in the log just fine. Here are the commands I ran and the sanitized output.

Start-Transcript .\transcript.txt
ssh-keyscan linuxhost
$PSVersionTable

As you can see below, the transcript contains the output from ssh-keyscan as well as $PSVersionTable

Machine: DESKTOP-NEKK74J (Microsoft Windows NT 10.0.19041.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 8692
PSVersion: 5.1.19041.546
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.546
BuildVersion: 10.0.19041.546
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is .\transcript.txt
PS C:\TEMP> ssh-keyscan.exe linuxhost
# linuxhost:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
linuxhost ssh-rsa AAAAB3NzaC1yc2EAAAADAQ
# linuxhost:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
linuxhost ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItb
# linuxhost:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
linuxhost ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOGy3M1
PS C:\TEMP> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.546
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.546
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Running your commands, I see all the commands typed in the transcript as well.

Transcript started, output file is .\transcript.txt
PS C:\TEMP> $sshhostfile = "$env:USERPROFILE\.ssh\known_hosts"
PS C:\TEMP> $cmd = "ssh-keyscan linuxhost | Out-File -Encoding ASCII $sshhostfile -Append"
PS C:\TEMP> Invoke-Expression $cmd -Verbose
PS C:\TEMP> Stop-Transcript

I have no clue why your commands wouldn't be listed. I'd run them again just to be sure. If you're expecting to see the output that you're redirecting to the file, only what is shown in the console may be captured. If you're wanting the output to go to both a log file and the transcript, perhaps these changes to your commands will help

$cmd = {ssh-keyscan -p 7000 blah.net}
& $cmd | Tee-Object -FilePath $sshhostfile -Append

Now the transcript and the log have this output.

Transcript started, output file is .\transcript.txt
PS C:\TEMP> $cmd = {ssh-keyscan linuxhost}
PS C:\TEMP> & $cmd | Tee-Object -FilePath $sshhostfile
linuxhost ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCgoqTpOufcs
linuxhost ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyN
linuxhost ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOGy3M1StlLMI
PS C:\TEMP> Stop-Transcript
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13