我正在使用 powershell 进行一些测试,以列出我域中每台计算机的硬件信息。我的脚本将从我使用 powershell 创建的文本文件中获取计算机列表。
$arrComputers = get-Content -Path "C:\powershell\List.txt"
foreach ($strComputer in $arrComputers)
{
$colItemsOs = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsOs)
{
Write-host "Computer Name: " $strComputer
Write-host "OsName: " $objItem.Name
Write-host "Version: " $objItem.Version
}
$colItemsCompsys = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsCompsys)
{
Write-host "Memory: " $objItem.TotalPhysicalMemory
Write-host "Manufacturer: " $objItem.Manufacturer
Write-host "Model: " $objItem.Model
Write-host "Domain: " $objItem.Domain
}
$colItemsSysEnc = get-wmiobject -class "Win32_SystemEnclosure" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsSysEnc)
{
Write-host "SerialNumber: " $objItem.SerialNumber
}
$colItemsProcessor = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsProcessor)
{
Write-host "Processor: " $objItem.Name
}
$colItemsIP = get-wmiobject -class "Win32_NetworkAdapterConfiguration " -namespace "root\CIMV2" `
-computername $strComputer -Filter "IpEnabled = TRUE"
foreach ($objItem in $colItemsIP)
{
Write-host "IPAddress: " $objItem.IpAddress
}
Write-host | Out-File c:\powershell\reportbeta.csv
}
但是,我无法这样做。我如何在 csv 文件中生成结果,以后可以使用 sqlcmd 将其上传到我的数据库。
我错过了什么?
谢谢你
我设法在文本文件中显示输出。但是,仅显示 1 条记录。
$arrComputers = get-Content -Path "C:\powershell\List.txt"
foreach ($strComputer in $arrComputers)
{
$colItemsOs = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsOs)
{
$strComputer
$Name =$objItem.Name
$Version = $objItem.Version
}
$colItemsCompsys = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsCompsys)
{
$TotalMemory = $objItem.TotalPhysicalMemory
$Manufacturer = $objItem.Manufacturer
$Model = $objItem.Model
$Domain = $objItem.Domain
}
$colItemsSysEnc = get-wmiobject -class "Win32_SystemEnclosure" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsSysEnc)
{
$SerialNUmber = $objItem.SerialNumber
}
$colItemsProcessor = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" `
-computername $strComputer
foreach ($objItem in $colItemsProcessor)
{
$Processor = $objItem.Name
}
$colItemsIP = get-wmiobject -class "Win32_NetworkAdapterConfiguration " -namespace "root\CIMV2" `
-computername $strComputer -Filter "IpEnabled = TRUE"
foreach ($objItem in $colItemsIP)
{
$IPV4 = $objItem.IpAddress
}
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $strComputer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Name
$OutputObj | Add-Member -MemberType NoteProperty -Name Version -Value $Version
$OutputObj | Add-Member -MemberType NoteProperty -Name TotalPhysicalMemory -Value $TotalMemory
$OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Manufacturer
$OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Model
$OutputObj | Add-Member -MemberType NoteProperty -Name Processor -Value $ProcessorSpeed
$OutputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $SerialNo
$OutputObj | Add-Member -MemberType NoteProperty -Name Domain -Value $Domain
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPV4
$OutputObj | Export-CSV -NoTypeInformation "c:\powershell\report.txt"
}
我现在没主意了。我应该把它做成数组吗?
请指教。
谢谢