2014-01-12 156 views
0

所以,我一直在爲HP Bios的腳本編寫一些任務。有一個稱爲HPBIOS_BIOSSettingInterface的Wmi命名空間。它基本上有一個特殊的SetBIOSSetting(「Property」,「Value」,「CurrentSetupPassword」)。以前的HP系統只支持KBD編碼,我編寫了一個函數來編碼帶有KDB值的字符串,可以在這裏找到:http://thomas-malkewitz.webnode.com/news/convert-tokbdstring/,並且使用此編碼設置設置密碼值時,它可以工作,但是對於新的HP,它只支持UTF-16(我無法找到它的大或小,但都嘗試過)。這樣做時我不斷收到錯誤。所以,我的問題是,我如何編碼一個字符串值並將其傳遞給此方法。我無法弄清楚我的生活。這裏是我的功能:Powershell編碼將值傳遞給WmiMethod

<# 
.Synopsis 
    Sets the Setup Password on an HP Bios. 
.DESCRIPTION 
    This function can be used to set a password on the Bios, it can also be used to clear the password, the current password is needed to change the value. 
    If a new value is being set, and not cleared, it must be between 8 and 30 characters. 
.EXAMPLE 
    Set-HpSetupPassword -NewSetupPassword "MyNewPassword" 
.EXAMPLE 
    Set-HpSetupPassword -ComputerName "mycomputer.mydomain.org" -NewSetupPassword " " -CurrentSetupPassword "MyCurrentPassword" 
.EXAMPLE 
    Set-HpSetupPassword -NewSetupPassword "MyNewSetupPassword" -CurrentSetupPassword "MyCurrentPassword" 
.LINKS 
    https://github.com/necromorph1024/HPTpmAndBitLocker 
#> 
function Set-HpSetupPassword 
{ 
    [CmdletBinding()] 
    [OutputType([void])] 
    Param 
    (
     # ComputerName, Type string, System to set Bios Setup Password. 
     [Parameter(Mandatory=$false, 
        Position=0)] 
     [string] 
     $ComputerName=$env:COMPUTERNAME, 

     # NewSetupPassword, Type string, The value of the password to be set. The password can be cleared by using a space surrounded by double quotes, IE: " ". 
     [Parameter(Mandatory=$true, 
        Position=1)] 
     [string] 
     $NewSetupPassword, 

     # CurrentSetupPassword, Type string, The value of the current setup password. 
     [Parameter(Mandatory=$false, 
        Position=2)] 
     [AllowEmptyString()] 
     [string] 
     $CurrentSetupPassword 
    ) 

    Begin 
    { 
     if (!(Test-Connection -ComputerName $ComputerName -Quiet -Count 2)) 
     { 
      throw "Unable to connect to $ComputerName. Please ensure the system is available." 
     } 

     try 
     { 
      $manufacturer = Get-WmiObject -Class Win32_ComputerSystem -Namespace "root\CIMV2" -Property "Manufacturer" -ComputerName $ComputerName -ErrorAction Stop 
      if ($manufacturer.Manufacturer -ne "Hewlett-Packard") 
      { 
       throw "Computer Manufacturer is not of type Hewlett-Packard. This cmdlet can only be used on Hewlett-Packard systems." 
      } 
     } 
     catch 
     { 
      throw "Unable to connect to the Win32_ComputerSystem WMI Namespace, verify the system is avaialbe and you have the permissions to access the namespace." 
     } 
    } 
    Process 
    { 
     if (-not([String]::IsNullOrWhiteSpace($NewSetupPassword))) 
     { 
      if (($NewSetupPassword.Length -lt 8) -or ($NewSetupPassword.Length -gt 30)) 
      { 
       throw "The Password Values must be be between 8 and 30 characters if not clearing the password." 
      } 
     } 

     $hpBios = Get-WmiObject -Class HP_BiosSetting -Namespace "root\HP\InstrumentedBIOS" -ComputerName $ComputerName -ErrorAction Stop 
     $hpBiosSettings = Get-WmiObject -Class HPBIOS_BIOSSettingInterface -Namespace "root\HP\InstrumentedBIOS" -ComputerName $ComputerName -ErrorAction stop 

     if (($hpBios | Where-Object { $_.Name -eq "Setup Password"}).SupportedEncoding -eq "kbd") 
     { 
      if (-not([String]::IsNullOrEmpty($NewSetupPassword))) 
      { 
       $NewSetupPassword="<kbd/>"+(Convert-ToKbdString -UTF16String $NewSetupPassword) 
      } 
      if (-not([String]::IsNullOrEmpty($CurrentSetupPassword))) 
      { 
       $CurrentSetupPassword="<kbd/>"+(Convert-ToKbdString -UTF16String $CurrentSetupPassword) 
      } 
     } 

     $hpBiosSettings.SetBIOSSetting("Setup Password",$NewSetupPassword,$CurrentSetupPassword) 
    } 
} 

它使返回6是拒絕訪問,這是我與舊的BIOS讓,直到我創建了一個轉換,KbdString方法。我知道我使用的密碼是正確的。但我不知道什麼編碼發送到WmiMethod。感謝您的幫助

這裏是GitHub庫:https://github.com/necromorph1024/HpTpmAndBitLocker

回答

0

我覺得自己太愚蠢了,甚至現在問這個問題。我回顧了我自己的方法,並且字符串需要使用編碼類型進行標記。我知道這一點,這就是爲什麼我追加<kbd/>的字符串,如果它支持該編碼類型。所以,<utf-16/>標記只需要追加到字符串的開頭。 OMG,對於任何時候我可能已經浪費了!

1

.NET字符串是Unicode編碼,根據對String類的文檔。

http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx

+0

你知道,這就是我的想法,現在我更加沮喪,因爲它應該與$ hpBiosSettings.SetBIOSSettings(「設置密碼」,「MyNewPassword」,「MyCurrentPassword」)一起工作,這讓我發瘋。謝謝。 – dotps1

0

你如何得到這個與Foreach($計算機$計算機名)循環運行?放在哪裏,會有什麼特別的語法?

+0

ComputerName參數有一個格式問題,您可以從我的github站點獲取更新的函數:https://github.com/necromorph1024/HpTpmAndBitLocker/blob/master/HPTpmAndBitLocker.psm1 您將需要Convert-ToKbdString函數以及。 – dotps1