3

I would like to use Powershell to create a couple of scheduled tasks, on a server. I have created the file from existing schedule's

I have loaded up the csv file, piped it to a select, and retreived all the info that I require from the csv file. However I am not sure on how to pass these results on to a external non powershell command.

Import-Csv .\listoftasks.csv | Select 'Run As User','RP','Scheduled Type','TaskName','Task To Run','Repeat: Every','Repeat: Until: Duration' 

What I would like to do is something like:

Import-Csv .\listoftasks.csv | Select 'Run As User','RP','Scheduled Type','TaskName','Task To Run','Repeat: Every','Repeat: Until: Duration' | schtasks /create /RU ....

2 Answers 2

4

Try using ForEach-Object:

Import-Csv .\listoftasks.csv | 
Select-Object 'Run As User','RP','Scheduled Type','TaskName','Task To Run','Repeat: Every','Repeat: Until: Duration' | 
ForEach-Object { Invoke-Expression "schtasks /create /RU ..." }

Also, be wary of the properties whose names contain spaces. You can access them like so if the name contains a space:

$_.'Run As User'
Sign up to request clarification or add additional context in comments.

Comments

4

Posting the end result of the advice I received, hopefully somebody else can use it as well. :)

#Added a column to the csv with heading RP - contains the password if you have to use
#hardcoded account \ passwords and cant use the system accounts
$ListOfTasks = Import-Csv ListOfTasksExport.csv |   Select 
                                                   'Run As User' `
                                                ,'RP' `
                                                ,'Scheduled Type' `
                                                ,'TaskName' `
                                                ,'Task To Run' `
                                                ,'Repeat: Every' `
                                                ,'Repeat: Until: Duration' `
                                                ,'Start Time' `
                                                , 'HostName' 

ForEach($item in $ListOfTasks)
{
    $dateOfItem = [DateTime]::Parse($item.'Start Time')
    $stringOfDateOfItem = $dateOfItem.ToString("HH:mm")
    $tr = $item.'Task To Run'
    $user = $item.'Run as User'
    $UserPassword = $item.'RP'
    $scheduledType = $item.'Scheduled Type'
    $taskName = $item.'TaskName'
    $taskToRun = $item.'Task To Run'
    $hostName = $item.'HostName'
    $serviceAccount = 'domain\serviceAccount'
    $serviceAccountPassword = 'HardCodedPassword'

   #Debugging
   #Write-Host -BackgroundColor yellow $taskName
   #Write-Host $stringOfDateOfItem
   #Write-Host $tr
   #Write-Host $user
   #Write-Host $UserPassword
   #Write-Host $scheduledType
   #Write-Host $taskName
   #Write-Host $taskToRun

  #Checks if the user is nt authority, it does not put the password field in
if($env:computername.Contains($hostName))
{
    if($user.Contains("NT AUTHORITY"))
    {
        schtasks /create /RU $user /SC $scheduledType /TN $taskName /TR $taskToRun /ST $stringOfDateOfItem /F 
    }
    else
    {
        schtasks /create /RU $user /RP $UserPassword /SC $scheduledType /TN $taskName /TR $taskToRun /ST $stringOfDateOfItem /F 
    }
}
else
{
    if($user.Contains("NT AUTHORITY"))
    {
        schtasks /create /s $hostName /U $serviceAccount /P $serviceAccountPassword /RU $user /SC $scheduledType /TN $taskName /TR $taskToRun /ST $stringOfDateOfItem /F 
    }
    else
    {
        schtasks /create /s $hostName /U $serviceAccount /P $serviceAccountPassword /RU $user /RP $UserPassword /SC $scheduledType /TN $taskName /TR $taskToRun /ST $stringOfDateOfItem /F 
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.