2017-08-20 41 views
0

我正在嘗試將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 
      } 
    } 
} 
+0

如果你想要那笨拙的,將索引更改爲'$ filenames [$ x] .Name.Split(「。」)[0]'後面'但我更喜歡一個簡單的'foreach($ filename中的$ filename){' – LotPings

+0

謝謝,但我仍然得到相同的錯誤信息。如果我使用更簡單,我會得到錯誤。 'Get-Content:無法將參數綁定到參數'Path',因爲它爲空。 在線:19 char:30 + $ computers = Get-Content $文件名[$ x] + ~~~~~~~~~~~~~~ + InfoInfo:(:) [Get-內容],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand' –

回答

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而不是名稱

+0

對不起,我不明白它是如何適用於你,而不是我。 '你不能在一個空值表達式上調用一個方法。 在線:8 char:5 + $ collectionname = $ filenames [$ x] .Name.Split(「。」)[0] + ~~~~~~~~~~~~~~~~~ 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 + :InvokeMethodOnNull' –

+0

$ filenames和類型的內容是什麼?在for前面插入'$ filenames | gm'。 – LotPings

+0

它只是一個帶有計算機名稱的文本文件。我還用完整的FQDN計算機名稱測試了另一個文本文件。 –

相關問題