none
Powershell, howto get it done in less lines? RRS feed

  • Question

  • Hi,

    I have these SCCM 2012 lines which I 'd like to do in less lines.
    I could use

    $Limitingcollections = "All Systems"
    but when I use
    $DevicecollectionName = @("Windows 7", "Windows 8" etc) and use

    New-CMDevicecollection -Name $DeviceCollectionName -LimitingCollection $Limitingcollection
    It says

    "New-CMDeviceCollection : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'.

    so apparently it is not supported.

    New-CMDeviceCollection -name "Windows 7" -LimitingCollectionName "All Systems"
    New-CMDeviceCollection -name "Windows 8" -LimitingCollectionName "All Systems"
    New-CMDeviceCollection -name "Windows 2012 R2" -LimitingCollectionName "All Systems"
    New-CMDeviceCollection -name "Domain Controllers" -LimitingCollectionName "All Systems"
    New-CMDeviceCollection -name "SCCM 2012 R2" -LimitingCollectionName "All Systems"
    New-CMDeviceCollection -name "SQL Servers" -LimitingCollectionName "All Systems"

    Please advise howto do this.

    J.


    Jan Hoedt

    Friday, March 13, 2015 2:07 PM

Answers

  • Here's one method:

    $collectionNames = @('Windows 7','Windows 8','Windows 2012 R2')
    
    foreach ($collectionName in $collectionNames) {
    
        New-CMDeviceCollection -Name $collectionName -LimitingCollection $LimitingCollection
    
    }


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Marked as answer by janhoedt Friday, March 13, 2015 2:25 PM
    Friday, March 13, 2015 2:13 PM
  • Do you mean this?


    $names = @(
      "Windows 7"
      "Windows 8"
      "Windows 2012 R2"
      "Domain Controllers"
      "SCCM 2012 R2"
      "SQL Servers"
    )
    $names | foreach-object {
      new-cmdevicecollection -name $_ -limitingcollectionname "All Systems"
    }
    


    -- Bill Stewart [Bill_Stewart]


    Friday, March 13, 2015 2:13 PM
    Moderator

All replies