Ok so I'm having a weird error with the following script:
$ErrorActionPreference = "silentlycontinue"
Try {
If (Test-Path \\Deploy\MyDocSize\live_docs_info.csv -PathType Leaf) {
$runscript = Get-ChildItem "C:\Users\$env:username\Documents" -Recurse | Measure-Object - property length -sum | Select @{Label="FolderSize";Expression={"{0:N2}" -f ($_.Sum / 1MB)}}, @{Label="Username";Expression={$env:username}} |
ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Out-File -Append -Encoding ascii -FilePath \\Deploy\MyDocSize\live_docs_info.csv -ErrorAction SilentlyContinue
}
Else {
$runscript = Get-ChildItem "C:\Users\$env:username\Documents" -Recurse | Measure-Object -property length -sum | Select @{Label="FolderSize";Expression={"{0:N2}" -f ($_.Sum / 1MB)}}, @{Label="Username";Expression={$env:username}} |
ConvertTo-Csv -NoTypeInformation | Select-Object | Out-File -Append -Encoding ascii -FilePath \\Deploy\MyDocSize\live_docs_info.csv
}
}
Finally {
If (Test-Path \\Deploy\MyDocSize\docs_info.csv -PathType Leaf) {
$runscript = Get-ChildItem "C:\Users\$env:username\Documents" -Recurse | Measure-Object -property length -sum | Select @{Label="FolderSize";Expression={"{0:N2}" -f ($_.Sum / 1MB)}}, @{Label="Username";Expression={$env:username}} |
ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Out-File -Append -Encoding ascii -FilePath \\Deploy\MyDocSize\docs_info.csv -ErrorAction SilentlyContinue
}
Else {
$runscript = Get-ChildItem "C:\Users\$env:username\Documents" -Recurse | Measure-Object -property length -sum | Select @{Label="FolderSize";Expression={"{0:N2}" -f ($_.Sum / 1MB)}}, @{Label="Username";Expression={$env:username}} |
ConvertTo-Csv -NoTypeInformation | Select-Object | Out-File -Append -Encoding ascii -FilePath \\Deploy\MyDocSize\docs_info.csv
}
}
This script takes the current username and size of that user's My Documents folder and exports it to a csv file. I have exported to two different csv files, and in case one is open when the script is run it will just keep running and write to the second one. In case both are open, it will not do anything.
Basically this script works for me, however when any other user tries to run it, it will not work. I suspect the $env:username is at fault but I'm not sure. Any help would be greatly appreciated!