Write-host - export-csv

SAM IT
SAM IT used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Darrell PorterEnterprise Business Process Architect
Commented:
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?
SAM ITAD windows Admin  

Author

Commented:
Yes.... let me execute and check the script
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
Most Valuable Expert 2018
Distinguished Expert 2018
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial