我有来自原始来源的以下powershell脚本。它用于生成用于验证视图状态MAC的机器密钥。但是,当我在Powershell中运行它时:
PS C:\Users\Documents> .\Generate-MachineKey.ps1 -decryptionAlgorithm AES -validationAlgorithm SHA1
没有输出,以下是脚本。什么都没有输出的原因是什么。
function Generate-MachineKey {
[CmdletBinding()]
param (
[ValidateSet("AES", "DES", "3DES")]
[string]$decryptionAlgorithm = 'AES',
[ValidateSet("MD5", "SHA1", "HMACSHA256", "HMACSHA384", "HMACSHA512")]
[string]$validationAlgorithm = 'HMACSHA256'
)
process {
function BinaryToHex {
[CmdLetBinding()]
param($bytes)
process {
$builder = new-object System.Text.StringBuilder
foreach ($b in $bytes) {
$builder = $builder.AppendFormat([System.Globalization.CultureInfo]::InvariantCulture, "{0:X2}", $b)
}
$builder
}
}
switch ($decryptionAlgorithm) {
"AES" { $decryptionObject = new-object System.Security.Cryptography.AesCryptoServiceProvider }
"DES" { $decryptionObject = new-object System.Security.Cryptography.DESCryptoServiceProvider }
"3DES" { $decryptionObject = new-object System.Security.Cryptography.TripleDESCryptoServiceProvider }
}
$decryptionObject.GenerateKey()
$decryptionKey = BinaryToHex($decryptionObject.Key)
$decryptionObject.Dispose()
switch ($validationAlgorithm) {
"MD5" { $validationObject = new-object System.Security.Cryptography.HMACMD5 }
"SHA1" { $validationObject = new-object System.Security.Cryptography.HMACSHA1 }
"HMACSHA256" { $validationObject = new-object System.Security.Cryptography.HMACSHA256 }
"HMACSHA385" { $validationObject = new-object System.Security.Cryptography.HMACSHA384 }
"HMACSHA512" { $validationObject = new-object System.Security.Cryptography.HMACSHA512 }
}
$validationKey = BinaryToHex($validationObject.Key)
$validationObject.Dispose()
[string]::Format([System.Globalization.CultureInfo]::InvariantCulture,
"<machineKey decryption=`"{0}`" decryptionKey=`"{1}`" validation=`"{2}`" validationKey=`"{3}`" />",
$decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
$validationAlgorithm.ToUpperInvariant(), $validationKey)
}
}
答案 0 :(得分:2)
因为在命令中调用脚本文件实际上并没有在脚本中调用函数Generate-MachineKey
。
在您的情况下,正确的方法是“dot source”脚本文件,然后调用脚本中包含的函数:
. .\Generate-MachineKey.ps1
Generate-MachineKey -decryptionAlgorithm AES -validationAlgorithm SHA1
请阅读dot sourcing Powershell scripts了解更多详情,了解脚本,功能,呼叫和点源概念之间的区别
答案 1 :(得分:1)
您正在调用脚本,但内部没有函数。在脚本末尾添加对函数的调用:
Generate-MachineKey -decryptionAlgorithm AES -validationAlgorithm SHA1
或者,你可以从函数中取出代码,然后让#pa;&#39;而是将section作为脚本的参数定义。