Good afternoon, I am very new to Powershell and am trying to achieve the following:
Loop through a folder directory
Set the folder name as a variable
Create a Task
Pass the variable (declared in Step 2) as the required parameter for the -File being called in the Task Action
Get-ChildItem -Path C:\Users\Paul\Documents\RSYNC -Directory -Recurse |ForEach-Object { $FolderName = $_.name $taskName = 'My Powershell Task_' + $FolderName # Create Action $Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File "C:\Users\Paul\Documents\RSYNC\Get-LatestAppLog.ps1" -name "$FolderName"' # Create Trigger $Trigger = New-ScheduledTaskTrigger -Daily -At 12:35am # Create Settings $Settings = New-ScheduledTaskSettingsSet # Create Task $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings # Register Task Register-ScheduledTask -TaskName $taskName -InputObject $Task -User 'username' -Password 'password' }
The tasks are created as desired, however, the problem is that inside the $Action step, instead of passing the folder name inside the $FolderName variable, it is simply passing $FolderName as a string (I hope that makes sense).
How can I correctly pass the folder name to the PowerShell script being called?