标题说明了一切。我有一个有效的PS代码,希望将结果输出到文本或excel文件。
$computers = get-content
"C:\Users\Administrator\Desktop\David\FINAL\RamCheck\input.txt"
Perform an operation for each row in the file
foreach ($strComputer in $computers) {
$colSlots = Get-WmiObject -Class "win32_PhysicalMemoryArray" -namespace
"root\CIMV2" -computerName $strComputer
write-host ""
$colSlots = Get-WmiObject -Class "win32_PhysicalMemoryArray" -namespace
"root\CIMV2" -computerName $strComputer
$colRAM = Get-WmiObject -Class "win32_PhysicalMemory" -namespace
"root\CIMV2" -
computerName $strComputer
$NumSlots = 0
$SlotsFilled = 0
$TotMemPopulated = 0
$colRAM | ForEach {
“Memory Installed: ” + $_.DeviceLocator
“Memory Size: ” + ($_.Capacity / 1GB) + ” GB”
$TotMemPopulated = $TotMemPopulated + ($_.Capacity / 1GB)
if ($_.Capacity = 0)
{write-host "found free slot"}
write-host ""
write-host "=== Summary Memory Slot Info for computer:" $strComputer "==="
write-host "Total Memory Populated = " $TotMemPopulated "GB"
我试过用
export-csv -append $fileOutput -Encoding utf8
Export-Csv
不会以一种好的方式工作,因为你的输入并不意味着在表格中表示。您生成的文本字符串具有只对人类可理解的格式。
如果您想将其作为CSV导出,则必须以可以这样做的方式更改输出。例如,您可以输出带有值的数组并跳过文本。
要输出文本,你可以使用像Out-File
这样的东西。
Out-File -Encoding utf8 -InputObject $fileOutput
您的脚本总体上是一团糟,使用错误类型的引号,缺少右括号等。就像现在一样,它根本不应该起作用。
对于您正在检查的每台PC,您可以构造一个对象并将该对象添加到一个数组,然后将该数组导出为CSV。这是较旧的示例,但这使用较新的PSCustomObject,这很好地学习如何使用。
#Create an array
$Array = @()
#Loop through all your computers again (or add to an existing loop)
foreach ($strComputer in $computers)
{
$compresults = New-Object -TypeName [PSCustomObject]@{
Computer = $strComputer
Memory = $TotMemPopulated
SlotsFilled = $SlotsFilled
}
$array += $compresults
}
#Export the array to CSV
$Array | export-csv $fileOutput -Encoding UTF8