【问题标题】:How to write all PowerShell screen output to .csv report file如何将所有 PowerShell 屏幕输出写入 .csv 报告文件
【发布时间】:2019-08-29 16:22:21
【问题描述】:

Fig1Fig2 虽然我知道这与许多其他问题类似,但是,我一直很难弄清楚如何使我在屏幕上看到的内容转到输出文件。我正在使用 PowerShell 版本 5.1.16299.1146。 Fig1 图像是我在 PS 屏幕上看到的。我希望脚本查看是否存在特定文件,如果它是 TRUE 或 FALSE,请将信息写入 .csv 文件。 Fig2 图像是实际写入 .csv 报告的内容。如果在特定计算机上每个用户的用户 AppDate 位置中找到计算机名称、结果 (TRUE/FALSE) 和用户 + LastWriteTime,我希望将其写入 .csv 文件。

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

【问题讨论】:

  • 请用经过处理的 text 替换这两个文本图像。图像难以阅读且难以与测试数据进行比较。
  • @Lee_Dailey 请解释净化后的文字
  • 净化文本是用演示数据替换您的私人/专有数据的文本。这样John Jacob Jinglehimer Smith 就变成了John Fakename,或者pc-nyc1-002 变成了pc-abc1-002。现实的,不重复的......但不是真实的。
  • @Lee_Dailey 图片被替换
  • 请重新阅读我对您问题的第一次回复的第一句。仔细注意第二句中的with sanitized _text_images are difficult to read ...。 [grin] ///// 不要发布文字图片,除非没有其他方法可以获取信息。

标签: windows powershell


【解决方案1】:

要读取其他用户文件夹,您需要 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

样本输出:

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

【讨论】:

  • 感谢您的帮助。这正是我需要的。假设我没有进行研究和测试,失败并重新开始对任何人都没有真正的帮助。我几乎每天都在尝试解决这个问题,并尝试了我能找到的一切。我知道脚本有效它只是不能产生我需要的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多