Powershell Script execution using Schdule tasks

I need to go over a list of web services and query them individually and pull the details and store it into the database, I designed the code in the following way

Script 1 connects to the SNOW and pulls in the list of web servcies that i need to connect and loops them through the list and triggers a “Start-ThreadJob” with the service name as an argument
Scipt 2 connects to that specific web service (received as an argument) and connects to it and pull in the data and reports it to the database.
When i run the script 1 manually The script 1 is successfully calling all the required web services and the data is being reported to the database, however when i try to call the script 1 using the windows schdule task, The script 1 executes but the script 2 is not being triggered. do i need to set

Any specific excution policies
Scope
Any inputs is appreciated, Here is an example for how the script 1 and 2 looks

Script 1

#Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
##############################################################################################################
#Function Name: ErrorHandler
#Inputs: Error Type(Info, Warning or Error), Error String
#Output: None
#Description: If the error type is ‘Error’ then captures the last known exceptions reported and appends to the file.

If the error type is something else then the error message is appended to the log file.

##############################################################################################################
Function ErrorHandler ($errType, $ErrorString)
{
$timestamp = Get-Date -Format o
if ($errType -eq ‘Error’)
{
$errorstr = $timestamp + ’ : ’ + $errType + ’ - ’ + $ErrorString + $Error[0].Exception
}
else
{
$errorstr = $timestamp + ’ : ’ + $errType + ’ - ’ + $ErrorString
}
$errcounter = 0
$maxerrcounter = 5
do
{
try
{
$errorstr | Out-File $ErrFile -Append
break
}
catch
{
Start-Sleep 10
$errcounter++
}
}while ($errcounter -le $maxerrcounter)
}

##############################################################################################################
#Initialize Variable
##############################################################################################################
#Key file path
$CredFilePath = "E:\WIS\MonitorConfiguration\Schduled\Stage\creds"
#Error file name
$ErrFile = ‘C:\MonitorReportLogs\Sitescope_Config_Pull.log’
$authorization = $null
#SNOW API server fqdn
$SnowFQDNhost = “”
###################################
#Build Snow API authorization header
###################################
$user= Get-Content $CredFilePath’SNOW_User.txt’
$KeyFile = $CredFilePath+‘AES.key’
$key = Get-Content $KeyFile
$EncryptedPW = Get-Content $CredFilePath’SNOW_Pass.txt’ | ConvertTo-SecureString -Key $key
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($EncryptedPW)
$pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$pair = “$($user):$($pass)”
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$authorization = “Basic $encodedCreds”

#SNOW API authorization Header assignment
$Headers = @{
Authorization = $authorization
}

###################################
#Query SNOW for the list of sitescope instances
###################################
$snowsysurl=“https://” + $SnowFQDNhost + “/api/now/table/u_cmdb_relationship_report?sysparm_query=u_service.name=BSM SiteScope - Production&sysparm_fields=u_hardware.fqdn”
try
{
$info=Invoke-WebRequest -Uri $snowsysurl -Headers $Headers
}
catch
{
$errStr = "SNOW Sitescope server list API request for the URL: “+ $snowsysurl +” failed - "
ErrorHandler ‘Error’ $errStr
}

###################################
#Process each sitescope instance monitor data
###################################
If($info -eq $null){
$errStr = “Unhandled Exception parsing data for " + $SnowFQDNhost + " exiting Script.”
ErrorHandler ‘Error’ $errStr
Exit
}
else
{
#convert the JSON response to PS Custom data object dictionary
$snowContent = $info | ConvertFrom-Json
#Query each sitescope on the list and process the configuration data
$jobCounter = 0
if (($snowContent.result.‘u_hardware.fqdn’ -ne $null) -and ($snowContent.result.‘u_hardware.fqdn’ -ne ‘’))
{
foreach($obj in $snowContent.result.‘u_hardware.fqdn’)
{
if (($obj -ne $null) -and ($obj -ne ‘’))
{
Start-ThreadJob -ScriptBlock {param($hostname) E:\WIS\MonitorConfiguration\Schduled\Stage\SIS_Config_DB_Push.ps1 $hostname}`
-ArgumentList ($obj) -ThrottleLimit 10 -Name $obj
$errStr = "Triggered the script for server : "+ $obj
ErrorHandler ‘Info’ $errStr
}
}
}
else
{
$errStr = “SNOW Sitescope server list API request for the URL: “+ $snowsysurl +” returned 0 records.”
ErrorHandler ‘Error’ $errStr
}
}

###################################
#Garbage Collection
###################################
$info = $null
$snowContent = $null
Script 2: (Limited Version)

#Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
##############################################################################################################
#Function Name: ErrorHandler
#Inputs: Error Type(Info, Warning or Error), Error String
#Output: None
#Description: If the error type is ‘Error’ then captures the last known exceptions reported and appends to the file.

If the error type is something else then the error message is appended to the log file.

##############################################################################################################
Function ErrorHandler ($errType, $ErrorString)
{
$timestamp = Get-Date -Format o
if ($errType -eq ‘Error’)
{
$errorstr = $timestamp + ’ : ’ + $errType + ’ - Server Name: ’ + $SiSServerName + ’ - ’ + $ErrorString + $Error[0].Exception
}
else
{
$errorstr = $timestamp + ’ : ’ + $errType + ’ - Server Name: ’ + $SiSServerName + ’ - ’ + $ErrorString
}
$errcounter = 0
$maxerrcounter = 5
do
{
try
{
$errorstr | Out-File $ErrFile -Append
break
}
catch
{
Start-Sleep 10
$errcounter++
}
}while ($errcounter -le $maxerrcounter)
}

$obj = $args[0]
#Error file name
$ErrFile = ‘C:\MonitorReportLogs\Sitescope_Config_Pull.log’
$errStr = "Start processing data for " + $obj
ErrorHandler ‘Info’ $errStr

Before we proceed - please go back, edit your question and fix the formatting of your code.

To post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

If the E: drive is mapped, does the user defined in the task running the script have the proper permissions?