gpt4 book ai didi

powershell - 无法使PowerShell代码正常工作

转载 作者:行者123 更新时间:2023-12-02 23:12:45 27 4
gpt4 key购买 nike

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

最佳答案

您在这里遇到的问题是PowerShell试图从对象$deviceinfile获取$computer定义的属性。但是$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

关于powershell - 无法使PowerShell代码正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com