locked
Two Variables for Script to Loop RRS feed

  • Question

  • I'm trying to loop a script I tested which I ran manually. I would like to automate the process by setting up two variables from a list (or csv if it is more efficient to use as 1 file.) 

    $PRGNAME = Get-Content C:\temp\names.txt
    $PRGID = Get-Content C:\temp\ids.txt

    I think I can use a CSV file with headers PRGNAME and PRGID and somehow import those line by line to run the script example I have below. I've just not had success getting a scripted loop to work the way I've attempted. 

    Any suggestions?

    $PRGIDP = $PRGID + "P"
    $PRGIDQ = $PRGID + "Q"
    $PRGIDD = $PRGID + "D"
    
    New-item -Name "$PRGNAME" -Path $($SiteCode.name + ":\DeviceCollection\PRGlication Server Collections")
    $Collection1 = @{Name = "$PRGNAME | ALL"; Query = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.NetbiosName like '%$PRGIDP%' or SMS_R_System.NetbiosName like '%$PRGIDQ%' or SMS_R_System.NetbiosName like '%$PRGIDD%'"}
    $Collection2 = @{Name = "$PRGNAME | PROD"; Query = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.NetbiosName like '%$PRGIDP%'"}
    $Collection3 = @{Name = "$PRGNAME | QA"; Query = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.NetbiosName like '%$PRGIDQ%'"}
    
    New-CMDeviceCollection -Name $Collection1.Name -Comment "All Servers" -LimitingCollectionName $LimitingCollectionAll -RefreshSchedule $Schedule -RefreshType 2 | Out-Null
    Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection1.Name -QueryExpression $Collection1.Query -RuleName $Collection1.Name
    Write-host *** Collection $Collection1.Name created ***
    New-CMDeviceCollection -Name $Collection2.Name -Comment "All Servers" -LimitingCollectionName $LimitingCollectionAll -RefreshSchedule $Schedule -RefreshType 2 | Out-Null
    Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection2.Name -QueryExpression $Collection2.Query -RuleName $Collection2.Name
    Write-host *** Collection $Collection2.Name created ***
    New-CMDeviceCollection -Name $Collection3.Name -Comment "All Servers" -LimitingCollectionName $LimitingCollectionAll -RefreshSchedule $Schedule -RefreshType 2 | Out-Null
    Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection3.Name -QueryExpression $Collection3.Query -RuleName $Collection3.Name
    Write-host *** Collection $Collection3.Name created ***


    T.J.

    Monday, December 17, 2018 4:41 PM

All replies

  • Hi TJ

    You can use Import-CSV and create a variable to do the loop. Below would be an example of the CSV format. 

    Do a foreach loop through each row. I dont think the query will work with $devices. header name so i would create two new variables. I would use a if statment for each row so the correct query is added to each colleciton. On the all collection just need to add variables to add the addtional two PRGID. See below this should work there is a missing varaible for $scheduale, the PRGNAME & PRGID will need to be updated to match your own name/ID's and change the if($Devices.PRGID -like "PRGIDP") to match. 

    $DeviceCollecitons = Import-Csv C:\temp\Collection.csv
    
    foreach ($Devices in $DeviceCollecitons){
    
    $collectionName = $Devices.PRGNAME
    $collectionGID = $Devices.PRGID
    
    New-item -Name "$collectionName" -Path $($SiteCode.name + ":\DeviceCollection\PRGlication Server Collections")
    if($Devices.PRGID -like "PRGIDD" ){
    $collectionGID2 = $DeviceCollecitons.PRGID[1]
    $collectionGID3 = $DeviceCollecitons.PRGID[2]
    $Collection = @{Name = "$collectionName | ALL"; Query = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.NetbiosName like '%$collectionGID%' or SMS_R_System.NetbiosName like '%$collectionGID2%' or SMS_R_System.NetbiosName like '%$collectionGID3%'"}
    }
    if($Devices.PRGID -like "PRGIDP"){
    $Collection = @{Name = "$collectionName | PROD"; Query = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.NetbiosName like '%$collectionGID%'"}
    }
    if($Devices.PRGID -like "PRGIDQ"){
    $Collection = @{Name = "$collectionName | QA"; Query = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.NetbiosName like '%$collectionGID%'"}
    }
    
    New-CMDeviceCollection -Name $Collection.Name -Comment "All Servers" -LimitingCollectionName $LimitingCollectionAll -RefreshSchedule $Schedule -RefreshType 2 | Out-Null
    Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -QueryExpression $Collection.Query -RuleName $Collection.Name
    Write-host *** Collection $Collection.Name created ***
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    Monday, December 17, 2018 6:36 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 24, 2018 2:51 AM
  • I'm sorry, I have been away but I am back to work on this and will let you know how it went for me.

    T.J.

    Wednesday, January 23, 2019 1:02 PM