How to store hexdump of a string in hex format in powershell? - powershell

I am trying to convert a shell script to PowerShell script, I am facing issue with below shell commands
label=`echo -n "signing key" | hexdump -ve '/1 "%02x"'`
sign_nist="00000001${label}0000000100"
/testing$ echo $sign_nist
000000017369676e696e67206b65790000000100
/testing$ echo -n $sign_nist | sed -e 's/../\\x&/g'
\x00\x00\x00\x01\x73\x69\x67\x6e\x69\x6e\x67\x20\x6b\x65\x79\x00\x00\x00\x01\x00
/testing$ echo -ne "$(echo -n $sign_nist | sed -e 's/../\\x&/g')"
signing key
when I try to do the same thing in PowerShell using the below commands my output is different
PS C:\testing> $sign_nist
000000017369676E696E67206B65790000000100
PS C:\testing>
$prep = $sign_nist -split '(..)' -ne ''
for($i = 0; $i -lt $prep.length; $i++)
{
$prep[$i] = [System.Convert]::ToUInt32($prep[$i],16)
}
$sign_key_input=$prep -join '';
$sign_key_input
00011151051031101051101033210710112100010
could someone please help me how to get signing key to $sign_key_input with PowerShell?
Thanks in advance for the help.
I think appending \x for each byte works for linux to treat it as hex value but how to indicate the same in powershell?

Related

How use character '$' in a Invoke-SSHCommand without error

I try to get information from a Linux server in a powershell script
I use the Windows forms and i get the informations in a RichTextBox.
There is no problem for some basic commands.
Example :
$infoversion = Invoke-SSHCommand -Index 0 -Command "uname -r"
$Result_Info.text += "Kernel Version : $($infoversion.Output) + ("`n")
But when i use a command with a pipe and the '$' character, it doesn't work.
Example :
$infouptime = Invoke-SSHCommand -Index 0 -Command "uptime -p | awk '{print $2,$3,$4,$5,$6,$7}'"
$Result_Info.text += "Server up since $($infouptime.Output)" + ("`n")
(This command work directly on the server)
I try to set \$ or 'e or '$' or "+chr(38)+" but nothing works
If someone can help me, it will be nice :)
PowerShell uses the backtick ` as the escape character in expandable strings:
$infouptime = Invoke-SSHCommand -Index 0 -Command "uptime -p | awk '{print `$2,`$3,`$4,`$5,`$6,`$7}'"
Alternatively, since you don't actually have any variables that need expanding, you could use a single-quoted string literal - the escape sequence for a literal ' is simply '':
$infouptime = Invoke-SSHCommand -Index 0 -Command 'uptime -p | awk ''{print $2,$3,$4,$5,$6,$7}'''

Why cannot I match for strings from wsl.exe output?

I am attempting to use PowerShell to grab useful output from wsl.exe.
For the life of me, I cannot seem to match for strings in the output (see below).
I get the same behavior in both PowerShell 5 and 7.
Does anyone have a reliable means to capture the output of wsl.exe?
Thanks
PS C:\Users\gsamuel> $foo = (wsl -l -v)
PS C:\Users\gsamuel> $foo
NAME STATE VERSION
* Ubuntu-20.04 Running 2
PS C:\Users\gsamuel> $foo | Select-String -Pattern 'Ubuntu'
PS C:\Users\gsamuel> $foo -match 'Ubuntu'
PS C:\Users\gsamuel>
PS C:\Users\gsamuel>
PS C:\Users\gsamuel> [string]$foo | Select-String -Pattern 'Ubuntu'
PS C:\Users\gsamuel> [string]$foo -match 'Ubuntu'
False
This appears to be an encoding issue of some sort: there is a null between each letter of the output, probably because a UTF-16 output is being interpreted as UTF-8.
You can see this by replacing all null characters with #:
$foo = (wsl -l -v)
$foo -Replace "`0", '#'
Which gives this:
# #N#A#M#E# # # # # # # # # # # # #S#T#A#T#E# # # # # # # # # # # #V#E#R#S#I#O#N#
#
#*# #U#b#u#n#t#u#-#2#0#.#0#4# # # # #R#u#n#n#i#n#g# # # # # # # # # #2#
#
#
So the output doesn't actually contain the string "Ubuntu"!
A simple workaround would be to replace those with empty strings:
$foo -Replace "`0", '' | Select-String 'Ubuntu'
Which outputs the expected match:
* Ubuntu-18.04 Stopped 2
This is now fixed in the latest WSL Preview release 0.64.0, but you do have to "opt-in" to the fix so that older workarounds don't inadvertently break.
Adding the environment variable WSL_UTF8 with a value of 1 (and only that value) will now prevent wsl.exe from generating this "mangled" UTF-16 that was causing the issue.
For your example:
> $env:WSL_UTF8=1
> $foo = (wsl -l -v)
> $foo | Select-String -NoEmphasis -Pattern 'Ubuntu'
Ubuntu20.04_WSL1 Stopped 1
Ubuntu20.04_WSL2 Stopped 2
Ubuntu_21.10_WSL2 Stopped 2
Ubuntu-22.04 Stopped 2
Ubuntu Running 2
> $foo -match 'Ubuntu'
# Same
I'll also point to another solution in this answer for those who can't yet upgrade to Windows 11 and/or the WSL Preview. That solution changes the console's OutputEncoding in PowerShell to force wsl.exe's output to be usable.

Converting a shell script to PowerShell

After switching an application from Linux to Windows, I need to convert a shell script to a Windows equivalent. My choices were basically batch and PowerShell and I decided to give a shot to PowerShell.
For anyone interested, it's a local check for Check_MK to get information about SoftEther installed version and the number of sessions with performance data.
The initial shell script was as follow:
#!/bin/sh
cmd=$(/usr/local/vpnserver/vpncmd localhost:port /server /password:password /in:/usr/lib/check_mk_agent/local/vpncmd.txt)
version=$(echo "$cmd" | head -4 | tail -1)
sessions=$(echo "$cmd" | grep Sessions | awk '$1=$1' | cut -c21-22)
if [ -z "$version" ]; then
echo "3 VPN_Version - Can't get the information from vpncmd"
else
echo "0 VPN_Version - SoftEther VPN Server $version"
fi
if [ -z "$sessions" ]; then
echo "3 VPN_Sessions - Can't get the information from vpncmd"
else
echo "P VPN_Sessions sessions=$sessions;2;2"
fi
I basically got everything working except the 2 hardest lines of code:
cd "C:\Program Files\SoftEther VPN Server"
$cmd = vpncmd localhost:port /server /password:password /in:vpncmd.txt
$version=
$sessions=
if($version -eq $null) {
echo "3 VPN_Version - Can't get the information from vpncmd"
} else {
echo "0 VPN_Version - SoftEther VPN Server $version"
}
if($sessions -eq $null) {
echo "3 VPN_Sessions - Can't get the information from vpncmd"
} else {
echo "P VPN_Sessions sessions=$sessions;2;2"
}
I need help with going from the head, tail, grep, awk and cut one liners to whatever is equivalent in PowerShell. I read about Get-Content but I'm not sure if it's the most efficient way to do this and would like to prevent going from 1 line definition to 10 lines if that's possible to be as efficient in PowerShell.
Sample output of vpncmd's output: https://pastebin.com/J5FcHzHK
with the data being an array of lines & the word Version appearing multiple times in the actual source, the code needs to change a tad. in this version, it uses the way that -match works on an array to give the whole line as a result. that requires working on the output line to parse the desired data.
$Version = ($Vpncmd_Output -match '^Version \d{1,}\.\d{1,}' -split 'Version ' )[-1].Trim()
$SessionCount = [int]($Vpncmd_Output -match 'Number of Sessions\s+\|').Split('|')[-1].Trim()
$Version
$SessionCount
output ...
4.29 Build 9680 (English)
0
using the data in your PasteBin post, and presuming that is a multiline string, not an array of strings, this seems to work [grin] ...
$Vpncmd_Output -match '(?m)Number of Sessions\s+\|(?<Sessions>.*)'
$Matches.Sessions
# output = 0
$Vpncmd_Output -match '(?m)Version (?<Version>.+)'
$Matches.Version
# output = 4.29 Build 9680 (English)
i tried to combine the regex into one, but failed. [blush] the way i have it requires two passes, but it does work.

Word separator in powershell

I need to open a folder on a FTP server. The folder name [DAILY Files Folder] has SPACE between the words. If I try it in CMD I can use double quotes and it works. but in power shell I need the double quotes to run the actual command! does anyone know how can I add the space in my folder name without using double quotes?
$cmd = #(
"cd /DAILY Files Folder",
"put $file",
"bye"
)
[String]($cmd | & $program -pw $pass "$user#$hst" 2>&1)
Escape nested double quotes with backticks (`):
$cmd = #(
"cd `"/DAILY Files Folder`"",
"put $file",
"bye"
)
[String]($cmd | & $program -pw $pass "$user#$hst" 2>&1)
or use single quotes as the outer quotes if you don't use variables inside the string:
$cmd = #(
'cd "/DAILY Files Folder"',
"put $file",
'bye'
)
[String]($cmd | & $program -pw $pass "$user#$hst" 2>&1)
you can use the back tick character (`) to escape each space:
DAILY` Files` Folder

Piping from a variable instead of file in Powershell

Is ther any way in Powershell to pipe in from an virable instead of a file?
There are commands that I need to pipe into another command, right now that is done by first creating a file with the additional commands, and then piping that file into the original command. Code looks somehting like this now:
$val = "*some command*" + "`r`n" + "*some command*" + "`r`n" + "*some command*"
New-Item -name Commands.txt -type "file" -value $val
$command = #'
db2cmd.exe /C '*custom db2 command* < \Commands.txt > \Output.xml'
'#
Invoke-Expression -Command:$command
So instead of creating that file, can I somehow just pipe in $val insatead of Commands.txt?
Try this
$val = #("*some command*1","*some command2*","*some command3*")
$val | % { db2cmd.exe /C $_ > \Output.xml }
You should be able to pipe in from $val provided you use Write-Output or its shorthand echo, but it may also be worth trying passing the commands directly on the command line. Try this (and if it doesn't work I can delete the answer):
PS C:\> filter db2cmd() { $_ | db2cmd.exe ($args -replace '(\\*)"','$1$1\"') }
PS C:\> $val = #"
>> *custom db2 command*
>> *some command*
>> *some command*
>> *some command*
>> "#
>>
PS C:\> db2cmd /C $val > \Output.xml
What happens here is that Windows executables receive their command line from a single string. If you run them from cmd.exe you cannot pass newlines in the argument string, but Powershell doesn't have that restriction so with many programs you can actually pass multiple lines as a single argument. I don't know db2cmd.exe so it might not work here.
The strange bit of string replacement is to handle any double quotes in the arguments: Powershell doesn't quote them and the quoting rules expected by most exe files are a bit bizarre.
The only limitation here would be that $val must not exceed about 32,600 characters and cannot contain nulls. Any other restrictions (such as whether non-ascii unicode characters work) would depend on the application.
Failing that:
echo $val | db2cmd.exe /C '*custom db2 command*' > \Output.xml
may work, or you can use it in combination with the filter I defined at the top:
echo $val | db2cmd /C '*custom db2 command*' > \Output.xml