Page 1 of 2

start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 9: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. {
  4.  
  5.   #Credentials Variables
  6.  
  7.   $username = "CRP\User"
  8.  
  9.   $secPass = 'bigstring' | ConvertTo-SecureString -Key (1..32)
  10.  
  11.   $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secPass
  12.  
  13.  
  14.  
  15.   #Scriptblock
  16.  
  17.   $sqlScript = {
  18.  
  19.     try{
  20.  
  21.       #SQL variables
  22.  
  23.       $sqlDataSource = "SQLServer"
  24.  
  25.       $sqlDatabase = "LIVE_DB"
  26.  
  27.    
  28.  
  29.       $connectionString = "Server=$sqlDataSource;Database=$sqlDatabase;Integrated Security=True;"
  30.  
  31.       $connection = New-Object System.Data.SqlClient.SqlConnection
  32.  
  33.       $connection.ConnectionString = $connectionString
  34.  
  35.    
  36.  
  37.       $connection.Open()
  38.  
  39.    
  40.  
  41.       #Sql Query
  42.  
  43.       $query = @"
  44.  
  45.        Select distinct
  46.  
  47.          A
  48.  
  49.           B
  50.  
  51.           C
  52.  
  53.        From
  54.  
  55.          A
  56.  
  57.           B
  58.  
  59.           C
  60.  
  61.        Where
  62.  
  63.          A LIKE '%notepad++%'
  64.  
  65.        order by
  66.  
  67.          1
  68.  
  69. "@
  70.  
  71.        
  72.  
  73.         $command = $connection.CreateCommand()
  74.  
  75.         $command.CommandText = $query
  76.  
  77.        
  78.  
  79.         [System.Data.SqlClient.SqlDataAdapter]$SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  80.  
  81.         $SqlAdapter.SelectCommand = $command
  82.  
  83.         [System.Data.DataSet]$SQLDS = New-Object -TypeName System.Data.DataSet "SQLDS"
  84.  
  85.        
  86.  
  87.         $null = $SqlAdapter.fill($SQLDS)
  88.  
  89.         $connection.Close()
  90.  
  91.         $SQLDS.Tables[0]
  92.  
  93.       }
  94.  
  95.       catch
  96.  
  97.       {
  98.  
  99.         $connection.Close()
  100.  
  101.        
  102.  
  103.         if ($_.Exception.InnerException)
  104.  
  105.         {
  106.  
  107.             Write-Output $Error
  108.  
  109.             Write-Output $_.Exception.InnerException.Message
  110.  
  111.         }
  112.  
  113.         else
  114.  
  115.         {
  116.  
  117.           Write-Output $Error
  118.  
  119.         }
  120.  
  121.       }
  122.  
  123.   }
  124.  
  125.    
  126.  
  127.   #Script starts here
  128.  
  129.   $sqlJob = Start-Job -ScriptBlock $sqlScript -Credential $cred
  130.  
  131.   $recJob = Receive-Job -Job $sqlJob -Wait -ErrorAction Inquire
  132.  
  133.  
  134.  
  135.   #export data
  136.  
  137.     $CSV = $recJob | ConvertTo-Csv -NoTypeInformation
  138.  
  139.     $CSVName = '{0}.csv' -f (Get-Date -UFormat "%Y-%m-%d_Software_Export_%I.%M.%S")
  140.  
  141.     $CSVPath = "$env:USERPROFILE\Documents\Audit\$CSVName"
  142.  
  143.  
  144.  
  145.     if (!(Test-Path "$env:USERPROFILE\Documents\Audit")) {
  146.  
  147.       $null = New-Item -Path "$env:USERPROFILE\Documents\Audit\$CSVName" -ItemType Directory -Force
  148.  
  149.     }
  150.  
  151.     $CSV| ConvertFrom-Csv | Export-Csv -Path $CSVPath -NoTypeInformation
  152.  
  153. }
  154.  
  155. catch
  156.  
  157. {
  158.  
  159.     if ($_.Exception.InnerException)
  160.  
  161.     {
  162.  
  163.       Write-Output $Error
  164.  
  165.       Write-Output $_.Exception.InnerException.Message
  166.  
  167.     }
  168.  
  169.     else
  170.  
  171.     {
  172.  
  173.       Write-Output $Error
  174.  
  175.     }
  176.  
  177. }
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 9: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 9: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 9: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 9: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 9: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 11: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 1: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 2:08 pm
by PXL_Posh

Re: start-job alternate credentials directory name is invalid

Posted: Wed Nov 06, 2019 2:19 pm
by PXL_Posh
Issue ODDLY, resolved:
  1. [environment]::CurrentDirectory='C:\Windows\System32\WindowsPowerShell\v1.0'
  2.  
  3. $sqlJob = Start-Job -ScriptBlock $sqlScript -Credential $cred -ArgumentList ($arg1, $arg2)
  4.  
  5. $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.