Powershell 以.csv格式筛选和导出数据
我需要一些说明来滚动服务器列表,远程编辑每个服务器的一些注册表项,并将每个注册表项更新的输出保存在.csv文件中。 我尝试了我的脚本,它们似乎可以正常工作,但我无法过滤结果,无法在.csv中只保存(准确地)我需要的内容: 脚本1:Powershell 以.csv格式筛选和导出数据,powershell,csv,registry,Powershell,Csv,Registry,我需要一些说明来滚动服务器列表,远程编辑每个服务器的一些注册表项,并将每个注册表项更新的输出保存在.csv文件中。 我尝试了我的脚本,它们似乎可以正常工作,但我无法过滤结果,无法在.csv中只保存(准确地)我需要的内容: 脚本1: $Invocation = (Get-Variable MyInvocation -Scope 0).Value $LocalDir = Split-Path $Invocation.MyCommand.Path $Computers = Get-Content $L
$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'
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
脚本2:
$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'
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”、“LegalNoticeSection”、“DebugServerCommand”、“ForceUnlockLogon”、“ReportBootOk”、“VMApplet”、“AutoRestartShell”、“关机后关机”、“未登录关机”、“后台”、“PreloFontFile”、“PasswordExpireyWarning”、“CachedLogonCount”、“WinStationsDisabled”、“PreCreateKnownFolders”、“DisableCAD”,“scremoveoption”、“ShutdownFlags”、“AutoLogonSID”、“LastUsedUsername”、“DefaultUserName”、“DefaultPassword”、“PSPath”、“PSParentPath”、“PSChildName”、“PSDrive”、“PSProvider”、“PSComputerName”、“RunspaceId”、“PSShowComputerName”
脚本2(我假设是Autogon_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
这将把新对象
命令(对象)的结果返回到管道,管道将反过来使其成为调用命令
脚本2的输出(我假设它是autogon\u 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
这将把
新对象
命令(对象)的结果返回到管道,这将使其成为调用命令
的输出。有关常见解决方案,请参阅:
$Results | |导出Csv-NoTypeInformation-路径$LocalDir'\resultsautogon.Csv
有关常见解决方案,请参阅:
$Results | | Export Csv-NoTypeInformation-Path$LocalDir'\resultsautogon.Csv
它仍然不能完全工作;许多字段现在没有出现,但除了“Computer”和“Status”之外,我还找到了“PSComputerName”、“RunspaceId”和“PSShowComputerName”“我的csv中的字段。这些字段是通过调用命令添加的。”。如果您在多台计算机上运行,它们会很有用,因为您可以看到哪个结果来自哪个计算机。要排除这些,您需要在Invoke命令之后过滤结果。例如:$Results |选择计算机,状态|导出Csv..
太好了,现在它可以正常工作了!非常感谢你!它仍然不能完全工作;许多字段现在没有出现,但除了“计算机”和“状态”之外,我在我的csv中还找到了“PSComputerName”、“RunspaceId”和“PSShowComputerName”字段。这些字段是通过调用命令添加的。如果您在多台计算机上运行,它们会很有用,因为您可以看到哪个结果来自哪个计算机。要排除这些,您需要在Invoke命令之后过滤结果。例如:$Results |选择计算机,状态|导出Csv..
太好了,现在它可以正常工作了!非常感谢你!