简体   繁体   English

如何将所有PowerShell屏幕输出写入.csv报告文件

[英]How to write all PowerShell screen output to .csv report file

Fig1 Fig2 While I know this is a similar to many other questions regarding this, however, I have been having a difficult time figuring out how to make what I see on the screen go to the output file. 图1图 2虽然我知道这与许多其他问题类似,但是,我一直很难确定如何使屏幕上显示的内容进入输出文件。 I'm using PowerShell Version 5.1.16299.1146. 我正在使用PowerShell版本5.1.16299.1146。 Fig1 image is what I see on the PS screen. 图1是我在PS屏幕上看到的图像。 I want the script to see if a particular file is present and if it is TRUE or FALSE, write the information to the .csv file. 我希望脚本查看是否存在特定文件,并且该文件为TRUE或FALSE,然后将信息写入.csv文件。 Fig2 image is what actually gets written to the .csv report. Fig2图像是实际写入.csv报告中的图像。 I want the computer Name, Results (TRUE/FALSE), and Users + LastWriteTime written to the .csv file if it is found in the user's AppDate location for each user on a particular machine. 我希望将计算机名称,结果(TRUE / FALSE)和用户+ LastWriteTime写入.csv文件(如果在特定计算机上的每个用户的用户AppDate位置中找到该文件)。

Set-ExecutionPolicy Bypass
$javausers = @()
$env:COMPUTERNAME = HostName

$TestPath = "$env:userprofile\AppData\LocalLow\Sun\Java\Deployment\deployment.properties"
$TestResult = if ( $(Try { Test-Path $TestPath.trim() } Catch { $false }) ) { Write-Output "True - deployment.properties" } Else { Write-Output "False - Path not found" }

$users = Get-ChildItem c:\users

foreach ($user in $users)
{

$folder = "C:\users\" + $user + "$env:userprofile\AppData\LocalLow\Sun\Java\Deployment\deployment.properties" 

if ( $(Try { Test-Path $TestPath.trim() } Catch { $false }) ) { Write-Output "True - deployment.properties" $users -join ','} Else { Write-Output "False - Path not found" $users-join ','}

}

$javauser = New-Object System.Object
$javauser | Add-Member -MemberType NoteProperty -Name "Computer Name" -Value $env:COMPUTERNAME
#$javauser | Add-Member -MemberType NoteProperty -Name "Java User" -Value $TestPath
$javauser | Add-Member -MemberType NoteProperty -Name "Results" -Value $TestResult
$javauser | Add-Member -MemberType NoteProperty -Name "Users" -Value $folder
#$javauser | Add-Member -MemberType NoteProperty -Name "Users" -Value $users

$javausers += $javauser

$javausers | Export-Csv -NoTypeInformation -Path "C:\Temp\JavaUsersList.csv" -Append

To read other users folders you'll need to RunsAsAdmin. 要读取其他用户的文件夹,您需要RunsAsAdmin。

#Requires -RunAsAdministrator
## Q:\Test\2019\08\29\SO_57714265.ps1

Set-ExecutionPolicy Bypass

$env:COMPUTERNAME = HostName

$DeplPath = "AppData\LocalLow\Sun\Java\Deployment\deployment.properties"

$javausers = foreach ($User in Get-ChildItem C:\Users -Directory){
    $folder = Join-Path $User.FullName $DeplPath
    if (Test-Path $folder) {
        $TestResult = "True  - deployment.properties"
    } Else {
        $TestResult = "False - Path not found"
    }
    [PSCustomObject]@{
        "Computer Name" = $env:COMPUTERNAME
        "Results"       = $TestResult
        "Users"         = $user.Name
    }
}
$javausers

#$javausers | Export-Csv -NoTypeInformation -Path "C:\Temp\JavaUsersList.csv" -Append

Sample output: 样本输出:

Computer Name Results                       Users
------------- -------                       -----
VBoxWin10     False - Path not found        SomeOne
VBoxWin10     False - Path not found        Public
VBoxWin10     True  - deployment.properties LotPings

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

相关问题 如何从.CSV单元格使用Powershell写入.cmd文件的新行 - How to write to new line of .cmd file with Powershell from .CSV cell 将powershell的输出保存在CSV文件中 - Save the powershell's output in a CSV file 如何在 Powershell 中换行写 output - How to write output with new lines in Powershell Windows Powershell:如何比较两个 CSV 文件和 output 仅在其中一个文件中但不在两个文件中的行 - Windows Powershell: How to compare two CSV files and output the rows that are just in either of the file but not in both 如何将目录中的所有* .txt文件写入一个文件中,并在输出文件的每一行中包含源文件名? - How to write all *.txt files in a directory into one file with source file name in each line in output file? 如何将 output 日期和时间从 powershell 到文件 - how to output date and time from powershell to a file 使用powershell输出多个.csv文件附加.csv文件名作为源文件夹名称 - Output multiple .csv files appending the .csv file name as the source folder name with powershell 如何通过Streamwriter将信息写入.csv文件 - How to write information to .csv file via streamwriter 如何使用 Powershell 或 Batch 将 csv 中的所有值相乘? - How can I multiply all values in a csv using Powershell or Batch? 如何将屏幕 output 保存到文本文件中 - How to save screen output into a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM