Write-host - export-csv

Hello .,

Below scripts works fine , post execution of the script we need output in csv format with the following detail - Is the User has added to local administrator group  or not , `Output result would be passed or failed for entry .

Thanks in advance

GC C:\temp\input.txt | %{
$comp = $_
$domain="domainname"
$group = "groupname"
  Try{
    ([adsi]"WinNT://$comp/Administrators,group").Add("WinNT://$domain/$group,group")
   }
  Catch{
    Write-Host "Unable to add $group to Server $comp - Error: $($_.exception.Message.Trim())" -F Red
   }
}

Open in new window

LVL 3
SAM ITAD windows Admin Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darrell PorterEnterprise Business Process ArchitectCommented:
GC C:\temp\input.txt | %{
function ExitWithCode 
{ 
    param 
    ( 
        $exitcode 
    )

    $host.SetShouldExit($exitcode) 
    exit 
}
$comp = $_
$domain="domainname"
$group = "groupname"
  Try{
    ([adsi]"WinNT://$comp/Administrators,group").Add("WinNT://$domain/$group,group")
   }
  Catch{
    Write-Host "Unable to add $group to Server $comp - Error: $($_.exception.Message.Trim())" -F Red
    -join('"',$group,'","Failed"') | Out-File AddGroup.Log - Append
    ExitWithCode 12345 # Set whatever errorlevel code you desire
   }
  Finally {
         -join('"',$group,'","Passed"') | Out-File AddGroup.Log - Append
  }
}

Open in new window


Something like this?
0
SAM ITAD windows Admin  Author Commented:
Yes.... let me execute and check the script
0
SAM ITAD windows Admin  Author Commented:
Getting below error post script execution

______________________________________________-

Out-File : Cannot validate argument on parameter 'Encoding'. The argument "-" does not belong to the set
"unknown,string,unicode,bigendianunicode,utf8,utf7,utf32,ascii,default,oem" specified by the ValidateSet attribute. Supply an argument that is in the set
and then try the command again.
At line:24 char:65
+          -join('"',$group,'","Passed"') | Out-File AddGroup.Log - Append
+                                                                 ~
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.OutFileCommand
0
oBdACommented:
Try it like this:
Get-Content -Path C:\temp\input.txt | ForEach-Object {
	$out = New-Object -TypeName PSObject -Property @{
		'ComputerName' = $_
		'Domain' = 'domainname'
		'Group' = 'groupname'
		'Result' = 'PASSED'
	}
	Try{
		([adsi]"WinNT://$($out.ComputerName)/Administrators,group").Add("WinNT://$($out.Domain)/$($out.Group),group")
	} Catch {
		Write-Host "Unable to add $group to Server $comp - Error: $($_.exception.Message.Trim())" -ForegroundColor Red
		$out.Result = "FAILED ($($_.Exception.Message.Trim()))"
	}
	$out
} | Export-Csv -NoTypeInformation -Path C:\Temp\output.csv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.