[英]Powershell output to text/csv
I have a script that runs and outputs ok. 我有一个可以运行并输出正常的脚本。 But I'm having trouble trying to get the output into a .txt or .csv file.
但是我在尝试将输出转换为.txt或.csv文件时遇到麻烦。
I used to have a "format-table -auto" on the end, and I tried piping that to the file. 我过去常常在最后使用“ format-table -auto”,然后尝试将其管道传输到文件。 But I found you cant pipe the "format-table output".
但是我发现您无法通过管道传递“格式表输出”。 I'm thinking iI'm m overlooking something.
我在想我正在俯视某事。 Any help outputting this to a text file would be appreciated.
任何帮助输出到文本文件的帮助,将不胜感激。
# Read all the computers from the file
$computers = get-content C:\Users\Administrator\Desktop\Newfolder\new\input.txt
# Perform an operation for each row in the file
foreach ($strComputer in $computers){
#pulling product key from inside pc
Get-CimInstance -ClassName SoftwareLicensingProduct -computer $_.Name|
where PartialProductKey |
#labeling table
select @{N='Computer';E={$strComputer}},Name,LicenseStatus
}
#output table (1) is good
Write-Output @{N='Computer';E={$strComputer}},Name,LicenseStatus | Export-Csv -Path C:\Users\Administrator\Desktop\Newfolder\new\output.csv
your code has ... many logical problems. 您的代码有很多逻辑问题。 see my comments for more info.
查看我的评论以获取更多信息。 i decided to simply rewrite it.
我决定简单地重写它。 [ grin ]
[ 咧嘴 ]
this one sets up the source & destination info, what to use when a target system fails to respond, and creates the system list file to work with. 这将设置源和目标信息,在目标系统无法响应时使用该信息,并创建要使用的系统列表文件。
next, it reads the list in, iterates thru that list, uses try/catch
to handle non-responding systems, calls the CIM class, filters it, and finally hands it off to the $Results
collection. 接下来,它读入列表,遍历该列表,使用
try/catch
处理无响应的系统,调用CIM类,对其进行过滤,最后将其交给$Results
集合。
after all that, the $Results
collection is displayed on screen and then sent to a CSV. 毕竟,
$Results
集合显示在屏幕上,然后发送到CSV。
$SourceDir = $env:TEMP
$SourceFile = 'ComputerList.txt'
$FullSourceFile = Join-Path -Path $SourceDir -ChildPath $SourceFile
$DestDir = $SourceDir
$TimeStamp = Get-Date -Format 'yyyy-MM-dd'
$DestFile = "ComputerLicenseInfo_$TimeStamp.csv"
$FullDestFile = Join-Path -Path $DestDir -ChildPath $DestFile
$NoResponse = '__NoResponse__'
#region >>> create a file to work with
# remove this region when ready to work with other sample data OR real data
@'
LocalHost
10.0.0.1
127.0.0.1
BetterNotBeThere
'@ | Set-Content -LiteralPath $FullSourceFile
#endregion >>> create a file to work with
$ComputerList = Get-Content -LiteralPath $FullSourceFile
$Results = foreach ($CL_Item in $ComputerList)
{
try
{
Get-CimInstance -ClassName SoftwareLicensingProduct -ComputerName $CL_Item -ErrorAction Stop |
Where-Object {
$_.PartialProductKey
} |
ForEach-Object {
[PSCustomObject]@{
ComputerName = $_.GetCimSessionComputerName()
Name = $_.Name
LicenseStatus = $_.LicenseStatus
}
}
}
catch
{
[PSCustomObject]@{
ComputerName = $CL_Item
Name = $NoResponse
LicenseStatus = $NoResponse
}
}
} # end = foreach ($CL_Item in $ComputerList)
# show on screen
$Results
#send to CSV
$Results |
Export-Csv -LiteralPath $FullDestFile -NoTypeInformation
screen output ... 屏幕输出...
ComputerName Name LicenseStatus
------------ ---- -------------
LocalHost Windows(R) 7, Professional edition 1
10.0.0.1 __NoResponse__ __NoResponse__
127.0.0.1 Windows(R) 7, Professional edition 1
BetterNotBeThere __NoResponse__ __NoResponse__
CSV file content ... CSV档案内容...
"ComputerName","Name","LicenseStatus"
"LocalHost","Windows(R) 7, Professional edition","1"
"10.0.0.1","__NoResponse__","__NoResponse__"
"127.0.0.1","Windows(R) 7, Professional edition","1"
"BetterNotBeThere","__NoResponse__","__NoResponse__"
Fixed this a little bit, try this out: 修正了一点,尝试一下:
$computers = get-content C:\Users\Administrator\Desktop\Newfolder\new\input.txt
$Output = @()
foreach ($strComputer in $computers) {
$CimData = Get-CimInstance -ClassName SoftwareLicensingProduct -computer $strComputer | where PartialProductKey
if ($CimData) {
$CimData | % {
$Row = "" | Select Computer,Name,LicenseStatus
$Row.Computer = $strComputer
$Row.Name = $_.Name
$Row.LicenseStatus = $_.LicenseStatus
$Output += $Row
}
}
}
$Output | Export-Csv -Path C:\Users\Administrator\Desktop\Newfolder\new\output.csv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.