Powershell Scheduled task only performing 2 foreach

We have a scheduled task which calls a PS1.  The PS1 using a foreach to add DNS records into InfoBlox.  The scheduled task will start but only add 2 of the DNS records in the CSV.  If the PS1 is run manually it works fine.  Scheduled Task and PS1 below:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2015-07-29T14:36:30.133207</Date>
    <Author>privilagedAccount</Author>
    <Description>Add DNS Hosts records in InfoBlox</Description>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2015-07-29T14:52:00</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>US\us-svcibwebapi</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT2H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell</Command>
      <Arguments>-file G:\DNSWebAPI\DNS_ADD\InfoBlox_Add_Host.ps1</Arguments>
    </Exec>
  </Actions>
</Task>


##############################################################################
#July 2015

#v1.0 - To bulk add/delete/change infoblox records
##########################################################



# import-module activedirectory

#############################
# basic auth header encoding 
#############################
$user = "admin"
$pass = "abc123"
$pair = "$($user):$($pass)"
$encauth = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($encauth)
$basicAuthValue = "Basic $base64"
$headers = @{ Authorization = $basicAuthValue }
$cred = New-Object System.Management.Automation.PSCredential($user,$pass)


$date = Get-Date -Format MMddyyyy
$csvinput = import-csv "G:\DNSWebAPI\DNS_ADD\input\$date-add.csv"
$url = "https://10.60.24.4/wapi/v1.0/record:host"
$logfile = "G:\DNSWebAPI\Logs\$date-ADDs.log"
$tomorrow = (get-date).AddDays(1).ToString("MMddyyyy")


[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

 
foreach ($item in $csvinput)
{
Invoke-WebRequest -Uri $url -Headers $headers -Body (ConvertTo-Json @{ipv4addrs= @( @{ipv4addr=$item.ipaddress} ); name=$item.fqdn; view=$item.view}) -Method Post -ContentType "application/json"
}


New-Item G:\DNSWebAPI\DNS_ADD\input\$tomorrow-add.csv -type file -Value "ipaddress,fqdn,view" -Force





#############################################################
# Powershell to JSON format test for array and hash table
#############################################################
#$body = @{ name="test.local"; ipv4addrs= @( @{ipv4addr="1.1.1.1"} )}
#
#$body = @{ipv4addrs= @( @{ipv4addr="1.1.1.1"} )}
#
#$body = @{ipv4addrs= @( @{ipv4addr="1.1.1.1"} ); name="test.local"}
#
#$body= @{view="default";name="test.local";ipv4addrs= @(@{ipv4addr="1.1.1.21"})}
#
#$JSON = $body | ConvertTo-Json
#
#$JSON

Cannot understand or figure out why the scheduled task will only create 2 of the CSV inputs then hang. Any help greatly appreciated!

-Paul


  • Edited by PaulT15 11 hours 51 minutes ago
July 29th, 2015 3:21pm

I would do it something like this to catch the errors.

# splat the parms
$p = @{
    Uri = $url
    Headers = $headers
    Method = 'Post'
    ContentType = 'application/json'
}

foreach ($item in $csvinput) {
    Try {
        $json = @{
            ipv4addrs = @(
            @{ ipv4addr = $item.ipaddress }
            )
            name = $item.fqdn
            view = $item.view
        } | ConvertTo-Json
        Invoke-WebRequest @p -Body $json -EA Stop
    } 
    Catch {
        "$_" | Out-File errors.log -append
    }
}

I splatted it and spread it so it is easier too read and understand.  THe json will work fine and can be more easily adjusted.

Set the default folder to one that is writable for the task account.

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 4:02pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics