具有新行的库存出库文件

时间:2019-06-03 14:44:25

标签: windows powershell

我有一个清单Powershell脚本,试图将其输出到csv中。我的目标是在脚本的各个列中的下一行中输出脚本的每次执行。

我尝试了Export-Csv,但是由于我正在使用变量,因此csv会显示(即猜测)元数据。


    $ComputerName =  Get-WmiObject Win32_OperatingSystem  | select -ExpandProperty CSName
    $OS_Name = Get-WmiObject Win32_OperatingSystem  | Select-Object -ExpandProperty Caption 
    $OS_Architecture = Get-WmiObject Win32_OperatingSystem  | select -ExpandProperty OSArchitecture
    $System_Manufacturer = Get-WmiObject win32_computersystem  | select -ExpandProperty Manufacturer
    $Model = Get-WmiObject win32_computersystem  | select -ExpandProperty Model
    $CPU_Manufacturer = Get-WmiObject Win32_Processor  | select -ExpandProperty Name
    $Disk_Size_GB = Get-WmiObject win32_diskDrive  | Measure-Object -Property Size -Sum | % {[math]::round(($_.sum /1GB),2)}
    $Physical_Memory_GB = Get-WMIObject -class Win32_PhysicalMemory  | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 1GB),2)}
    $Version=(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
    $InstallDate= systeminfo | find /I “Install Date”
    $Assettag=(Get-WmiObject -Class Win32_SystemEnclosure | Select-Object SMBiosAssetTag).SMBiosAssetTag 
    $SerialNumber =  (Get-WmiObject -Class Win32_BIOS | Select-Object SerialNumber).SerialNumber


  ($Assettag, $ComputerName, $System_Manufacturer, $Model, $OS_Name, $Version, $SerialNumber | Format-Table | Out-File C:\Users\1\Desktop\Newinvent.csv  )

我希望每一行都充满每次执行 谢谢

3 个答案:

答案 0 :(得分:1)

如果要使用Export-Csv,则需要具有其属性包含要导出的名称和值的对象。一种实现方法是使用哈希表中定义的所有属性创建一个[PSCustomObject]。您可以将该自定义对象传递给Export-Csv命令。

[PSCustomObject]@{"ComputerName" = $ComputerName
        "OS_Name" = $OS_Name
        "OS_Architecture" = $OS_Architecture
        "System_Manufacturer" = $System_Manufacturer
        "Model" = $Model
        "CPU_Manufacturer" = $CPU_Manufacturer
        "Disk_Size_GB" = $Disk_Size_GB
        "Physical_Memory_GB" = $Physical_Memory_GB
        "Version" = $Version
        "InstallDate" = $InstallDate
        "Assettag" = $Assettag
        "SerialNumber" = $SerialNumber
} | Export-Csv -Path file.csv -NoTypeInformation -Append

答案 1 :(得分:1)

并尝试查看Get-ComputerInfo的结果-在那里满足您的大多数需求。

答案 2 :(得分:0)

我要避免浪费时间的li:before { content: "0"; } li:first-child:before { content: "000"; } ul { width:100px; list-style: none; }

Systeminfo

以及> (Measure-Command {$systeminfo=(systeminfo) 2>$NULL}).Totalseconds 3,0428012

Get-ComputerInfo

并使用脚本的优化版本,该版本可从注册表获取/转换InstallDate:

$OldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
(Measure-Command {$ComputerInfo = Get-ComputerInfo}).Totalseconds
3,1837208
$ProgressPreference = $OldProgressPreference

> (Measure-Command{Q:\Test\2019\06\03\SO_56429703.ps1}).TotalSeconds
1,635074
Get-Content file.csv