Very simple powershell how to manually add computer to SCCM

I must say apologizes to Guy, he contacted me and it took me over a month to reply. He asked me if I could fix my site. I couldn’t but now I have re-write the content. This once again is a great reminder on the importance of getting backups.

Rant over and topic at hand. There is a simple way to make a powershell script to add computer to your SCCM database.

$SiteCode = "DEV" 
$ProviderMachineName = "sccm.de.mo"
$initParams = @{}

if((Get-Module ConfigurationManager) -eq $null) {
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
}
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
Set-Location "$($SiteCode):\" @initParams
$MAC = Read-Host -Prompt 'Input Computer MAC address'
$CpuName = Read-Host -Prompt 'Input Computername'
Set-Location "$($SiteCode):\" @initParams
Import-CMComputerInformation -CollectionName "All Systems" -ComputerName "$CpuName" -MacAddress $mac
Write-Host "Added $Added Computers, skipped $Skipped"
get-date -Format "HH:mm:ss dd.MM.yyyy"

Last time I think i had this. This connects to SCCM and prompts Computer name and MAC.

If you’d need more than a single import then you could use this:

$SiteCode = "DEV"
$ProviderMachineName = "sccm.de.mo"
$initParams = @{}

if ((Get-Module ConfigurationManager) -eq $null) {
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams
}

if ((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}

Set-Location "$($SiteCode):\" @initParams
$Added = 0
$Skipped = 0

while ($true) {
    $MAC = Read-Host -Prompt 'Input Computer MAC address (leave blank to stop)'
    $CpuName = Read-Host -Prompt 'Input Computer name (leave blank to stop)'
    if ([string]::IsNullOrWhiteSpace($MAC) -or [string]::IsNullOrWhiteSpace($CpuName)) {
        Write-Host "Ending import as one of the inputs was blank."
        break
    }
    try {
        Import-CMComputerInformation -CollectionName "All Systems" -ComputerName "$CpuName" -MacAddress $MAC
        $Added++
        Write-Host "Added $Added"
    } catch {
        Write-Host "Failed to add computer with name $CpuName and MAC $MAC"
        $Skipped++
    }
}
Write-Host "Added $Added computers, skipped $Skipped"
Write-Host "Script completed at $(Get-Date -Format 'HH:mm:ss dd.MM.yyyy')"

When you no longer need to import just leave a field empty and it will cut you out and end the script.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *