0

I have a profile that gets executed every time I open my PowerShell window. Since I do some SSH'ing, I need to ensure that my gets added right after I start up PowerShell. But I want to avoid asking my passphrase for the SSH as long as it's already added to the ssh-agent. The solution to this problem is described here, but for the Linux shell.

I have converted the solution described there to a PowerShell equivalent, which is as follows:

$ssh_add = "$env:ProgramFiles/Git/usr/bin/ssh-add.exe"
$ssh_keygen = "$env:ProgramFiles/Git/usr/bin/ssh-keygen.exe"
$my_key_path = "$env:USERPROFILE/.ssh/id_rsa"

$my_ssh_key = & $ssh_keygen -lf $my_key_path
$ssh_keys = & $ssh_add -l

if (!(Select-String -Pattern $my_ssh_key -Path $ssh_keys -SimpleMatch -Quiet))
{
    & $ssh_add -t 5h $my_key_path
}

The SSH keys happen to contain colons, which PowerShell seems to think are drive letters. This results in the following error message:

Select-String : Cannot find drive. A drive with the name '4096 SHA256' does not exist.

The SSH keys in the following form:

$my_ssh_key = 4096 SHA256:somelongSSHkey some.email@stackoverflow.com (RSA)
$ssh_keys = 4096 SHA256:anotherlongSSHkey /c/Users/MyUser/.ssh/id_rsa (RSA)

How can I prevent the colon from being parsed as a drive letter seperator?

Community
  • 1
  • 1
tambre
  • 4,625
  • 4
  • 42
  • 55

1 Answers1

1

-Path is supposed to be a path. The value you are providing to it will, naturally, be interpreted as a path. You could escape the colon, but that doesn't solve your problem of telling Select-String to treat something as a path when it isn't a path.

You want the -InputObject (can be shortened, as all PS arguments can) parameter where you're using -Path right now. That will match the literal string, rather than treating it as a file to search. You can also pipe the contents of $ssh_keys to Select-String instead, and not specify any path.

if (!(Select-String -Input $ssh_keys -Pattern $my_ssh_key -SimpleMatch -Quiet))
...

See https://technet.microsoft.com/library/hh849903.aspx for full documentation.

CBHacking
  • 1,984
  • 16
  • 20