powershell - Unable to add txt file of computers into SCCM collection -
i trying add 400 computer collection , have error running powershell in sccm. try change . _ encounter same error.
method invocation failed because [system.char] not contain method named 'split'. @ line:8 char:5 + $collectionname = $filenames.name[$x].split(".")[0] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo : invalidoperation: (:) [], runtimeexception + fullyqualifiederrorid : methodnotfound
powershell:
#set path collection directory $collectiondir = "d:\collections\" #pull .txt files array $filenames = get-childitem $collectiondir* -include *.txt ($x=0; $x -lt ($filenames.length); $x++) { $collectionname = $filenames.name[$x].split(".")[0] $collectionname #add new collection based on file name try { new-cmdevicecollection -name $collectionname -limitingcollectionname "all systems" } catch { "error creating collection - collection may exist: $collectionname" | out-file "$collectiondir\$collectionname`_invalid.log" -append } #read list of computers 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 exist: $computer" | out-file "$collectiondir\$collectionname`_invalid.log" -append } } }
you must have made mistake following hints, both work:
$collectiondir = "d:\collections\" #pull .txt files array $filenames = get-childitem -path $collectiondir -filter *.txt ($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 wanted split extension better way use basename instead of name
Comments
Post a Comment