Import Computer From Web Service

This example demonstrates how to import a computer, using only the Mac-address, from a tool programmed in PowerShell.

 

The Import Computer and Computer Template web services must be configured to allow access to the users of this tool.

 

Create a PowerShell script and copy the code below. You must modify the SoftwareCentral address and site code to match your own. Note that only Computer Templates with naming series assigned works with this script.

 

The tool will look like this:

 

It will show a status message once it has completed:

 

Or if it failed:

ImportComputer.ps1
Copy Code
# Set your SCCM Site Code
$SiteCode = "PAX"
# SoftwareCentral Address. E.g.: http://softwarecentral
$SoftwareCentralAddress = "http://softwarecentral"

# Load assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Draw forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Import Computer"
$Form.Size = New-Object System.Drawing.Size(300,200)
$Form.StartPosition = "CenterScreen"
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size

# MAC-Address
$labelMac = New-Object System.Windows.Forms.label
$labelMac.Location = New-Object System.Drawing.Size(5,8)
$labelMac.Size = New-Object System.Drawing.Size(120,30)
$labelMac.Text = "MAC-Address:"
$Form.Controls.Add($labelMac)
$maskedTextBoxMax= New-Object System.Windows.Forms.MaskedTextBox
$maskedTextBoxMax.Location = New-Object System.Drawing.Size(130,5)
$maskedTextBoxMax.Size = New-Object System.Drawing.Size(140,20)
$maskedTextBoxMax.Mask = ">AA:AA:AA:AA:AA:AA"
$Form.Controls.Add($maskedTextBoxMax)

# Computer Template
$labelTemplate = New-Object System.Windows.Forms.label
$labelTemplate.Location = New-Object System.Drawing.Size(5,40)
$labelTemplate.Size = New-Object System.Drawing.Size(120,20)
$labelTemplate.Text = "Computer Template:"
$Form.Controls.Add($labelTemplate)
$comboBoxComputerTemplate = New-Object System.Windows.Forms.ComboBox
$comboBoxComputerTemplate.Location = New-Object System.Drawing.Size(130,40)
$comboBoxComputerTemplate.Size = New-Object System.Drawing.Size(140,5)
$Form.Controls.Add($comboBoxComputerTemplate)

# Get Computer Templates from SoftwareCentral
$computerTemplateProxy = New-WebServiceProxy -Uri "$($SoftwareCentralAddress)/Api/WS_ComputerTemplate.asmx" -Class "Proxy" -UseDefaultCredential
$computerTemplates = $computerTemplateProxy.GetComputerTemplates()

# Add Computer Templates to the ComboBox
foreach ($computerTemplate in $computerTemplates)
{
    [void]$comboBoxComputerTemplate.Items.Add($computerTemplate.TemplateName)
}

# Import Button
$buttonImport = New-Object System.Windows.Forms.Button
$buttonImport.Location = New-Object System.Drawing.Size(100,70)
$buttonImport.Size = New-Object System.Drawing.Size(75,23)
$buttonImport.Text = "Import"
$buttonImport.Add_Click($ImportComputer)
$Form.Controls.Add($buttonImport)

# Result label
$result_label = New-Object System.Windows.Forms.label
$result_label.Location = New-Object System.Drawing.Size(5,95)
$result_label.Size = New-Object System.Drawing.Size(270,50)
$Form.Controls.Add($result_label)

# Status Bar
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$statusBar1.Name = "statusBar1"
$statusBar1.Text = "Ready..."
$form.Controls.Add($statusBar1)

# Import computer if Enter is pressed
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $ImportComputer}})

# Close the from if Escape is pressed
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$Form.Close()}})

# Show form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

# Import the computer
$ImportComputer =
{
    $statusBar1.Text = "Communicating with SCCM..."
    # Web Service Parameters Start
    [string]$strComputerName = ""
    [string]$strMacAddress = $maskedTextBoxMax.Text
    [string]$strSmBiosGuid = ""
    [string]$strSiteCode = $SiteCode
    [string]$strComputerTemplateName = $comboBoxComputerTemplate.Text
    # Web Service Parameters End
    # The object that will make the call to the Web Service
    $proxy = New-WebServiceProxy -Uri "$($SoftwareCentralAddress)/Api/WS_Devices.asmx" -Class "Proxy" -UseDefaultCredential
    # Call the Web Service
    $result = $proxy.ImportComputer($strComputerName,$strMacAddress,$strSmBiosGuid,$strSiteCode,$strComputerTemplateName)
       
    $result_label.Text = $result
    if ($result_label.Text)
    {
        $statusBar1.Text = "Done"
    }
    else
    {
        $statusBar1.Text = "Failed"
    }
}

 

 

 


© Copyright - SoftwareCentral

https://softwarecentral.cloud/help