简体   繁体   English

Powershell将变量导出到Excel

[英]Powershell exporting variables to excel

Ok, so I've been given the task of learning powershell at work to write a few scripts. 好的,所以我已经得到了在工作中学习powershell的任务,以编写一些脚本。 I have some experience with other languages (just a little) like Java, Python, HTML, and CSS. 我对Java,Python,HTML和CSS等其他语言有一点经验。 Powershell is turning out to be a totally different beast, and so I'm not exactly sure how to go about doing things sometimes. Powershell变成了完全不同的野兽,因此我不确定到底该怎么做。 Right now I'm trying to figure out how to get my computer name, domain, username and datetime into an excel doc. 现在,我正在尝试弄清楚如何将我的计算机名称,域,用户名和日期时间转换为Excel文档。 Here's what I'm trying: 这是我正在尝试的:

$compname = $env:COMPUTERNAME | Select-Object @{Name='Computer Name'; Expression={$_.Computer_Name}}
$domain = $env:USERDNSDOMAIN | Select-Object @{Name='Domain'; Expression={$_.Domain}}
$user = $env:USERNAME | Select-Object @{Name='Username'; Expression={$_.Username}}
$date = Get-Date -format F | Select-Object @{Name='Date'; Expression={$_}}
$file = "$env:USERPROFILE\Desktop\test4.csv" 
$row = @($Date, $compname, $domain, $user)
$row | Export-Csv $file -NoType

I put my variables into an array hoping it would help, but no luck. 我将变量放入数组中,希望有帮助,但是没有运气。 Right now only the date is printing. 现在只有日期在打印。 The other variables are all empty and I have no idea why. 其他变量都是空的,我不知道为什么。 I can get them to work using Write-Out, but not exactly the way I want, and I'm assuming that Export-CSV is my best option. 我可以使用Write-Out来使它们工作,但不完全是我想要的方式,并且我认为Export-CSV是我的最佳选择。 Am I wrong in that assumption? 我在这个假设上错了吗?

When you pipe an array or a collection of objects ( @(1,2,3) is 3-element array for example), Export-Csv treats each object as a separate row. 当您通过管道传输对象数组或对象集合时(例如@(1,2,3)是3元素数组), Export-Csv将每个对象视为单独的行。

If you want all the values in different columns on the same row, you'll need to create an object with the properties you want in each column. 如果要在同一行的不同列中包含所有值,则需要在每个列中创建具有所需属性的对象。

You can create a custom object with the New-Object cmdlet and its -Property parameter. 您可以使用New-Object cmdlet及其-Property参数创建自定义对象。 Assign the properties to a hashtable and use that as the Property argument, like so: 将属性分配给哈希表,并将其用作Property参数,如下所示:

$Props = @{
    "Date" = Get-Date -Format f
    "Computer Name" = $env:COMPUTERNAME
    "Username" = $env:USERNAME
    "Domain" = $env:USERDNSDOMAIN
}

$MyObject = New-Object -TypeName psobject -Property $Props

Now, you can pipe the object to Export-Csv : 现在,您可以将对象通过管道传递到Export-Csv

$FilePath = "$env:USERPROFILE\Desktop\test4.csv" 

$MyObject | Export-Csv $FilePath -NoTypeInformation

If you want to change the order of the columns (or remove or add new ones on the fly), you can use Select-Object : 如果要更改列的顺序(或动态删除或添加新列),可以使用Select-Object

$MyObject | Select-Object 'Date','Domain','Username','Computer Name' | Export-Csv $FilePath -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM