Failure to add a computer to SCCM

I'm trying to add a computer to SCCM, but not having much luck. This is for testing so I'm generating the MAC and ComputerName randomly. I keep getting the error 'Exception calling "InvokeMethod" with "3" argument(s): "Generic failure ". Can someone tell me what I'm doing wrong? I know I'm getting a good connection to the SCCM server because I can run a SelectQuery to get information from SCCM. I also know it's not a permissions issue, as I have been successful using another method to add a computer using this same account.

FYI, I'm avoiding using "[wmiclass]" type constructors because I will be translating this to vb.net later for incorporation into a project in Visual Studio and didn't know how to translate "[wmiclass]" into vb.net, whereas objects created via New-Object should be easy.

Here is my code:

	$MAC = '{0:X12}' -f (Get-Random 0xFFFFFFFFFFFF) | ? {$_ -match ('(..)' * 6)} | % { $Matches[1..6] -join ':' }
	$ComputerName = 'w-amtest-l-{0:D5}' -f (Get-Random 99999)
	Write-Host ('Adding {0} (MAC {1}) to SCCM' -f $ComputerName,$MAC)
	$SCCMServerName = 'enaarwmg02'
	$SiteName = 'site_AM2'
	$CollectionName = 'AM20002F'
	#endregion

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

	#region Add the computer to SCCM
	$MethodName = 'ImportMachineEntry'
	$InParams = $Site.GetMethodParameters($MethodName)
	$InParams.MACAddress = $MAC
	$InParams.NetbiosName = $ComputerName
	$InParams.OverwriteExistingRecord = $false
	$CMComputer = $Site.InvokeMethod($MethodName, $InParams, $Null)
Any help would be appreciated.

August 12th, 2015 2:15pm

The ImportMachineEntry method, which is documented here, indeed has a lot more parameters. This is an example that I've successfully used before.

Invoke-WmiMethod -Namespace root/SMS/site_$($SiteCode) -Class SMS_Site -Name ImportMachineEntry -ArgumentList @($null, $null, $null, $null, $null, $null, $MACAddress, $null, $ResourceName, $True, $null, $null) -ComputerName $SiteServer
Free Windows Admin Tool Kit Click here and download it now
August 12th, 2015 2:29pm

You could try the cmdlets in the SCCM PowerShell Module, import-cmcomputerinfomration.

Import-CMComputerInformation -CollectionName "MyCollection" -ComputerName "MyComputerName" -MacAddress "5F:DA:FA:FA:FA:FA"

You can also invoke them on a remote computer, such as the site server that has SCCM console and module already there.
August 12th, 2015 2:44pm

Thanks, but all the reading I've done on this says I must use the InvokeMethod method to call ImportMachineEntry; I cannot call ImportMachineEntry directly. Hence the need for calling GetMethodParameters to return an object to pass the parameters needed by ImportMachingEntry. And, indeed, the object returned by ...GetMethodParameters('ImportMachineEntry') does contain properties for all those mentioned in the document you linked to.

Also, note I'm staying away from most WMI command built into PowerShell because this will be used later in vb.net; I'm just prototyping the code in PowerShell because it's easier to test with.

So, I guess, bottom line is I don't think that's the issue, but thanks anyway.

Free Windows Admin Tool Kit Click here and download it now
August 12th, 2015 2:46pm

Thanks, but, again, the end result will be in vb.net so, while PowerShell has many nifty modules and cmdlets to do something similar, I'd like to know what's wrong with the code I've posted.

August 12th, 2015 2:52pm

Stranger and stranger. This is weird. If I do this:

$Site = [WmiClass]"\\$SCCMServerName\ROOT\SMS\$($SiteName):SMS_Site"
	$InParams = $Site.PSBase.GetMethodParameters($MethodName)
	$InParams.MACAddress = '99:EB:40:84:3A:D1'
	$InParams.NetbiosName = 'w-amtest-29507'
	$InParams.OverwriteExistingRecord = $true
	$CMComputer = $Site.PSBase.InvokeMethod($MethodName, $inParams, $Null)

It succeeds. However if I change it slightly to this:

$Site = [WmiClass]"\\$SCCMServerName\ROOT\SMS\$($SiteName):SMS_Site"
	$InParams = $Site.PSBase.GetMethodParameters($MethodName)
	$InParams.MACAddress = '{0:X12}' -f (Get-Random 0xFFFFFFFFFFFF) | ? {$_ -match ('(..)' * 6)} | % { $Matches[1..6] -join ':' }
	$InParams.NetbiosName = 'w-amtest-l-{0:D5}' -f (Get-Random 99999)
	$InParams.OverwriteExistingRecord = $true
	$CMComputer = $Site.PSBase.InvokeMethod($MethodName, $inParams, $Null)

It fails?!? All I'm doing differently is using a statement to generate the machine name and mac address, whereas if I hard code in the same values it succeeds. WTF?

But I still can't get it to work if I create the $Site object using New-Object rather than "[wmiclass]"... and I need to know why.

Free Windows Admin Tool Kit Click here and download it now
August 12th, 2015 3:07pm

You could try the cmdlets in the SCCM PowerShell Module, import-cmcomputerinfomration.

Import-CMComputerInformation -CollectionName "MyCollection" -ComputerName "MyComputerName" -MacAddress "5F:DA:FA:FA:FA:FA"

You can also invoke them on a remote computer, such as the site server that has SCCM console and module already there.
August 12th, 2015 6:41pm

You could try the cmdlets in the SCCM PowerShell Module, import-cmcomputerinfomration.

Import-CMComputerInformation -CollectionName "MyCollection" -ComputerName "MyComputerName" -MacAddress "5F:DA:FA:FA:FA:FA"

You can also invoke them on a remote computer, such as the site server that has SCCM console and module already there.
Free Windows Admin Tool Kit Click here and download it now
August 12th, 2015 6:41pm

Follow-up to this. I figured out what's happening, though it seems to be a bug in System.Management to me.

If I use a computer name that is 15 characters or less it succeeds 100% of the time. If I use a computer name that is 16 characters or more it fails 100% of the time.

My company's standard workstation names are 16 characters. How do I work around this bug?

August 13th, 2015 3:34pm

There's a terrible standard since most things rely on NetBIOS naming in which computer names are technically limited to 15 characters:

https://msdn.microsoft.com/en-us/library/ms853959.aspx

Free Windows Admin Tool Kit Click here and download it now
August 13th, 2015 3:56pm

Thanks Jason. I think that explains it. Not sure what to do about it, but this is probably the reason I get the error.

August 13th, 2015 4:02pm

That makes sense. I tried importing a machine via the AdminConsole to test and hit the same limitation.
Free Windows Admin Tool Kit Click here and download it now
August 13th, 2015 4:04pm

Well, you can run down the hall screaming that so-and-so, the person who made the naming policy, is an idiot and should be fired immediately since this has been in place for well over 20 years and then laugh and throw a party when security escorts them out. Just kidding.

Not much you can do except pass the link on to those responsible for the policy and quietly point out that it's a bad policy for the reason given on that page. It'll be up to them to correct the ship. You could also open a support case with Microsoft -- you'll be footing the bill there though as this isn't a bug. You could also file a design change request to have ConfigMgr perhaps allow for longer names in the future -- nothing will change there anytime soon though.

August 14th, 2015 10:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics