我正在尝试生成机器密钥以在几台机器之间共享,经过快速谷歌搜索后我发现了article KB 2915218, Appendix A 。
PS E:./Generate-MachineKey -validation sha1
它运行正常,但它没有输出密钥。有什么理由吗?我在Powershell做错了吗?
谢谢,
答案 0 :(得分:12)
该脚本包含一个函数,因此您必须首先加载脚本,然后运行该函数才能使其工作。
首先我们必须加载文件,我打电话给我的ps1" MachineKey"这就是我加载它的方式
PS E:\> . .\MachineKey.ps1
如果我想运行名为" Generate-MachineKey"的函数,我已经加载了文件。我必须在之后输入
PS E:\> Generate-MachineKey -validationAlgorithm SHA1
答案 1 :(得分:3)
它比我想象的要容易:无需保存.ps1文件,只需粘贴脚本并运行它。我在本地PC上,以管理员身份Win 8.1。
以管理员身份打开PowerShell。
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.
PS C:\Users\JM> # Generates a <machineKey> element that can be copied + pasted into a Web.config file.
PS C:\Users\JM> 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)
>> }
>> }
>>
在下一行输入此命令
PS C:\Users\JM> Generate-MachineKey
<machineKey decryption="AES" decryptionKey="xxxxxxxxxxxxxxxxxxxx" validation="HMACSHA256" validationKey="xxxxxxxxxxxxxxxxx" />