简体   繁体   English

Powershell - 使用invokeMethod()指定凭证

[英]Powershell - Specify credential using invokeMethod()

I'm trying to create a script to add computer objects to SCCM. 我正在尝试创建一个脚本来向SCCM添加计算机对象。 Long story short, I can't use the cmdlets found in the SCCM module as I need to invoke the command so that different credentials can be used. 简而言之,我无法使用SCCM模块中的cmdlet,因为我需要调用命令以便可以使用不同的凭据。

I can't for the life of me figure out where to specify the credentials to use or if its even possible. 我不能为我的生活找出指定要使用的凭据的位置,或者它是否可能。 Below are a couple samples of how i'm doing this. 以下是我如何做这个的几个样本。

    $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)
    }
}

The above syntax is how i'd prefer to do it, just seems cleaner to me. 上面的语法是我喜欢做的,对我来说似乎更清洁。 I've also been playing with the below method which works for importing via mac address. 我也一直在使用以下方法,该方法适用于通过mac地址导入。 Not sure yet which positional param is for the GUID. 还不确定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

Worst case i'll use the second snippet to do what I need. 最坏的情况我将使用第二个片段来做我需要的。 Was just hoping someone could confirm if I can supply credentials via the first method and if so where and how? 只是希望有人可以确认我是否可以通过第一种方法提供凭证,如果是,那么在哪里以及如何提供?

For the first method: 对于第一种方法:
If you want to use the credential, you need to do it when making the connection. 如果要使用凭证,则需要在建立连接时执行此操作。 For example: 例如:

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

The above store the password as clear text which is really risky, just for example. 以上将密码存储为明文,这是非常危险的,例如。

For your second method: 对于你的第二种方法:
the BIOS Guid position is like below: (11111111-1111-1111-1111-111111111111 is your BIOS Guid) BIOS Guid位置如下:(11111111-1111-1111-1111-111111111111是你的BIOS指南)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM