User data from 2 text files in a Set command

Hi All,

I am new to PowerShell. I am trying to use data from 2 files in a set command. Tried the below. But it gives an error. AppName.txt has multiple application names and CollName.txt has its Collection Names. I want to use both the files in the below code. Any help would be really appreciated. Thanks in advance.

Code:

$AName = Get-Content -Path "C:\Temp\AppName.txt"
$CName = Get-Content -Path "C:\Temp\CollName.txt"
Set-CMApplicationDeployment -ApplicationName $AName -CollectionName $CName -UserNotification HideAll

Error:
Set-CMApplicationDeployment : Cannot convert ‘System.Object’ to the type ‘System.String’ required by parameter
‘ApplicationName’. Specified method is not supported.
At line:1 char:46

  • Set-CMApplicationDeployment -ApplicationName $AName -CollectionName $ …
  •                                          ~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Set-CMApplicationDeployment], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Deployments.S
      etApplicationDeployment

Sn152,
Welcome to the forum. :wave:t4:

Assuming you have more than one application and more than one collection in your input files you will need a nested loop to create deployments for all apps for all collections.

$AppNameList = Get-Content -Path "C:\Temp\AppName.txt"
$CollectionNameList = Get-Content -Path "C:\Temp\CollName.txt"
foreach ($Collection in $CollectionNameList) {
    foreach ($AppName in $AppNameList) {
        Set-CMApplicationDeployment -ApplicationName $AppName -CollectionName $Collection -UserNotification HideAll
    }
}
1 Like

Olaf is right.
you have two collections and you can’t simply use two collecitons as the parameter.
you should use foreach to read every name in collecitons.

Hi Olaf, Chen,

Thank you so much for the reply.

I have already tried using foreach. But it didnt work. It gave the below error. Any other suggestion?

Set-CMApplicationDeployment : No object corresponds to the specified parameters.
At line:3 char:9

  •     Set-CMApplicationDeployment -ApplicationName $AppName -Collec ...
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (Microsoft.Confi…ationDeployment:SetApplicationDeployment) [Set-CMAp
      plicationDeployment], ItemNotFoundException
    • FullyQualifiedErrorId : ItemNotFound,Microsoft.ConfigurationManagement.PowerShell.Cmdlets.Deployments.SetAppli
      cationDeployment

Thanks!

It depends pretty much on the content of the files you’re using to provide the ApplicationName and the CollectionName.
You could output both to the console and check if they exist actually. And you could try to add a -Verbose swtich to your command to get more information.

Hi Olaf, Iam sorry. The foreach loop is working. It has made the required changes. However, the above mentioned error message is still showing. I think this is because for each application name in AppName.txt, the script is executing against all the collections available in CollName.txt. Each application should execute only once against its respective collection. Is it possible?

Thanks!

That’s an essential information. You should have mention that in your initial question.

Everything is possible. :wink:

But in that case having two unrelated txt files is the worst condition you can have. You should save the applications and the according collections together in a csv file.
Here’s an example: I will name the file “ApplicationsAndCollections.csv” and it will have this content:

"Application","Collection"
"Chrome","Chrome_PCs"
"Firefox","Firefox_PCs"
"Brave","Brave_PCs"
"Vivaldi","Vivaldi_PCs"
"Opera","Opera_PCs"

Now you can use this code snippet to create your deployments:

$ApplicationCollectionList = 
    Import-Csv -Path '.\ApplicationsAndCollections.csv'

foreach ($ApplicationCollectionPair in $ApplicationCollectionList) {
    Set-CMApplicationDeployment -ApplicationName $ApplicationCollectionPair.Application -CollectionName $ApplicationCollectionPair.Collection -UserNotification HideAll
}
3 Likes

@Olaf Thanks a lot! It worked perfectly. :slight_smile: