Link to home
Start Free TrialLog in
Avatar of Ron Shorts
Ron ShortsFlag for United States of America

asked on

Powershell help - save log files to Teams or Sharepoint online site

Hi experts,

Is is possible to save output from a powershell script to a Microsoft Teams site, or a Sharepoint online site instead of saving locally?  

Below example I'm saving to C:\Temp\Outlog.csv and also emailing results.

$TestAdd = "TestGroup"
$TestRemove = "TestRemoveGroup"
$inputfile = "C:\samaccountname.csv"
$outfile = "C:\temp\OutLog.csv" 

$adGroupMembersAdd = Get-ADGroupMember -Identity $TestAdd | Select-Object -ExpandProperty distinguishedName
$adGroupMembersRemove = Get-ADGroupMember -Identity $TestRemove | Select-Object -ExpandProperty distinguishedName
$dtFormat = 'yyyy-MM-dd HH:mm:ss'
$results = Import-Csv -Path $inputfile | ForEach-Object {
	$adUser = Get-ADUser -Identity $_.SamAccountName
	$out = $adUser | Select-Object -Property UserPrincipalName, @{n='Time'; e={Get-Date -Format $dtFormat}}, @{n='Group'; e={$TestAdd}}, Action
	If ($adGroupMembersAdd -contains $adUser.distinguishedName) {
		Write-Host -ForegroundColor Cyan " $($adUser.UserPrincipalName) is already a member of $($TestAdd)"  
		$out.Action = 'Existing'
	} Else {
		Add-ADGroupMember -Identity $TestAdd -Members $adUser.distinguishedName 
		#Write-Host -ForegroundColor Green " $($_.SamAccountName) added to $($TestAdd) successfully"  #Add output by UPN
		Write-Host -ForegroundColor Green " $($adUser.UserPrincipalName) added to $($TestAdd) successfully"  
		$out.Action = 'Added'
	}
	$out
	$out = $adUser | Select-Object -Property UserPrincipalName, @{n='Time'; e={Get-Date -Format $dtFormat}}, @{n='Group'; e={$TestRemove}}, Action
	If ($adGroupMembersRemove -contains $adUser.distinguishedName) {
		Remove-ADGroupMember -Identity $TestRemove -Members $adUser.distinguishedName -Confirm:$false  
		Write-Host -ForegroundColor Magenta " $($adUser.UserPrincipalName) removed from $($TestRemove) successfully"  
		$out.Action = 'Removed'
	} Else {
		Write-Host -ForegroundColor Cyan " $($adUser.UserPrincipalName) is not a member of $($TestRemove)"  
		$out.Action = 'NoMember'
	}
	$out
}
$results | Export-Csv -NoTypeInformation -Path $outfile

$htmlHead = @'
<style>
	table	{border-width:1px; border-style:solid; border-color:black;}
	th		{border-width:1px; border-style:solid; border-color:black; padding:1px;}
	td		{border-width:1px; border-style:solid; border-color:black; padding:1px;}
</style>
'@
$html = $results | ConvertTo-Html -Head $htmlHead

$mailArgs = @{
	To = 'jdoe@acme.com'
	From = 'memberchange@acme.com'
	Subject = 'Group Member Change Result'
	Body = $html -join '<br />'
	SmtpServer = 'x.x.x.x'
	BodyAsHtml = $true
}

Send-MailMessage @mailArgs

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Harjit Dhaliwal
Harjit Dhaliwal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ron Shorts

ASKER

I was hoping this could be done without tenant admin privileges... maybe I'll look into flow to manage.
well Everything that is a log on O365 will require admin privileges, imagine what you could do if you can query logs on o365 without admin privileges... that request doesn't have any sense Ron
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Jose!
Glad to help