Page 1 of 2

start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 8:07 am
by PXL_Posh
Product, version and build:
SAPIEN PrimalScript 2019 - v7.6.135.0 - 64 bit
Operating system:
64 bit OS: Windows 10 Enterprise

*** Please add details and screenshots as needed below. ***

When attempting to use alternate credentials an error is being thrown.

ERROR: [localhost] An error occurred while starting the background process. Error reported: The directory name is invalid.
ERROR: + CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
ERROR: + FullyQualifiedErrorId : -2147467259,PSSessionStateBroken

*** PowerShell Script finished. ***
Execution time: < 1 second
  1. try
  2. {
  3.   #Credentials Variables
  4.   $username = "CRP\User"
  5.   $secPass = 'bigstring' | ConvertTo-SecureString -Key (1..32)
  6.   $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secPass
  7.  
  8.   #Scriptblock
  9.   $sqlScript = {
  10.     try{
  11.       #SQL variables
  12.       $sqlDataSource = "SQLServer"
  13.       $sqlDatabase = "LIVE_DB"
  14.    
  15.       $connectionString = "Server=$sqlDataSource;Database=$sqlDatabase;Integrated Security=True;"
  16.       $connection = New-Object System.Data.SqlClient.SqlConnection
  17.       $connection.ConnectionString = $connectionString
  18.    
  19.       $connection.Open()
  20.    
  21.       #Sql Query
  22.       $query = @"
  23.        Select distinct
  24.          A
  25.           B
  26.           C
  27.        From
  28.          A
  29.           B
  30.           C
  31.        Where
  32.          A LIKE '%notepad++%'
  33.        order by
  34.          1
  35. "@
  36.        
  37.         $command = $connection.CreateCommand()
  38.         $command.CommandText = $query
  39.        
  40.         [System.Data.SqlClient.SqlDataAdapter]$SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  41.         $SqlAdapter.SelectCommand = $command
  42.         [System.Data.DataSet]$SQLDS = New-Object -TypeName System.Data.DataSet "SQLDS"
  43.        
  44.         $null = $SqlAdapter.fill($SQLDS)
  45.         $connection.Close()
  46.         $SQLDS.Tables[0]
  47.       }
  48.       catch
  49.       {
  50.         $connection.Close()
  51.        
  52.         if ($_.Exception.InnerException)
  53.         {
  54.             Write-Output $Error
  55.             Write-Output $_.Exception.InnerException.Message
  56.         }
  57.         else
  58.         {
  59.           Write-Output $Error
  60.         }
  61.       }
  62.   }
  63.    
  64.   #Script starts here
  65.   $sqlJob = Start-Job -ScriptBlock $sqlScript -Credential $cred
  66.   $recJob = Receive-Job -Job $sqlJob -Wait -ErrorAction Inquire
  67.  
  68.   #export data
  69.     $CSV = $recJob | ConvertTo-Csv -NoTypeInformation
  70.     $CSVName = '{0}.csv' -f (Get-Date -UFormat "%Y-%m-%d_Software_Export_%I.%M.%S")
  71.     $CSVPath = "$env:USERPROFILE\Documents\Audit\$CSVName"
  72.  
  73.     if (!(Test-Path "$env:USERPROFILE\Documents\Audit")) {
  74.       $null = New-Item -Path "$env:USERPROFILE\Documents\Audit\$CSVName" -ItemType Directory -Force
  75.     }
  76.     $CSV| ConvertFrom-Csv | Export-Csv -Path $CSVPath -NoTypeInformation
  77. }
  78. catch
  79. {
  80.     if ($_.Exception.InnerException)
  81.     {
  82.       Write-Output $Error
  83.       Write-Output $_.Exception.InnerException.Message
  84.     }
  85.     else
  86.     {
  87.       Write-Output $Error
  88.     }
  89. }
The script successfully pulls data from the database when using ISE.
To test, the script is run under a standard user logon which does not have access to the sql server. So the credentials MUST be good or it denies access.
No elevation is used.

In PrimalScript:
Running the script straight from PrimalScript produces the error above.
Running with elevated permissions also throws the error.
Oddly, this style of start-job using alternate credentials worked earlier in the year without throwing an error.

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 8:21 am
by Alexander Riedel
Hmm, that's odd. Check what platform / PowerShell version is selected in the ribbon. Run it in PrimalScript in the console (Ctrl+F8).
Does that change anything?

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 8:25 am
by Alexander Riedel
Also, not knowing with line throws the error, running things in the ISE can be misleading because or runspace contamination. You may use a variable that has been set in the ISE by a previous run, another script or a profile.
In PrimalScript you get a clean slate each time you run a script. So check your variables that they are all initialized.

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 8:47 am
by PXL_Posh
ISE is tested fresh each time. It is closed and reopened.
Even between user sessions the script will complete successfully

PrimalScript ribbon has v5 selected
With and without elevation
Also with and without STA mode


Ctrl+F8 worked

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 8:51 am
by Alexander Riedel
If it works in the console it can be a firewall issue or a setting in the profile that should be in the script really.
Most commonly it is the latter, so try in a console launched with -noprofile

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 8:59 am
by Alexander Riedel
It could also be a remoting configuration issue on the receiving side. Since the request is not coming from an approved host it might get denied.
Not sure how buttoned up your remoting is.

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 10:22 am
by PXL_Posh
how would that differ from PrimalScript console (ctrl+F8), to ISE to standard running of PrimalScript?
Ideally, this would work no matter the source system.
Earlier in the year a version of this construct was generated and continues to work on two separate systems. It's not the same query but it is the same DB and different users have no trouble running it.

The profile issue is of interest to me and started me down the path of doing a start-process instead of start-job. Tracking this down on the web has results specific to start-job and the way it attempts to source powershell.exe.

This was sourced from several locations:
http://www.alexwinner.com/articles/powe ... valid.html

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 12:57 pm
by Alexander Riedel
Ideally yes. But some modules for example are host specific. That link is not working for me.

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 1:08 pm
by PXL_Posh

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 1:19 pm
by PXL_Posh
Issue ODDLY, resolved:
  1. [environment]::CurrentDirectory='C:\Windows\System32\WindowsPowerShell\v1.0'
  2. $sqlJob = Start-Job -ScriptBlock $sqlScript -Credential $cred -ArgumentList ($arg1, $arg2)
  3. $recJob = Receive-Job -Job $sqlJob -Wait -ErrorAction Inquire
The args are provided in the event someone needs to pass along args, which this script does use. Args are not present in the original example because I hard coded them to eliminate issues.