2013-01-22 55 views
1

我在編寫一個腳本,它將使用PowerShell更改遠程服務器上的管理員密碼。下面的命令可以做到這一點我可以使用PowerShell SecureString更改服務器管理員密碼嗎?

$Admin=[adsi]("WinNT://" + MyServer + "/Administrator, user") 
$Admin.SetPassword("NewPassword") 

但是我想能夠隱藏"NewPassword"腳本,使之更加安全。

那麼有沒有辦法將"NewPassword"保存到一個安全的.txt文件,然後能夠像這樣使用它?

$Admin.SetPassword("$secureFile") 

該腳本將作爲計劃任務運行。

回答

1

,則可以使用ConvertTo-SecureStringConvertFrom-SecureString的cmdlet將其保存到磁盤上的文件之前,加密密碼。

但是,請記住,您需要加密密鑰才能使用cmdlet加密/解密密碼。從the documentation

如果加密密鑰是通過使用KeySecureKey 參數指定,高級加密標準(AES)加密 算法。指定的密鑰長度必須爲128,192, 或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) 
} 
+0

我覺得要注意,這裏要確保密碼被模糊處理是很重要的,但任何可以訪問.xml文件的人都可以訪問它。對OP來說足夠好的解決方案,但不是特別安全。 –

相關問題