Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以使用PowerShell SecureString更改服务器管理员密码吗?_Powershell_Passwords_Powershell 2.0_Securestring - Fatal编程技术网

我可以使用PowerShell SecureString更改服务器管理员密码吗?

我可以使用PowerShell SecureString更改服务器管理员密码吗?,powershell,passwords,powershell-2.0,securestring,Powershell,Passwords,Powershell 2.0,Securestring,我希望编写一个脚本,使用PowerShell更改远程服务器上的管理员密码。下面的命令将执行此操作 $Admin=[adsi]("WinNT://" + MyServer + "/Administrator, user") $Admin.SetPassword("NewPassword") 但是我希望能够在脚本中隐藏“NewPassword”,使其更安全 那么,有没有办法将“NewPassword”保存到一个安全的.txt文件中,然后像这样使用它呢 $Admin.SetPassword("$se

我希望编写一个脚本,使用PowerShell更改远程服务器上的管理员密码。下面的命令将执行此操作

$Admin=[adsi]("WinNT://" + MyServer + "/Administrator, user")
$Admin.SetPassword("NewPassword")
但是我希望能够在脚本中隐藏
“NewPassword”
,使其更安全

那么,有没有办法将
“NewPassword”
保存到一个安全的.txt文件中,然后像这样使用它呢

$Admin.SetPassword("$secureFile")

脚本将作为计划任务运行。

,在将密码保存到磁盘上的文件之前,可以使用和cmdlet对密码进行加密

但是,请记住,要使用cmdlet加密/解密密码,您需要一个加密密钥。发件人:

如果使用
密钥
安全密钥
参数,高级加密标准(AES)加密 算法被使用。指定的密钥的长度必须为128192, 或256位,因为这些是AES支持的密钥长度 加密算法

如果未指定密钥,则将使用Windows数据保护API(DPAPI)进行加密。这意味着密钥将绑定到调用cmdlet的用户帐户。现在,如果您将脚本作为计划作业运行,则此解决方案将正常工作

下面是几个脚本,它们将使用生成的密钥将加密密码保存并读取到磁盘上的XML文件:

function Get-SecurePassword {
<#
.Synopsis
    Gets a password stored securely in an XML file.
.Parameter Path
    The path to the XML file to import the password from.
#>
[CmdletBinding()]
param(
    [Parameter(Position=1)]
    [string]$Path = "Password.xml"
)
    if (Test-Path $Path) {
        $cache = Import-Clixml $Path
        $key = [System.Convert]::FromBase64String($cache.Secret)
        $password = $cache.EncryptedPassword | ConvertTo-SecureString -Key $key
        $password
    }
}

function Set-SecurePassword {
<#
.Synopsis
    Stores a password securely in an XML file.
.Parameter Path
    The path to the XML file to export the password to.
#>
[CmdletBinding()]
param(
    [Parameter(Position=1)]
    [string]$Password,
    [Parameter(Position=2)]
    [string]$Path = "Password.xml"
)
    $key = New-StrongPasswordBytes -Length 32
    $textualKey = [System.Convert]::ToBase64String($key)
    $securePassword = $Password | ConvertFrom-SecureString -Key $key
    $cache = New-Object PSObject -Property @{ "EncryptedPassword" = $securePassword; "Secret" = $textualKey }
    $cache.PSObject.TypeNames.Insert(0, "SecurePassword")
    $cache | Export-Clixml $Path
}

function New-StrongPasswordBytes ($length) {
    Add-Type -Assembly System.Web
    $password = [System.Web.Security.Membership]::GeneratePassword($length, $length / 2)
    [System.Text.Encoding]::UTF8.GetBytes($password)
}
函数获取安全密码{
[CmdletBinding()]
param(
[参数(位置=1)]
[string]$Path=“Password.xml”
)
if(测试路径$Path){
$cache=Import Clixml$Path
$key=[System.Convert]::FromBase64String($cache.Secret)
$password=$cache.EncryptedPassword | ConvertTo SecureString-Key$Key
$password
}
}
函数集SecurePassword{
[CmdletBinding()]
param(
[参数(位置=1)]
[字符串]$Password,
[参数(位置=2)]
[string]$Path=“Password.xml”
)
$key=新的StrongPasswordBytes-长度32
$textualKey=[System.Convert]::ToBase64String($key)
$securePassword=$Password | convertfromsecurestring-Key$Key
$cache=New Object PSObject-Property@{“EncryptedPassword”=$securePassword;“Secret”=$textualKey}
$cache.PSObject.TypeNames.Insert(0,“安全密码”)
$cache |导出Clixml$Path
}
新函数StrongPasswordBytes($length){
添加类型-Assembly System.Web
$password=[System.Web.Security.Membership]::GeneratePassword($length,$length/2)
[System.Text.Encoding]::UTF8.GetBytes($password)
}

,在将密码保存到磁盘上的文件之前,可以使用和cmdlet对密码进行加密

但是,请记住,要使用cmdlet加密/解密密码,您需要一个加密密钥。发件人:

如果使用
密钥
安全密钥
参数,高级加密标准(AES)加密 算法被使用。指定的密钥的长度必须为128192, 或256位,因为这些是AES支持的密钥长度 加密算法

如果未指定密钥,则将使用Windows数据保护API(DPAPI)进行加密。这意味着密钥将绑定到调用cmdlet的用户帐户。现在,如果您将脚本作为计划作业运行,则此解决方案将正常工作

下面是几个脚本,它们将使用生成的密钥将加密密码保存并读取到磁盘上的XML文件:

function Get-SecurePassword {
<#
.Synopsis
    Gets a password stored securely in an XML file.
.Parameter Path
    The path to the XML file to import the password from.
#>
[CmdletBinding()]
param(
    [Parameter(Position=1)]
    [string]$Path = "Password.xml"
)
    if (Test-Path $Path) {
        $cache = Import-Clixml $Path
        $key = [System.Convert]::FromBase64String($cache.Secret)
        $password = $cache.EncryptedPassword | ConvertTo-SecureString -Key $key
        $password
    }
}

function Set-SecurePassword {
<#
.Synopsis
    Stores a password securely in an XML file.
.Parameter Path
    The path to the XML file to export the password to.
#>
[CmdletBinding()]
param(
    [Parameter(Position=1)]
    [string]$Password,
    [Parameter(Position=2)]
    [string]$Path = "Password.xml"
)
    $key = New-StrongPasswordBytes -Length 32
    $textualKey = [System.Convert]::ToBase64String($key)
    $securePassword = $Password | ConvertFrom-SecureString -Key $key
    $cache = New-Object PSObject -Property @{ "EncryptedPassword" = $securePassword; "Secret" = $textualKey }
    $cache.PSObject.TypeNames.Insert(0, "SecurePassword")
    $cache | Export-Clixml $Path
}

function New-StrongPasswordBytes ($length) {
    Add-Type -Assembly System.Web
    $password = [System.Web.Security.Membership]::GeneratePassword($length, $length / 2)
    [System.Text.Encoding]::UTF8.GetBytes($password)
}
函数获取安全密码{
[CmdletBinding()]
param(
[参数(位置=1)]
[string]$Path=“Password.xml”
)
if(测试路径$Path){
$cache=Import Clixml$Path
$key=[System.Convert]::FromBase64String($cache.Secret)
$password=$cache.EncryptedPassword | ConvertTo SecureString-Key$Key
$password
}
}
函数集SecurePassword{
[CmdletBinding()]
param(
[参数(位置=1)]
[字符串]$Password,
[参数(位置=2)]
[string]$Path=“Password.xml”
)
$key=新的StrongPasswordBytes-长度32
$textualKey=[System.Convert]::ToBase64String($key)
$securePassword=$Password | convertfromsecurestring-Key$Key
$cache=New Object PSObject-Property@{“EncryptedPassword”=$securePassword;“Secret”=$textualKey}
$cache.PSObject.TypeNames.Insert(0,“安全密码”)
$cache |导出Clixml$Path
}
新函数StrongPasswordBytes($length){
添加类型-Assembly System.Web
$password=[System.Web.Security.Membership]::GeneratePassword($length,$length/2)
[System.Text.Encoding]::UTF8.GetBytes($password)
}

我认为重要的是要注意,此处要保护的密码是模糊的,但可以访问.xml文件的任何人都可以访问。对于OP来说,这是一个足够好的解决方案,但不是特别安全。我认为需要注意的是,这里要保护的密码是模糊的,但是任何可以访问.xml文件的人都可以访问。对于OP来说,这是一个足够好的解决方案,但不是特别安全。