This is an automated archive.
The original was posted on /r/sysadmin by /u/Intelligent_Ad3362 on 2024-01-23 14:52:46+00:00.
Hello Reddit Community,
I hope you're all doing well. I'm currently facing the challenge of running a PowerShell script via Task Scheduler and wondering what the minimum permissions required for successful execution are.
So far, I've only been able to execute the script successfully as a domain administrator. However, I'd like to grant only the essential permissions to adhere to security policies. I'm seeking information on the specific permissions needed to run the script properly.
The script involves actions such as gathering information on Remote Desktop Sessions. Could you please share insights on the exact permissions required for Task Scheduler to successfully run the script without needing extensive domain administrator rights?
Thank you in advance for your assistance!
# Verification
$Date = (Get-Date).AddMonths(-1).ToString('MM.yyyy')
if (-Not (Test-Path "C:\Scripts\RDS\Archive\RDS $Date.csv")) {
if ((Get-Date).Day -eq '1') {
Move-Item -Path "$pathForCsv\RDS.csv" -Destination "C:\Scripts\RDS\Archive\RDS $Date.csv"
Move-Item -Path "$pathForCsv\RDSNamen.csv" -Destination "C:\Scripts\RDS\Archive\RDSNamen $Date.csv"
}
}
Get our FQDN
$ourname = "$env:COMPUTERNAME.$env:USERDNSDOMAIN".ToLower()
FQDN of the Active Management Server
$rdsMgmtServer = (Get-RDConnectionBrokerHighAvailability | Select-Object -ExpandProperty ActiveManagementServer).ToLower()
$rdsMgmtServer
if (-Not ($ourname.Equals($rdsMgmtServer))) {
Write-Host 'Error: The Remote Desktop Services deployment is not present on "cb1.company.co.at". This operation can be performed after creating a deployment.'
exit
}
Array with all collections to query
$collectionArray = @("Collection1")
Get current date and time
$currentDatetime = (Get-Date).ToString('dd.MM.yyyy HH:mm:ss')
Path for CSV
$pathForCsv = 'C:\Scripts\RDS'
Actual CSV names
$csvCount = "$pathForCsv\RDS.csv"
$csvNames = "$pathForCsv\RDSNamen.csv"
foreach ($collectionName in $collectionArray) {
List of all currently logged-in users
$rdsUserList = (Get-RDUserSession -CollectionName $collectionName -ConnectionBroker $rdsMgmtServer).UserName
Number of currently logged-in users
$rdsUserCount = $rdsUserList.Count
# We now save individual usernames to the $csvNames file
foreach ($username in $rdsUserList) {
$namesText = "$collectionName`t$currentDatetime`t$username"
$namesText | Add-Content -Path $csvNames -Encoding UTF8 -Force
}
# Composition of the CSV file `t indicates tab
$countText = "$collectionName`t$currentDatetime`t$rdsUserCount"
$countText | Add-Content -Path $csvCount -Encoding UTF8 -Force
}