【问题标题】:Filter and export data in .csv过滤和导出 .csv 中的数据
【发布时间】:2017-11-21 21:28:44
【问题描述】:

我需要一些指令来滚动服务器列表、远程编辑每个服务器的一些注册表项并将每个注册表项更新的输出保存在 .csv 文件中。 我尝试了我的脚本,它们似乎可以工作,但我无法过滤结果并仅(并且准确地)保存我在 .csv 中需要的内容:

脚本 1:

$Invocation = (Get-Variable MyInvocation -Scope 0).Value
$LocalDir = Split-Path $Invocation.MyCommand.Path
$Computers = Get-Content $LocalDir'\HostsList.txt'
$Results = foreach ($Computer in $Computers) {
    Invoke-Command -ComputerName $Computer -FilePath $LocalDir'\autologon_editor.ps1' -ArgumentList $Computer
}
$Results | Export-Csv -NoTypeInformation -Path $LocalDir'\ResultsAutologon.csv'

脚本 2:

Param(
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$true)]
    [string[]]$Computer
)
$Path = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
$Property1 = 'DefaultUserName'
$Property2 = 'DefaultPassword'
$Value1 = 'NewUserName-new4'
$Value2 = 'NewPassword-new4'
if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
    try {
        Set-ItemProperty -Path $Path -Name $Property1 -Value $Value1 -ErrorAction 'Stop'
        Set-ItemProperty -Path $Path -Name $Property2 -Value $Value2 -ErrorAction 'Stop'
        $Status = 'Username and password set ok'
    } catch {
        $Status = 'Username or password set ko'
    }
} else {   
    $Status = 'Unreachable'
}
Get-ItemProperty -Path $Path
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True
$Properties = @{            
    'Computer' = $Computer;
    'Status'   = $Status         
}
$Record = New-Object -TypeName PSObject -Property $Properties

问题

我的过滤器不起作用:.csv 文件不包含“计算机”和“状态”字段,但包含以下列表:

“Userinit”、“LegalNoticeText”、“Shell”、“LegalNoticeCaption”、“DebugServerCommand”、“ForceUnlockLogon”、“ReportBootOk”、“VMApplet”、“AutoRestartShell”、“PowerdownAfterShutdown”、“ShutdownWithoutLogon”、“Background” 、“PreloadFontFile”、“PasswordExpiryWarning”、“CachedLogonsCount”、“WinStationsDisabled”、“PreCreateKnownFolders”、“DisableCAD”、“scremoveoption”、“ShutdownFlags”、“AutoLogonSID”、“LastUsedUsername”、“DefaultUserName”、“DefaultPassword”、“ PSPath"、"PSParentPath"、"PSChildName"、"PSDrive"、"PSProvider"、"PSComputerName"、"RunspaceId"、"PSShowComputerName"

【问题讨论】:

    标签: powershell csv registry


    【解决方案1】:

    脚本 2(我假设它是 autologon_editor.ps1)当前没有输出 $Record。但是我怀疑它是返回此命令的输出:

    Get-ItemProperty -Path $Path
    

    我不确定是否需要。

    我建议删除上述命令,然后将脚本的结尾修改为:

    RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True
    $Properties = @{            
        'Computer'=$Computer;
        'Status'=$Status         
    }                                       
    New-Object -TypeName PSObject -Property $Properties
    

    这会将New-Object 命令(对象)的结果返回到管道,而管道又会使其成为Invoke-Command 的输出。

    【讨论】:

    • 它仍然不能完全工作;现在很多字段都没有出现,但除了“计算机”和“状态”之外,我在我的 csv 中找到了“PSComputerName”、“RunspaceId”和“PSShowComputerName”字段。
    • Invoke-Command 正在添加这些字段。如果您在多台计算机上运行,​​它们会很有帮助,因为您可以看到哪个结果来自哪台计算机。要排除那些您需要在 Invoke-Command 之后过滤结果的内容。例如:$Results | Select Computer,Status | Export-Csv ..
    • 太好了,现在它可以正常工作了!非常感谢!
    【解决方案2】:

    对于常见的解决方案,请参阅Union-Object for Not all properties displayed

    $Results | @987654323@ | Export-Csv -NoTypeInformation -Path $LocalDir'\ResultsAutologon.csv

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 2020-06-12
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      相关资源
      最近更新 更多