Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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编码将值传递给WmiMethod_Powershell_Wmi_Bios - Fatal编程技术网

Powershell编码将值传递给WmiMethod

Powershell编码将值传递给WmiMethod,powershell,wmi,bios,Powershell,Wmi,Bios,因此,我一直在为HP Bios编写一些任务脚本。有一个名为HPBIOS_BIOSSettingInterface的Wmi命名空间。它基本上只有一个方法设置(“属性”、“值”、“当前设置密码”)。较旧的HP系统只支持KBD编码,我编写了一个函数用KDB值对字符串进行编码,可以在这里找到:当使用这种编码设置设置密码值时,它可以工作,但对于新的HP系统,它只支持UTF-16(我找不到它是大还是小,但已经尝试了两者)。我做这件事时总是出错。所以,我的问题是,如何对字符串值进行编码并将其传递给这个方法。我

因此,我一直在为HP Bios编写一些任务脚本。有一个名为HPBIOS_BIOSSettingInterface的Wmi命名空间。它基本上只有一个方法设置(“属性”、“值”、“当前设置密码”)。较旧的HP系统只支持KBD编码,我编写了一个函数用KDB值对字符串进行编码,可以在这里找到:当使用这种编码设置设置密码值时,它可以工作,但对于新的HP系统,它只支持UTF-16(我找不到它是大还是小,但已经尝试了两者)。我做这件事时总是出错。所以,我的问题是,如何对字符串值进行编码并将其传递给这个方法。我一辈子都搞不懂这件事。以下是我的功能:

<#
.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)
    }
}

函数集HpSetupPassword
{
[CmdletBinding()]
[输出类型([void])]
Param
(
#ComputerName,键入字符串,系统设置Bios设置密码。
[参数(强制=$false,
位置=0)]
[字符串]
$ComputerName=$env:ComputerName,
#NewSetupPassword,键入字符串,要设置的密码值。可以使用双引号包围的空格清除密码,即:“”。
[参数(必需=$true,
职位=1)]
[字符串]
$NewSetupPassword,
#CurrentSetupPassword,键入字符串,当前设置密码的值。
[参数(强制=$false,
位置=2)]
[AllowEmptyString()]
[字符串]
$CurrentSetupPassword
)
开始
{
if(!(测试连接-计算机名$ComputerName-安静-计数2))
{
抛出“无法连接到$ComputerName。请确保系统可用。”
}
尝试
{
$manufacturer=Get WmiObject-Class Win32\u ComputerSystem-Namespace“root\CIMV2”-属性“manufacturer”-ComputerName$ComputerName-ErrorAction Stop
如果($manufacturer.manufacturer-ne“Hewlett-Packard”)
{
throw“计算机制造商不是Hewlett-Packard类型。此cmdlet只能在Hewlett-Packard系统上使用。”
}
}
抓住
{
抛出“无法连接到Win32_ComputerSystem WMI命名空间,请验证系统是否可用,以及您是否有访问该命名空间的权限。”
}
}
过程
{
if(-not([String]::IsNullOrWhiteSpace($NewSetupPassword)))
{
如果($NewSetupPassword.Length-lt 8)-或($NewSetupPassword.Length-gt 30))
{
throw“如果不清除密码,密码值必须介于8到30个字符之间。”
}
}
$hpBios=获取WmiObject-类HP\U BiosSetting-命名空间“root\HP\InstrumentedBIOS”-计算机名$ComputerName-错误操作停止
$hpbiosettings=Get WmiObject-Class HPBIOS\u biosettinginterface-Namespace“root\HP\InstrumentedBIOS”-ComputerName$ComputerName-error操作停止
if($hpBios | Where Object{$\ux.Name-eq“Setup Password”}).SupportedEncoding-eq“kbd”)
{
if(-not([String]::IsNullOrEmpty($NewSetupPassword)))
{
$NewSetupPassword=”“+(转换为tokbstring-UTF16String$NewSetupPassword)
}
if(-not([String]::IsNullOrEmpty($CurrentSetupPassword)))
{
$CurrentSetupPassword=”“+(转换为TokbString-UTF16String$CurrentSetupPassword)
}
}
$hpBiosSettings.SetBIOSSetting(“设置密码”、$NewSetupPassword、$CurrentSetupPassword)
}
}
它保持返回6,即访问被拒绝,这是我在创建Convert-KbdString方法之前使用旧bios得到的结果。我知道我使用的密码是正确的。但是我不知道什么编码被发送到WmiMethod。谢谢你的帮助


这是GitHub Repo:

。根据
字符串
类的文档,NET字符串采用Unicode编码


我现在连问这个问题都觉得很愚蠢。我回顾了我自己的方法,需要用编码类型标记字符串。我知道这一点,这就是为什么如果字符串支持这种编码类型,我会将
附加到字符串中。因此,
标记只需要附加到字符串的开头。天哪,很抱歉我浪费了这么多时间

如何让它在Foreach($computername中的计算机)循环中运行?这四个字母分别放在哪里,有什么特殊的语法吗

你知道,这就是我想的,现在我更沮丧了,因为它应该与$hpbiosettings.SetBIOSSettings(“设置密码”、“MyNewPassword”、“MyCurrentPassword”)一起工作,这让我抓狂。谢谢。ComputerName参数存在格式化问题,您可以从我的github站点获得更新的函数:您还需要Convert TokbString函数。