无法将计算机的 txt 文件添加到 SCCM 集合中



我正在尝试将 400 台计算机添加到集合中,但在 SCCM 中运行电源外壳时出错。我尝试更改 .到 _ 但也会遇到相同的错误。

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
}
}
}

按照我的提示,你一定犯了一个错误,两者都有效:

$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
}

如果你想拆分扩展,更好的方法是简单地使用BaseName而不是Name

最新更新