使用Power shell生成机器密钥

Val*_*ter 11 powershell

我想生成机器关键的几个机之间共享,快速谷歌搜索后,我发现这个文章KB 2915218,附录A.

  1. 我复制了代码并保存为.ps1扩展名,我相信是power shell扩展名.
  2. 打开电源外壳
  3. 移动到文件的位置
  4. 运行脚本.

PS E:./Generate-MachineKey -validation sha1

它运行正常,但它不输出密钥.有什么理由吗?我在Powershell做错了吗?

谢谢,

Blu*_*kes 13

该脚本包含一个函数,因此您必须首先加载脚本,然后运行该函数才能使其工作.

首先我们必须加载文件,我打电话给我的ps1"MachineKey"所以这就是我加载它的方式

PS E:\> . .\MachineKey.ps1
Run Code Online (Sandbox Code Playgroud)

一旦我加载了文件,如果我想运行名为"Generate-MachineKey"的函数,我必须在之后输入

PS E:\> Generate-MachineKey -validationAlgorithm SHA1
Run Code Online (Sandbox Code Playgroud)


Jef*_*ler 5

它的方式比我想象的要容易:不需要保存 .ps1 文件,只需粘贴脚本并运行它。我在本地 PC 上,以管理员身份使用 Win 8.1。

以管理员身份打开 PowerShell。

粘贴来自https://support.microsoft.com/en-us/help/2915218/resolving-view-state-message-authentication-code-mac-errors#AppendixA的脚本,如下所示:

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)
>>   }
>> }
>>
Run Code Online (Sandbox Code Playgroud)

在下一行输入此命令

PS C:\Users\JM> Generate-MachineKey
<machineKey decryption="AES" decryptionKey="xxxxxxxxxxxxxxxxxxxx" validation="HMACSHA256" validationKey="xxxxxxxxxxxxxxxxx" />
Run Code Online (Sandbox Code Playgroud)