Powershell - 使用 invokeMethod() 指定凭据

Powershell - Specify credential using invokeMethod()

我正在尝试创建一个脚本来将计算机对象添加到 SCCM。长话短说,我无法使用在 SCCM 模块中找到的 cmdlet,因为我需要调用命令以便可以使用不同的凭据。

我这辈子都想不出在哪里指定要使用的凭据,或者是否可能。以下是我如何执行此操作的几个示例。

    $CSVLocation = #Combo box item, will fill in later
$CSVImport = Import-Csv $CSVLocation

#Initialize connection to the SCCM 2012 Environment
$ScopeOptions = $Null
$Scope = New-Object System.Management.ManagementScope -ArgumentList "\$SiteServer\root\sms\site_$SiteCode",$ScopeOptions
$Null = $Scope.Connect()
$Site = New-Object System.Management.ManagementClass -ArgumentList $Scope,'SMS_Site',$Null

foreach ($Computer in $CSVImport)
{
    if ($Computer.MAC)
    {
        $MethodName = 'ImportMachineEntry'
        $InParams = $Site.GetMethodParameters($MethodName)
        $InParams.MACAddress = $Computer.Mac
        $InParams.NetbiosName = $Computer.Name
        $InParams.OverwriteExistingRecord = $true
        $CMComputer = $Site.InvokeMethod($MethodName, $InParams, $Null)
    }
    elseif ($Computer.GUID)
    {
        $MethodName = 'ImportMachineEntry'
        $InParams = $Site.GetMethodParameters($MethodName)
        $InParams.SMBIOSGUID = $Computer.GUID
        $InParams.NetbiosName = $Computer.Name
        $InParams.OverwriteExistingRecord = $true
        $CMComputer = $Site.InvokeMethod($MethodName, $InParams, $Null)
    }
}

上面的语法是我喜欢的方式,对我来说似乎更清晰。我也一直在玩下面的方法,它适用于通过 mac 地址导入。不确定哪个位置参数用于 GUID。

Invoke-WmiMethod -Credential $Credential -Namespace root/SMS/site_$($SiteCode) -Class SMS_Site -Name ImportMachineEntry -ArgumentList @($null, $null, $null, $null, $null, $null, $Computer.mac, $null, $Computer.name, $True, $null, $null) -ComputerName $SiteServer

最坏的情况我会使用第二个片段来做我需要的。只是希望有人可以确认我是否可以通过第一种方法提供凭据,如果可以,在哪里以及如何?

第一种方法:
如果要使用凭据,则需要在建立连接时使用。例如:

$Scope = new-object system.management.managementscope
$Scope.path = "\$SiteServer\root\sms\site_$SiteCode"
$options = $Scope.Options
$Options.Username = 'domain\user'
$Options.Password = 'Password' 

上面以明文形式存储密码确实有风险,仅举个例子。

第二种方法:
BIOS Guid 位置如下所示:(11111111-1111-1111-1111-111111111111 是您的 BIOS Guid)

-ArgumentList @($null, $null, $null, $null, $null, $null, $Computer.mac, $null, $Computer.name, $True,'11111111-1111-1111-1111-111111111111', $null)