CMD.exe and Powershell.exe cannot generate ssh keys using ssh-keygen - windows

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!

Related

Problems using "RunAs" in powershell

I am trying to access powershell from another user (user2), but at the same time I also want to access the resources of my main user (user1), I have a virtual environment, windows server 2022 and a windows 10 pro machine, in which I am doing the following:
Right click on the powershell icon > Run as another user, then I access user 2 to which I want to connect with their respective credentials.
Once this is done in powershell I execute the following:
runas /user:corp\user2 /netonly powershell
Then, I put the credentials again and navigate to the path of my main user(user1) and when I execute "ls", it gives me the following error:
ls : Access denied to path 'C:\Users\user1'.
At line:1 char:14
+ ls
+ ~~
+ CategoryInfo : PermissionDenied: (C:\Users\user1:String) [Get-ChildItem], UnauthorizedAccessExcepti
on
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand

Configure a GCP Filestore on a GCP Windows Instance

I've been trying to mount the Google Filestore and use as a NFS Shared volume on two of my GCP Windows VM instances but I keep getting the error below. Any assistance is much appreciated. I need to figure this one out.
PS C:\Windows\system32> mount 10.*.***.250:/logs_arcon D:\Storage
cmdlet New-PSDrive at command pipeline position 1
Supply values for the following parameters:
Root: D:\
mount : Cannot find a provider with the name 'D:\Storage'.
At line:1 char:1
+ mount 10.42.144.250:/logs_arcon D:\Storage
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (D:\Storage:String) [New-PSDrive], ProviderNotFoundException
+ FullyQualifiedErrorId : ProviderNotFound,Microsoft.PowerShell.Commands.NewPSDriveCommand
The format of your command mount 10.42.144.250:/logs_arcon D:\Storage doesn't look to be aligned with the provided in the documentation examples.
The format shown in the docs should work:
mount 10.0.0.2:/vol1 z:

How to get output from powershell script over ssh?

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 )

cURL in power shell windows 8.1: "A drive with the name 'localhost' does not exist"

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

Varying permissions on Windows Server based on method of authenticating

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

Resources