Reputation: 299
I am trying to add 400 computer to a collection and I have an error running the powershell in SCCM. I try to change the . to _ but also encounter the same error.
Method invocation failed because [System.Char] does not contain a method named 'Split'.
At line:8 char:5
+ $collectionname = $filenames.Name[$x].Split(".")[0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Powershell:
#Set path to collection directory
$collectiondir = "D:\Collections\"
#Pull only .TXT files into array
$filenames = Get-ChildItem $collectiondir* -include *.txt
for ($x=0; $x -lt ($filenames.Length); $x++) {
$collectionname = $filenames.Name[$x].Split(".")[0]
$collectionname
#Add new collection based on the file name
try {
New-CMDeviceCollection -Name $collectionname -LimitingCollectionName "All Systems"
}
catch {
"Error creating collection - collection may already exist: $collectionname" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
}
#Read list of computers from the text file
$computers = Get-Content $filenames[$x]
foreach($computer in $computers) {
try {
Add-CMDeviceCollectionDirectMembershipRule -CollectionName $collectionname -ResourceId $(get-cmdevice -Name $computer).ResourceID
}
catch {
"Invalid client or direct membership rule may already exist: $computer" | Out-File "$collectiondir\$collectionname`_invalid.log" -Append
}
}
}
Upvotes: 0
Views: 709
Reputation:
You must have made a mistake following my hints, both do work:
$collectiondir = "D:\Collections\"
#Pull only .TXT files into array
$filenames = Get-ChildItem -Path $collectiondir -Filter *.txt
for ($x=0; $x -lt ($filenames.Length); $x++) {
$collectionname = $filenames[$x].Name.Split(".")[0]
$collectionname
}
"-----"
ForEach ($file in $filenames){
$collectionname = $file.Name.Split(".")[0]
$collectionname
}
If you wanted to split the extension a better way is to simply use BaseName instead of Name
Upvotes: 1