Powershell sshd commands different - powershell

I have OpenSSH installed on Windows Server 2019 https://github.com/PowerShell/Win32-OpenSSH
SSH works great except for an issue with pipe ( | ) commands in Powershell SSH. I have successfully converted some of my | commands to ; example:
powershell Mount-VHD -Path D:/VMdir/tester.vhdx -PassThru | Get-Disk | Get-Partition | Get-Volume
becomes
powershell Mount-VHD -Path D:/VMdir/tester.vhdx -PassThru; Get-Disk; Get-Partition; Get-Volume
and it works fine, but I also have:
powershell (Get-VM tester | select-object MemoryMaximum).memorymaximum/1mb
and I can't get this one to work in SSH, works fine in windows though but not via SSH. I have tried a ; and & but it doesn't work. First why would the pipe commands work inside windows but not outside windows via ssh and any idea on how I can replace the pipe in the above command to get it work through ssh? The above command displays only a number (eg. 2048) it doesn't display any other info and that's what I need only a number.

Use "|" instead of | works perfectly.

Related

How does this custom script to find expiring certificates work?

Our monitoring software has custom scripts to download. I downloaded the following PowerShell version of a "Certificates Expiring" script. The software has a command line portion and a script body portion.
The command line
cmd /c powershell.exe -ExecutionPolicy RemoteSigned .\${FileName}.ps1 ${DeviceName} ${UserName} ${Password}
The script body is as such
$certificates= get-childitem cert: -recurse | where-object {$_.NotAfter -gt (get-date)}
Write-Host "Subject Certificate Going to Expire"
Write-Host "Data:";
$certificates | select FriendlyName,#{Name="Expires in (Days)";Expression={($_.NotAfter).subtract([DateTime]::Now).days}} | where "Expires in (Days)" | Sort "Expires in (Days)"
This outputs EVERY certificate on the server when I only want 1 specific certificate. I am somewhat new to PowerShell scripting and I cannot get this particular script to work when using PowerShell locally on the server. When I run it from the monitoring server it outputs fine. I just want to find a parameter I can enter to only look for the one certificate to monitor.

powershell cmd executed in ISE but not as ps1

I am facing a peculiar issue.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion |
Format-Table –AutoSize > C:\temp\InstalledSoftwareList.txt
When I run the above command from ISE, it creates the file and provides the expected output to txt file.
But when I save as PS1 and run with double click or by opening powershell console and run, it just creates txt file with no output.
I tried changing the working directory, admin user etc but no use. Suggestions please
It is PowerShell 5 and Windows 2016 R2

Get-PsDrive Description not showing drive name

I am trying to get the location of a usb named Foobar.
Running (Get-PsDrive -Name D).Description works in Powershell ISE but not in Powershell.
When running in Powershell ISE the result is Foobar
When running in Powershell I get nothing back (empty string)
What is the difference when running the same command in ISE and in the normal powershell?
Also is it possible to get a drive location reliably by the name/label of the device?
Thomas, I tested using this script:
clear-host
$x = Get-Psdrive | Where-object Name -like "?"
ForEach ($drv in $x) {
"$($drv.name) : $($drv.Description)"
}
I got the following result on 5.1 ISE 5.1.18362.1171 & CMD 5.1.18362.1171 and also on PS7 7.0.0 (all 64 Bit):
Since you're checking for D, I'm assuming it is a CD drive. Thus, it appears to be normal behavior? Not sure why you got different results.

Piping file contents to remote file over SSH

I am trying to share an SSH public key with a remote server using Powershell. Typically, in Linux environments, using Bash, I would use cat and a pipe to append the contents of the file, like so:
cat ~/.ssh/id_rsa.pub | ssh user#machine 'cat >> ~/.ssh/authorized_keys'
However, I am communicating between Windows 10 hosts using the SSH feature in Windows 10, so do not have access to GNU utilities.
Instead, I am trying to use Powershell. I currently have the following Powershell code:
Get-Content ~\.ssh\id_ed25519.pub | ssh user#machine "'$_' | Out-File -FilePath ~\.ssh\authorized_keys -Append"
This runs without error but the file on the remote machine is empty. How would I go about appending my public key to the authorized_keys file on the remote machine?
I may be approaching this problem from the wrong direction; most of my shell scripting experience is in Bash on Linux. If there is a cleaner Powershell solution, I would love feedback!
CLARIFICATION: I am using PowerShell as the default shell on my remote Windows host.
I solved this by getting rid of the pipe after Get-Content and evaluating the Get-Content call in the command I run with SSH:
ssh user#machine "'$(Get-Content .\.ssh\id_ed25519.pub)' | Out-File -FilePath ~\.ssh\authorized_keys -Append"

Powershell NAS command not working

I am trying to get NAS drive parameters using PS commands. The below command, however, is not getting any value when called via Python code by connecting to remote server. However, if I run it by copy-pasting into that remote server it gives output.
$nas = Get-PSDrive | Where-Object {$_.DisplayRoot -like '*jkl.com*'}
write-host $nas
No error and no output when run through Python code.