2015-08-24 55 views
2

我正在嘗試將腳本從我的Active Directory主機運行到網絡上的其他主機,這些主機將爲每個主機提取設備信息。無法讓PowerShell代碼工作

我使用的代碼是:

# Exports Local System Information to CSV 
# Run this PowerShelll script at log on to collect PC information to a CSV file on a network share 
# Thom McKiernan 28/08/2014 

#Get hostnames 
#$Computers = Get-Content ("C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\ComputerListAug2015.txt") -ErrorAction SilentlyContinue 

foreach ($computer in Get-Content "C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\ComputerListAug2015.txt") 
{ 

# Collect the info from WMI 
$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $computer.$deviceinfile 
$computerBIOS = get-wmiobject Win32_BIOS -Computer $computer.$deviceinfile 
$computerOS = get-wmiobject Win32_OperatingSystem -Computer $computer.$deviceinfile 
$computerCPU = get-wmiobject Win32_Processor -Computer $computer.$deviceinfile 
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $computer.$deviceinfile -Filter drivetype=3 
$macAddress = Get-WmiObject win32_networkadapterconfiguration -Computer $computer.$deviceinfile -Filter "IpEnabled = TRUE" 

#Build the CSV file 
$csvObject = New-Object PSObject -property @{ 
    "PCName" = $computerSystem.Name 
    "Manufacturer" = $computerSystem.Manufacturer 
    "Model" = $computerSystem.Model 
    "SerialNumber" = $computerBIOS.SerialNumber 
    "RAM" = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB" 
    "HDDSize" = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB" 
    "HDDFree" = "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + "GB" 
    "CPU" = $computerCPU.Name 
    "OS" = $computerOS.caption 
    "SP" = $computerOS.ServicePackMajorVersion 
    "User" = $computerSystem.UserName 
    "BootTime" = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime) 
    "MACAddress" = $macAddress.MacAddress 
    } 

#Export the fields you want from above in the specified order 
$csvObject | Select PCName, Maufacturer, Model, SerialNumber, RAM, HDDSize, HDDFree, CPU, OS, SP, User, BootTime, MACAddress | Export-Csv 'system-info.csv' -NoTypeInformation -Append 

} 

# Open CSV file for review (leave this line out when deploying) 
notepad system-info.csv 

不過,我不斷收到以下錯誤:

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

我試圖建立txt文件剛剛計算機名稱(出口從Active Directory),我試圖附加每個名稱與「.domain.com」,都沒有工作。

我試圖讓我自己的設備在列表中,沒有發生錯誤,但也沒有輸出。就好像它在使用「powershell -noexit。\ ComputerDetails.ps1」時沒有運行,但是當我通過右鍵單擊它運行腳本時,我可以看到錯誤飛過,並且記事本發出的錯誤提示文件不存在。

我曾試圖谷歌這個問題,並發現無數的資源,似乎並沒有幫助擺脫這個錯誤。收到錯誤的

全部列表:

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum 
ent is null or empty. Supply an argument that is not null or empty and then try 
the command again. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:12 char:63 
+ $computerSystem = get-wmiobject Win32_ComputerSystem -Computer <<<< $compute 
r.$deviceinfile 
    + CategoryInfo   : InvalidData: (:) [Get-WmiObject], ParameterBindi 
    ngValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
    Shell.Commands.GetWmiObjectCommand 

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum 
ent is null or empty. Supply an argument that is not null or empty and then try 
the command again. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:13 char:51 
+ $computerBIOS = get-wmiobject Win32_BIOS -Computer <<<< $computer.$deviceinf 
ile 
    + CategoryInfo   : InvalidData: (:) [Get-WmiObject], ParameterBindi 
    ngValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
    Shell.Commands.GetWmiObjectCommand 

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum 
ent is null or empty. Supply an argument that is not null or empty and then try 
the command again. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:14 char:60 
+ $computerOS = get-wmiobject Win32_OperatingSystem -Computer <<<< $computer.$ 
deviceinfile 
    + CategoryInfo   : InvalidData: (:) [Get-WmiObject], ParameterBindi 
    ngValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
    Shell.Commands.GetWmiObjectCommand 

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum 
ent is null or empty. Supply an argument that is not null or empty and then try 
the command again. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:15 char:55 
+ $computerCPU = get-wmiobject Win32_Processor -Computer <<<< $computer.$devic 
einfile 
    + CategoryInfo   : InvalidData: (:) [Get-WmiObject], ParameterBindi 
    ngValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
    Shell.Commands.GetWmiObjectCommand 

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum 
ent is null or empty. Supply an argument that is not null or empty and then try 
the command again. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:16 char:61 
+ $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName <<<< $computer. 
$deviceinfile -Filter drivetype=3 
    + CategoryInfo   : InvalidData: (:) [Get-WmiObject], ParameterBindi 
    ngValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
    Shell.Commands.GetWmiObjectCommand 

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argum 
ent is null or empty. Supply an argument that is not null or empty and then try 
the command again. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:17 char:72 
+ $macAddress = Get-WmiObject win32_networkadapterconfiguration -Computer <<<< 
$computer.$deviceinfile -Filter "IpEnabled = TRUE" 
    + CategoryInfo   : InvalidData: (:) [Get-WmiObject], ParameterBindi 
    ngValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power 
    Shell.Commands.GetWmiObjectCommand 

Attempted to divide by zero. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:27 char:53 
+  "HDDFree" = "{0:P2}" -f ($computerHDD.FreeSpace/ <<<< $computerHDD.Size) 
+ "GB" 
    + CategoryInfo   : NotSpecified: (:) [], RuntimeException 
    + FullyQualifiedErrorId : RuntimeException 

Export-Csv : A parameter cannot be found that matches parameter name 'Append'. 
At C:\Users\Administrator.ESP\Documents\SoftwareAuditing\ComputerDetails\Comput 
erDetails.ps1:37 char:183 
+ $csvObject | Select PCName, Maufacturer, Model, SerialNumber, RAM, HDDSize, H 
DDFree, CPU, OS, SP, User, BootTime, MACAddress | Export-Csv 'system-info.csv' 
-NoTypeInformation -Append <<<< 
    + CategoryInfo   : InvalidArgument: (:) [Export-Csv], ParameterBind 
    ingException 
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Comm 
    ands.ExportCsvCommand 
+0

請出示ComputerListAug2015.txt文件的內容,它會出現,就像腳本的時候遇到麻煩該文本文件 – ShaneC

+1

解析計算機名字,我沒有看到任何地方'$定義deviceinfile'。爲什麼在你的'GWMI'調用中? – TheMadTechnician

+0

[TheMadTechnician](http://stackoverflow.com/users/3245749/themadtechnician)是正確的。什麼是'$ deviceinfile'? Powershell將其視爲「$計算機」的屬性。如果該屬性不存在(它不),則返回null。 – Matt

回答

3

您在這裏的問題是,PowerShell是試圖從對象$computer通過$deviceinfile中定義的屬性。但是$computer只是一個沒有屬性$deviceinfile的字符串。這應該是一個後綴作爲你的命名約定的一部分嗎?

簡要問題的解釋

請看下面的例子

$Computer = "comp1" 
$deviceinfile = "Test" 

$Computer.$deviceinfile 

這什麼也沒有返回,因爲沒有了$computer字符串對象的「測試」屬性。我想你期待的結果是comp1.Test。現在看看這個例子。

$deviceinfile = "Test" 
$computer = New-Object -TypeName PSCustomObject -Property @{Test = "Bagel"} 
$Computer.$deviceinfile 

我們用名爲Test的屬性做了一個對象。 「百吉餅」是從該代碼返回的內容。

你可以在你的代碼

做什麼如果這是你需要將它放入引號被視爲一個屬性調用停止它的命名約定。這些變量將按照您期望的方式進行擴展。當然,假設您在調用之前已經定義了$deviceinfile

$computerSystem = get-wmiobject Win32_ComputerSystem -Computer "$computer.$deviceinfile" 

其他只是刪除它。

$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $computer 
+0

謝謝! deviceinfile是一個讓這個工作成功的實驗 - 失敗了,我以爲我把它移除了。不過,在刪除它們之後它似乎正在工作。對不起,這是一個愚蠢的錯誤。 –