Error in powershell script call

Hi, 

I am using 8.1.1 and when trying to call a powershell script through custom process, I am getting below error:

ErrorMessages (2021-01-22 08:48:43.883) [Progress] Reading web response
[System.NullReferenceException] Object reference not set to an instance of an object.
at StdioProcessor.StdioProcessor._Execute(Job job)
at VI.Jobservice.JobComponents.PowershellComponentNet4.Activate(String task)
at VI.Jobservice.JobComponents.PoshBaseTask.Execute()
at System.Linq.Enumerable.Aggregate[TSource](IEnumerable`1 source, Func`3 func)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at VI.Jobservice.JobComponents.PoshBaseTask.<>c.<Execute>b__28_0(PSObject r)

While when I run the script directly in ISE or powershell it gives me the result. 

Has anyone else ever faced this issue?

Regards,

Priyank

  • As the PowerShell is called using the credentials and profile of the user running the Job Service, please check if you may miss some of the modules in that user's profile. You may need to add these to the PowerShell process step configuration.

    If you have the ability to do so, try to test your script using the user running the Job Service.

  • Hi Markus,

    Thanks for coming back to me. It's just an API call to check status of an account on our target system. We have other API calls which are working fine. I am not sure why this one not working. 

    I have tried it with service account directly on powershell it's working fine.

    Regards,

    Priyank

  • Hard to give any further advice without seeing the process step definition and the PowerShell script. You might want to try and route this through the support.

  • Hi Markus, 

    Thanks for your help so far. So my scenario is this:

    1 - I want to check status of an account on target system using the API call - So I have written a powershell script which is working fine through powershell directly with service account as well but does not work when I call it with PowershellComponentNet4 - ExecuteScript where I want return value as "True" or "False" 

    2 - Strangely the script is getting executed with PowerShellComponent - Execute Script but here I do not know how to capture the output value. Its giving me something like this when getting run:

    key                                                  Value

    OutValue_0                                     <some_XML with Value>

    Script                                                <My Script>

    Is there a way to capture this outvalue_0 in next step to update my personwantsorg?

    Regards,

    Priyank

  • Just as an explanation, PowerShellComponent is using PowerShell 2 and PowerShellComponent4 is using PowerShell 3.x to 5.1. You haven't specified in which version you tested your script.

    The following screenshots are from a document for version 6 but the basics, how to process the output are the same.

  • Sorry for not mentioning it before. I am trying this in Powershell v4.0. Basically all my scripts are on 4.0 only. I am calling other script with Powershell4component - they all are working fine but not this one. for your info - below is my script which I am trying to call:

    #Define Parameters
    param([string]$userid)

    $Enabled=""
    #$userid = "watsonb"

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    [string] $allusers = "">hostname/.../"
    $requestUri
    $Username="XXXXX"
    $Password="XXXXX"
    try
    {

    $basicAuth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Username):$($Password)"))
    $headers = @{
    Authorization = $basicAuth
    "Content-Type"="application/json"

    }

    $response = Invoke-RestMethod -Uri $allusers -Method GET -Headers $headers

    foreach ($record in $response.body)
    {
    if($userid -eq $record.username)
    { #$record.username
    if($record.status -eq "S" -or $record.groups.Count -eq 0)
    {$Enabled="False"} else {$Enabled="True"}

    }

    }
    #if ($response.body.status -eq "S" -or $response.body.groups.Count -eq 0){$Enabled="False"}else{$Enabled="True"}

    $Enabled
    }

    catch{

    Write-Warning "Remote Server Response: $($_.Exception.Message)"
    Write-Output "Status Code: $($_.Exception.Response.StatusCode)"
    }

    And below is my ps4 script to call this:

    Dim f As ISqlFormatter = Connection.SqlFormatter
    Dim GamingUid As String = Connection.GetSingleProperty("UNSRootB", "UID_UNSRootB", f.Comparison("DisplayName", "Application", ValType.String, CompareOperator.Equal))
    dim GamingUsername = Connection.GetSingleProperty("UNSAccountB", "accountname", f.AndRelation(f.UidComparison("UID_Person", $UID_PersonOrdered$), _
    f.UidComparison("UID_UNSRootB", GamingUid)))

    Value = String.Format("{0} -userid '{1}'" ,"C:\scripts\Gaming\checkAccountStatus.ps1", GamingUsername)

  • I would add some log messages to your PowerShell script and log the response of the WebService to identify what's going wrong.

    I assume you simulated the process chain in Designer to check if all necessary parameters of the process step look okay?

  • yes they are OK. BTW I have been able to capture OutValue_0 so as of now I have got this script working but not sure why it didn't work with powershellv4 component. 

  • As I've said, If you are using the PowerShellComponent the system is using PowerShell 2 to execute your script and not PowerShell3 or higher. It is interesting that it is not working with the other component. What PowerShell version do you have installed on the Job Service executing the PowerShell script?