locked
Computer role assignment through powershell script fails RRS feed

  • Question

  • Hi

    I am trying to assign more than one role to a computer as it is added to the MDT database.  The powershell script I am using is well known and works fine when only one role is in the CSV file.  I've listed the script below along with the createmachines.csv. My aim is to create a new machine in the database and assign it more than one Role dependant on the entry/entries against it in the csv file.  To prove it can be done, there are two lines in the script which I have used.  Where I have defined the roles inside the script, they are applied correctly to the computer.  Where I try reading the roles from the csv file, I cannot get it to work properly.

    I have tried changing the Roles inside the csv file to various formats, for example,

    @('Role1,Role2,Role3')
    @('Role1','Role2','Role3')

    I have tried other formats, but none of the formats I have tried has succeeded in populating the database correctly.

    Can anyone shed some light on this problem for me?

    Many thanks in advance.

    Mike

    [createmachines.csv]
    name,mac,Role
    test1,00:00:00:00:00:01,Role1,Role2,Role3
    test2,00:00:00:00:00:02,Role1,Role2,Role3

    [CreateMachinesInDatabase.ps1]

    Import-Module –name D:\Scripts\MDTDB.psm1 -Force

    Connect-MDTDatabase –sqlServer SQLSERVER –instance sqlexpress –database MDT

    $machines = Import-Csv D:\Scripts\createmachines.csv

    For ($i=1; $i -le $machines.count; $i++)

    {

       New-MDTComputer -description $machines[$i-1].name -macAddress $machines[$i-1].mac -settings @{
        OSInstall='YES';
        OSDComputerName=$machines[$i-1].name;
        }

    # Set the roles in the script works fine
    Get-MDTComputer -macAddress $machines[$i-1].mac | Set-MDTComputerRole -roles @('Role 1','Role 2');

    #Set the roles reading the csv fails
    Get-MDTComputer -macAddress $machines[$i-1].mac | Set-MDTComputerRole -roles $machines[$i-1].Role;

    }


    • Edited by Lothian313 Tuesday, October 4, 2016 2:19 PM Error in script
    Tuesday, October 4, 2016 10:23 AM

Answers

  • I never did find an answer to this.  MDT and Powershell has moved on so much since I posted this that I don't think I would consider trying to implement this now.

    What are you trying to achieve using multiple roles?  Perhaps I can help suggest an alternative.

    • Marked as answer by Lothian313 Tuesday, July 16, 2019 1:04 PM
    Tuesday, July 16, 2019 11:48 AM

All replies

  • I am trying to do this and was wondering if you resolved this problem?

    Tuesday, July 16, 2019 11:38 AM
  • I never did find an answer to this.  MDT and Powershell has moved on so much since I posted this that I don't think I would consider trying to implement this now.

    What are you trying to achieve using multiple roles?  Perhaps I can help suggest an alternative.

    • Marked as answer by Lothian313 Tuesday, July 16, 2019 1:04 PM
    Tuesday, July 16, 2019 11:48 AM