I need a few instructions to scroll a list of servers, remotely edit some registry keys for each server and save the output of each registry key update in a .csv file. I tried my scripts and they seem to work but I'm not able to filter the results and save only (and exactly) what I need in the .csv:
Script 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'
Script 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
Issue
My filter is not working: the .csv does not contain the fields "Computer" and "Status", but the following list:
"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"
Script 2 (which I assume is autologon_editor.ps1) is not currently outputting $Record
. However I suspect it is instead returning the output from this command:
Get-ItemProperty -Path $Path
Which I'm not sure is needed.
I suggest removing the above command and then modifying the end of your script to this:
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True
$Properties = @{
'Computer'=$Computer;
'Status'=$Status
}
New-Object -TypeName PSObject -Property $Properties
This will return the result of the New-Object
command (the object) to the pipeline, which will in turn make it the output of the Invoke-Command
.