Я хочу выполнить функцию в PowerShell из Windows CMD.По сути, требование состоит в том, чтобы вычислить хеш-строку, поскольку Windows batch / cmd не предоставляет функциональные возможности для генерации хеш-строки, я должен использовать PowerShell.
Я не хочу запускатьсценарий PowerShell или поместите функцию в сценарий ps, так как при этом я должен обойти политику безопасности Windows PowerShell.
Ниже приведен код, который я выполняю в Hash.cmd
.Код, выполняемый в PowerShell, работает отлично.
@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; }"
Но это приводит к ошибке CMD, как показано ниже:
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-----