我正在嘗試將400臺計算機添加到集合中,並且在SCCM中運行powershell時出現錯誤。我嘗試改變。來_但也遇到同樣的錯誤。無法將txt文件的計算機添加到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
}
}
}
如果你想要那笨拙的,將索引更改爲'$ filenames [$ x] .Name.Split(「。」)[0]'後面'但我更喜歡一個簡單的'foreach($ filename中的$ filename){' – LotPings
謝謝,但我仍然得到相同的錯誤信息。如果我使用更簡單,我會得到錯誤。 'Get-Content:無法將參數綁定到參數'Path',因爲它爲空。 在線:19 char:30 + $ computers = Get-Content $文件名[$ x] + ~~~~~~~~~~~~~~ + InfoInfo:(:) [Get-內容],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand' –