2014-09-10 65 views
0

我正在編寫工作腳本並試圖確定爲什麼我的代碼顯示錯誤。我是這種編碼的新手,並且想要了解什麼是錯誤的。爲AD PC列出AD組的錯誤

我得到的錯誤來自標籤....我的.txt文件中的PC列表。

例如:Get-Content:找不到路徑'F:\ tag 77909',因爲它不存在。 我的困惑是,當我編寫主機後.Replace代碼打印正確

例如:你不能調用一個空值表達式的方法。 + $ Notags = $ PC.Replace < < < <( 「標籤」, 「PC」) + CategoryInfo:InvalidOperation:(更換:字符串)[],RuntimeEx ception + FullyQualifiedErrorId:InvokeMethodOnNull

最後一個錯誤我得到的是,它只打印出我的.txt文件列表中的最後一個PC .... ID。我不確定爲什麼給我有一個foreach循環

**MY CODE SO FAR:** 

Import-Module activedirectory 

$compImports = Get-Content "C:\Temp\temp\input.txt" 
$groupExport = "C:\temp\temp\output.txt" 
Clear-Content $groupExport 

$Header = "PC Name" + "|" + "Group Name" + "|" + "Group Description" 

#Write header 
$Header | Out-File $groupExport -Append 

#get PC tag listing 
$PCs = Get-Content $compImports 

#For loop to change all "tag " to "PC" 
foreach($PC in $PCS) 
{ 
$Notags =$PC.Replace("tag ", "PC") 
} 

#loop to get information and print it out 
foreach ($Notag in $Notags) { 
    $computerobj = Get-ADComputer $Notag -Properties memberof 
    $computerobj.memberof | ? {$_ -match '^CN=APP.*'} ` 
    | % {get-adgroup $_ -Properties name, description} | ` 
    % {$computerobj.Name + "|" + $_.name + "|" + $_.description ` 
    | Out-File $groupExport -Append} 
} 
+0

文本文件是否只包含「tag 77909」這樣的項目? – Matt 2014-09-10 17:48:11

+0

是的。但是,我發現我的錯誤。謝謝 – narue1992 2014-09-10 17:57:01

回答

1

我看到至少有一個在這裏

$compImports = Get-Content "C:\Temp\temp\input.txt" 
... 
$PCs = Get-Content $compImports 

問題要調用Get-Content這兩次會產生你所看到的最有可能的錯誤。 可以簡化爲

$PCs = Get-Content "C:\Temp\temp\input.txt" 

你其他錯誤應該消失的結果,因爲$電腦應該在這一點上包含實際數據。

+0

是的,我現在看到,感謝!我意識到爲什麼我的.txt文件沒有被讀取。我在第一個foreach循環中移動了第二個foreach循環。它打印正確。 – narue1992 2014-09-10 17:55:53

+0

如果這回答了你的問題,這將是有益的,因爲它回答 – Matt 2014-09-10 19:13:41

+0

不知道如何將其標記爲答案 – narue1992 2014-10-08 12:50:00