# Need help with logging within script

**URL:** <https://forums.powershell.org/t/need-help-with-logging-within-script/21483>\
**Category:** PowerShell Help\
**Created:** [January 26, 2023, 10:41pm UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483 "2023-01-26T22:41:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [January 26, 2023, 10:41pm UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483/1 "2023-01-26T22:41:28Z")

</div>

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

```auto
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

```auto
[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

```auto
[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 ...

```

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [January 27, 2023, 12:27pm UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483/2 "2023-01-27T12:27:01Z")

</div>

Hello,

You are overwriting your $result variable every time you are looping through.

```auto
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
        $Result = @()
        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"
}

```

I have not tested this, but this might work for you

---

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [January 27, 2023, 5:25pm UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483/3 "2023-01-27T17:25:41Z")

</div>

Unfortunately, that also has the same results of what I had before.

---

<div class="post-metadata">

**Author:** ![alex-innes](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/alex-innes/32/129_2.png) [@alex-innes](https://forums.powershell.org/u/alex-innes)\
**Post date:** [January 30, 2023, 9:30am UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483/4 "2023-01-30T09:30:14Z")

</div>

Ah sorry I didn’t read your output properly, its all going into one line rather than splitting over multiple lines. My apologies. It should be a fairly easy fix, at the end of your string output use a new line character:

```auto
$Result = "$(Get-TimeStamp) The Share $Share on server $using:Server has the everyone group with $Access access`n"

```

using the new line character:

```auto
`n 

```

It adds a new line to the end of your string, which should force it out over multiple lines.

---

<div class="post-metadata">

**Author:** ![tbingeman](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tbingeman/32/2122_2.png) [@tbingeman](https://forums.powershell.org/u/tbingeman)\
**Post date:** [January 30, 2023, 6:02pm UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483/5 "2023-01-30T18:02:48Z")

</div>

Thanks Alex, your suggestions have resolved my issue.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/need-help-with-logging-within-script/21483/6 "2024-05-16T20:16:03Z")

</div>


