我正在尝试将400台计算机添加到集合中,并且在SCCM中运行powershell时出错。我试着改变。到_但也遇到同样的错误。
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
}
}
}
答案 0 :(得分:1)
你的提示一定是犯了错误,两者都有效:
$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