【问题标题】:Unable to get PowerShell code to work无法让 PowerShell 代码工作
【发布时间】:2015-08-25 00:28:29
【问题描述】:

我正在尝试从我的 Active Directory 主机向网络上的其他主机运行一个脚本,该脚本将为每个主机提取设备信息。

我使用的代码是:

# 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:无法验证参数“ComputerName”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。

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

【问题讨论】:

  • 请显示 ComputerListAug2015.txt 文件的内容,脚本似乎无法从该文本文件中解析计算机名称
  • 我没有看到 $deviceinfile 在任何地方定义。为什么会出现在您的 GWMI 电话中?
  • TheMadTechnician 说得对。 $deviceinfile 是什么? Powershell 将其视为$computer 的属性。如果该属性不存在(它不存在),则返回 null。
  • 致歉。 deviceinfile 是一个让它工作的实验——失败了,我以为我删除了它。但是,我已经删除了它,显然没有。在删除它们之后,它现在似乎正在工作。谢谢大家!

标签: powershell


【解决方案1】:

您在这里遇到的问题是 PowerShell 试图从对象 $computer 获取由 $deviceinfile 定义的属性。然而$computer 只是一个没有$deviceinfile 属性的字符串。作为命名约定的一部分,这应该是一个后缀吗?

问题的简要说明

看下面的例子

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

$Computer.$deviceinfile

因为$computer 字符串对象上没有“Test”属性,所以什么也没返回。我认为您期望的结果是comp1.Test。现在看看这个例子。

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

我们创建了一个具有名为Test 的属性的对象。 “Bagel”是从该代码返回的内容。

您可以在代码中做什么

如果这是一个命名约定,您需要将其放在引号中以防止它被视为属性调用。这些变量将以您期望的方式扩展。这当然是假设您在调用它之前 定义了 $deviceinfile

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

否则,只需将其删除。

$computerSystem = get-wmiobject Win32_ComputerSystem -Computer $computer

【讨论】:

  • 谢谢! deviceinfile 是一个让它工作的实验——失败了,我以为我删除了它。但是,在删除它们之后,它现在似乎正在工作。抱歉,这是一个愚蠢的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多