views:

340

answers:

2

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 ....
+1  A: 

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'
George Howarth
A: 

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 
        }
    }
}

Rihan Meij