简体   繁体   中英

Executing a command in PowerShell by setting an environment variable in cmd

I want to execute a function in PowerShell from Windows CMD. Basically the requirement is to calculate the hash of a string, since Windows batch/cmd does not provide the functionality to generate a hash of a string, I've to use PowerShell.

I don't want to run a PowerShell script or put the function in a ps script as by doing so I have to bypass the Windows PowerShell security policy.

Below is the code which I am running in Hash.cmd . The code, when executed in PowerShell, runs perfectly fine.

@echo off
powershell -Command "& { Function Get-Hash([String] $String) { $StringBuilder = New-Object System.Text.StringBuilder;   [System.Security.Cryptography.HashAlgorithm]::Create("sha1").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{  [Void]$StringBuilder.Append($_.ToString("x2")); }; $StringBuilder.ToString(); }; $res=Get-Hash "Hello world"; echo $res; }"

But this is resulting in error on CMD as below:

At line:1 char:153
+ ... lder;   [System.Security.Cryptography.HashAlgorithm]::Create(sha1).Co ...
+                                                                  ~
Missing ')' in method call.
At line:1 char:153
+ ... ;   [System.Security.Cryptography.HashAlgorithm]::Create(sha1).Comput ...
+                                                              ~~~~
Unexpected token 'sha1' in expression or statement.
At line:1 char:41
+ & { Function Get-Hash([String] $String) { $StringBuilder = New-Object ...
+                                         ~
Missing closing '}' in statement block or type definition.
At line:1 char:3
+ & { Function Get-Hash([String] $String) { $StringBuilder = New-Object ...
+   ~
Missing closing '}' in statement block or type definition.
At line:1 char:157
+ ...    [System.Security.Cryptography.HashAlgorithm]::Create(sha1).Compute ...
+                                                                 ~
Unexpected token ')' in expression or statement.
At line:1 char:262
+ ... etBytes($String))|{  [Void]$StringBuilder.Append($_.ToString(x2)); }; ...
+                                                                  ~
Missing ')' in method call.
At line:1 char:262
---- TRUNCATED-----

You're having quoting issues because you're using double quotes within double quotes which breaks the quoting.

As the strings in your code that are double quoted don't require expansion (see about_quoting_rules ), you can just change the double quotes "sha1" / "x2" / "Hello world" to single quotes eg 'sha1'


EDIT: Working code for me:

powershell -command "& { Function Get-Hash([String]$String) { $StringBuilder = New-Object System.Text.StringBuilder;   [System.Security.Cryptography.HashAlgorithm]::Create('sha1').ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{  [Void]$StringBuilder.Append($_.ToString('X2')); }; $StringBuilder.ToString(); }; $res=Get-Hash 'Hello world'; echo $res; }"

The issue was that I was using % in place for Foreach-Object

Below is the correct cmd script

@echo off
powershell -command "& { Function Get-Hash([String] $String) { $StringBuilder =  New-Object System.Text.StringBuilder;   [System.Security.Cryptography.HashAlgorithm]::Create('sha1').ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String)) | Foreach-object {  [Void]$StringBuilder.Append($_.ToString('x2')); }; $StringBuilder.ToString(); }; $hash=Get-Hash 'Hello World'; echo $hash; }"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM