I am trying to write a script that will provide a file with a list of file shares and what permissions the everyone group has in the share permissions. The main part of the script is working correctly but the logging is not working the way I want it. I am not sure how to go about getting the output to the preferred output below from the existing output also below. Any help would be greatly appreciated.
Script
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
$LogFolder = "E:\Automation\Results\Groups"
$Exists = Test-Path -Path $LogFolder
if ($Exists -eq "True") {
Write-Host "Directory Exists!"
} else {
New-Item -Path $LogFolder -ItemType directory
}
$logdate = (Get-Date -UFormat ".%a %m-%d-%Y %H-%M.%S %p")
$logs = new-item -path $LogFolder -Name "SMB Share Access Results$logdate.log"
Foreach ($Server in $ListOfServers){
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
Add-Content -PATH $Logs -Value "$(Get-TimeStamp) Connecting to server $Server ..."
#Invoke-Command -ComputerName $_ -ErrorAction SilentlyContinue -SessionOption (New-PSSessionOption -IncludePortInSPN) -ScriptBlock{
# Insert your code here
#}
$Results = Invoke-Command -ComputerName $Server -ScriptBlock{
function Get-TimeStamp {
return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}
$Logs = $using:Logs
$Shares = Get-SmbShare | Select-object name -expandproperty name
ForEach ($Share in $Shares){
$Permissions = Get-SmbShareAccess -name $Share | select-object AccountName,AccessRight
ForEach ($User in $Permissions){
if($user.AccountName -like 'Everyone'){
$Access = $user.AccessRight
Write-output "$(Get-TimeStamp) The Share $Share on server $using:Server has the everyone group with $Access access"
$Result = "$(Get-TimeStamp) The Share $Share on server $using:Server has the everyone group with $Access access"
}
}
}
Return $Result
}
write-output $Results
Add-Content -path $Logs -Value "$Results"
}
Current output in file
[01/26/23 14:21:04] Connecting to server Server1 ...
[01/26/23 14:21:09] The Share ShareEnterprise on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Shareroot on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Share_Scratch_Drive on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Share3 on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Share4 on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Share5 on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Share6 on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share Share2 on server Server1 has the everyone group with Full access [01/26/23 14:21:09] The Share print$ on server Server1 has the everyone group with Read access
[01/26/23 14:21:09] Connecting to server Server09 ...
Preferred output
[01/26/23 14:21:04] Connecting to server Server1 ...
[01/26/23 14:21:09] The Share ShareEnterprise on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Shareroot on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Share_Scratch_Drive on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Share3 on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Share4 on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Share5 on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Share6 on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share Share2 on server Server1 has the everyone group with Full access
[01/26/23 14:21:09] The Share print$ on server Server1 has the everyone group with Read access
[01/26/23 14:21:09] Connecting to server Server09 ...