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
Select-String : Cannot find drive. A drive with the name '4096 SHA256' does not exist.
$my_ssh_key = 4096 SHA256:somelongSSHkey email@example.com (RSA)
$ssh_keys = 4096 SHA256:anotherlongSSHkey /c/Users/MyUser/.ssh/id_rsa (RSA)
-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
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.