繁体   English   中英

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

[英]Unable to add txt file of computers into SCCM collection

我正在尝试将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

电源外壳:

  #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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM