We're trying to collect the mapped drives of a user that is logged on to a Windows 7 client. To do this we need to create a scheduled task and have it run as that user. This works fine but the problem is on retrieving the data from the scheduled task.
Code
Invoke-Command -ScriptBlock {
$User = 'John'
$Script = 'C:\Users\' + $User + '\AppData\Local\Temp' + '\Script.ps1'
$File = 'C:\Users\' + $User + '\AppData\Local\Temp' + '\Data.txt'
$Code = {
$User = 'John'
$File = 'C:\Users\' + $User + '\AppData\Local\Temp' + '\Data.txt'
Get-WmiObject -Class win32_mappedlogicaldisk | Select-Object Name, ProviderName |
Export-Csv $File -Encoding UTF8 -NoTypeInformation
}
$Code | Set-Content $Script -Encoding utf8
schtasks /create /RL HIGHEST /SC ONCE /ST 23:00 /TN "Test" /TR "powershell.exe -ExecutionPolicy Bypass -File '$Script'" /RU "$env:USERDNSDOMAIN\$User"
schtasks /run /TN "Test"
schtasks /delete /F /TN "Test"
for ($i = 0; $i -le 5; $i++) {
if (Test-Path $File) {
Import-Csv $File
Break
}
else {
Start-Sleep -Seconds 1
}
}
} -ComputerName $Computer
The problem seems to be retrieving the Data.txt from the users $ENV:Temp folder. It seems like a bit of a repetitive thing, is there not a cleaner way of doing this?
Thank you for your help.