繁体   English   中英

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指南)

-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