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?
 
oBdAConnect With a Mentor Commented:
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
 
Darrell PorterConnect With a Mentor Enterprise 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.