String Powershell:安全字符串';加密密钥';用法---加入域脚本

String Powershell:安全字符串';加密密钥';用法---加入域脚本,string,security,powershell,dns,key,String,Security,Powershell,Dns,Key,我在一个企业环境中工作,在那里我管理一个由75台计算机组成的实验室。我使用Ghost进行图像处理,然后在计算机上漫游,以更改PC名称和SID 我正在实现一个脚本来自动将计算机添加到域中,但我是PowerShell的新手,非常感谢您的帮助。下面是我正在使用的脚本1.ps1: Param ( [String]$User = $(Throw "MYDOMAINUSERINFO"), [String]$Domain = "MYDOMAININFO", [String]

我在一个企业环境中工作,在那里我管理一个由75台计算机组成的实验室。我使用Ghost进行图像处理,然后在计算机上漫游,以更改PC名称和SID

我正在实现一个脚本来自动将计算机添加到域中,但我是PowerShell的新手,非常感谢您的帮助。下面是我正在使用的脚本1.ps1:

    Param (
    [String]$User = $(Throw "MYDOMAINUSERINFO"),
     [String]$Domain = "MYDOMAININFO",
     [String]$PathToCred = "C:\OMC\AutoPost"
     ) 

    #Make sure our path string has a trailing backslash
    If ($PathToCred[$PathToCred.Length - 1] -ne "\")
    {    $PathToCred += "\"
    }

    #Now create file string
    $File = $PathToCred + "JoinDomain-$User.crd"

    #And find out if it's there, if not create it
    If (-not (Test-Path $File))
    {    (Get-Credential).Password | ConvertFrom-SecureString | Set-Content $File
    }

    #Load the credential file
    $Password = Get-Content $File | ConvertTo-SecureString
    $Credential = New-Object System.Management.Automation.PsCredential($User,$Password)

    #Add the computer to the domain
    Add-Computer -DomainName $Domain -Credential $Credential
我使用放置在启动文件夹中的批处理文件运行此脚本

   Powershell.exe -ExecutionPolicy Bypass C:\OMC\AutoPost\1.ps1 -User MYDOMAINUSERINFO -Domain MYDOMAININFO -PathToCred C:\OMC\AutoPost\
运行此脚本可以正常工作,它会创建一个凭据文件,读取凭据文件并加入域。在重影和漫游后运行此脚本不起作用,我得到错误:

    Key not valid for use in specified state.
我想这是因为电脑知道有些东西已经改变了。我正在使用与最初创建凭据时相同的用户帐户添加到域中,因此我认为计算机正在拒绝这些凭据,因为SID已更改

我在网上读到,我可以使用[-key Byte[]]设置标准加密密钥,这将允许我绕过此错误。我对PowerShell太陌生了,不知道如何使用它,有人能帮我吗

更多信息:


Powershell SecureString使用加密用户作为加密密钥进行加密,这意味着其他用户无法对其解密,但是您可以在加密时指定不同的密钥并再次使用它进行解密,当然这不安全,但这是您的决定

要创建凭据并将其导出到文件,请执行以下操作:

$credential = Get-Credential
$Key = [byte]1..16
$credential.Password | ConvertFrom-SecureString -Key $Key | Set-Content $File
要加载它,请更新脚本部分

#Load the credential file

$Key = [byte]1..16
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$Credential = New-Object System.Management.Automation.PsCredential($User,$Password)

Powershell SecureString使用加密用户作为加密密钥进行加密,这意味着其他用户无法对其解密,但是您可以在加密时指定其他密钥并再次使用它进行解密,当然这不安全,但这是您的决定

要创建凭据并将其导出到文件,请执行以下操作:

$credential = Get-Credential
$Key = [byte]1..16
$credential.Password | ConvertFrom-SecureString -Key $Key | Set-Content $File
要加载它,请更新脚本部分

#Load the credential file

$Key = [byte]1..16
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$Credential = New-Object System.Management.Automation.PsCredential($User,$Password)

使用$Key=[byte]1..16的格式是什么?我读到钥匙应该是24位数字。这有必要吗?从理论上讲,我可以使用:$Key=[byte]1----也很感谢,我会测试它。使用$Key=[byte]1..16的格式是什么?我读到钥匙应该是24位数字。这有必要吗?从理论上讲,我可以使用:$Key=[byte]1----也很好,谢谢你,我会测试它。为什么你要使用Ghost(在没有系统准备的机器上)而不是WDS和MDT(有点像标准的MS部署工具):有了MDT,自动加入域很容易。额外的好处是无穷无尽的:数据库支持,或使用特定设置自动部署,软件安装和其他好东西。(WDS是标准的windows服务器角色,MDT是免费的)。在很多情况下,人们都在寻找一种技术解决方案来规避那些他们应该实际查看工作流程并加以改进的问题。为什么要使用Ghost(在非sysprepped机器上)而不是WDS和MDT(有点像标准的MS部署工具):使用MDT进行自动域加入很容易。额外的好处是无穷无尽的:数据库支持,或使用特定设置自动部署,软件安装和其他好东西。(WDS是标准的windows服务器角色,MDT是免费的)。在很多情况下,人们寻求一种技术解决方案来规避问题,在这些问题上,他们实际上应该关注工作流程并加以改进。