我对Powershell脚本还很陌生,并且一直在玩在线查找的脚本,该脚本查询域中的所有系统并以CSV格式输出一些硬件信息。脚本:
$testcomputers = Get-Content -Path 'C:\scripts\computers.txt'
$exportLocation = 'C:\scripts\pcInventory.csv'
foreach ($computer in $testcomputers) {
if (Test-Connection -ComputerName $computer -Quiet -count 2){
Add-Content -value $computer -path c:\scripts\livePCs.txt
}else{
Add-Content -value $computer -path c:\scripts\deadPCs.txt
}
}
$computers = Get-Content -Path 'C:\scripts\livePCs.txt'
foreach ($computer in $computers) {
$Bios = Win32_computerSystem -Computername $Computer
$Sysbuild = Get-WmiObGet-WmiObject win32_bios -Computername $Computer
$Hardware = Get-WmiObjectject Win32_WmiSetting -Computername $Computer
$OS = Get-WmiObject Win32_OperatingSystem -Computername $Computer
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | Where-Object {$_.IPEnabled}
$driveSpace = Get-WmiObject win32_volume -computername $Computer -Filter 'drivetype = 3' |
Select-Object PScomputerName, driveletter, label, @{LABEL='GBfreespace';EXPRESSION={'{0:N2}' -f($_.freespace/1GB)} } |
Where-Object { $_.driveletter -match 'C:' }
$cpu = Get-WmiObject Win32_Processor -computername $computer
$username = Get-ChildItem "\\$computer\c$\Users" | Sort-Object LastWriteTime -Descending | Select-Object Name, LastWriteTime -first 1
$totalMemory = [math]::round($Hardware.TotalPhysicalMemory/1024/1024/1024, 2)
$lastBoot = $OS.ConvertToDateTime($OS.LastBootUpTime)
$IPAddress = $Networks.IpAddress[0]
$MACAddress = $Networks.MACAddress
$systemBios = $Bios.serialnumber
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Hardware.Manufacturer
$OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Hardware.Model
$OutputObj | Add-Member -MemberType NoteProperty -Name Processor_Type -Value $cpu.Name
$OutputObj | Add-Member -MemberType NoteProperty -Name System_Type -Value $Hardware.SystemType
$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value $OS.Caption
$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System_Version -Value $OS.version
$OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System_BuildVersion -Value $SysBuild.BuildVersion
$OutputObj | Add-Member -MemberType NoteProperty -Name Serial_Number -Value $systemBios
$OutputObj | Add-Member -MemberType NoteProperty -Name IP_Address -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name MAC_Address -Value $MACAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name Last_User -Value $username.Name
$OutputObj | Add-Member -MemberType NoteProperty -Name User_Last_Login -Value $username.LastWriteTime
$OutputObj | Add-Member -MemberType NoteProperty -Name C:_FreeSpace_GB -Value $driveSpace.GBfreespace
$OutputObj | Add-Member -MemberType NoteProperty -Name Total_Memory_GB -Value $totalMemory
$OutputObj | Add-Member -MemberType NoteProperty -Name Last_ReBoot -Value $lastboot
$OutputObj | Export-Csv $exportLocation -Append -NoTypeInformation
}
运行脚本时,出现以下错误,但不确定如何解决。我还注意到应该输出系统RAM的行始终输出0。任何指导将不胜感激。如果有任何区别,我将在Windows 7上运行Powershell 3。
Win32_computerSystem : The term 'Win32_computerSystem' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
Get-WmiObGet-WmiObject : The term 'Get-WmiObGet-WmiObject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a
path was included, verify that the path is correct and try again.
Get-WmiObjectject : The term 'Get-WmiObjectject' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
答案 0 :(得分:1)
第15行:
$Bios = Win32_computerSystem -Computername $Computer
您忘记为Win32_ComputerSystem
加上Get-WmiObject
前缀。它试图作为命令运行Win32_ComputerSystem
,而不是评估WMI类。要解决此问题,请将行更改为如下所示:
$Bios = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer
第16行:
$Sysbuild = Get-WmiObGet-WmiObject win32_bios -Computername $Computer
没有名为Get-WmiObGet-WmiObject
的cmdlet。将其更改为Get-WmiObject
:
$Sysbuild = Get-WmiObject Win32_Bios -ComputerName $Computer
第17行:
$Hardware = Get-WmiObjectject Win32_WmiSetting -Computername $Computer
这是尝试呼叫Get-WmiObject
时的另一种错字。纠正拼写错误,它应该可以工作:
$Hardware = Get-WmiObject Win32_WmiSetting -ComputerName $Computer
简而言之,设置$Hardware
时使用的WMI类错误。您可以从Win32_PhysicalMemory
类中获取物理内存信息。将第17行替换为:
$Hardware = Get-WmiObject Win32_PhysicalMemory -ComputerName $Computer
,当您获得$ totalRam时,可以使用以下计算:
$totalRamBytes = 0
$Hardware.Capacity | Foreach-Object { $totalRamBytes += $_ }
$totalRamGb = $totalRamBytes / 1GB