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
- try
- {
- #Credentials Variables
- $username = "CRP\User"
- $secPass = 'bigstring' | ConvertTo-SecureString -Key (1..32)
- $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secPass
- #Scriptblock
- $sqlScript = {
- try{
- #SQL variables
- $sqlDataSource = "SQLServer"
- $sqlDatabase = "LIVE_DB"
- $connectionString = "Server=$sqlDataSource;Database=$sqlDatabase;Integrated Security=True;"
- $connection = New-Object System.Data.SqlClient.SqlConnection
- $connection.ConnectionString = $connectionString
- $connection.Open()
- #Sql Query
- $query = @"
- Select distinct
- A
- B
- C
- From
- A
- B
- C
- Where
- A LIKE '%notepad++%'
- order by
- 1
- "@
- $command = $connection.CreateCommand()
- $command.CommandText = $query
- [System.Data.SqlClient.SqlDataAdapter]$SQLAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
- $SqlAdapter.SelectCommand = $command
- [System.Data.DataSet]$SQLDS = New-Object -TypeName System.Data.DataSet "SQLDS"
- $null = $SqlAdapter.fill($SQLDS)
- $connection.Close()
- $SQLDS.Tables[0]
- }
- catch
- {
- $connection.Close()
- if ($_.Exception.InnerException)
- {
- Write-Output $Error
- Write-Output $_.Exception.InnerException.Message
- }
- else
- {
- Write-Output $Error
- }
- }
- }
- #Script starts here
- $sqlJob = Start-Job -ScriptBlock $sqlScript -Credential $cred
- $recJob = Receive-Job -Job $sqlJob -Wait -ErrorAction Inquire
- #export data
- $CSV = $recJob | ConvertTo-Csv -NoTypeInformation
- $CSVName = '{0}.csv' -f (Get-Date -UFormat "%Y-%m-%d_Software_Export_%I.%M.%S")
- $CSVPath = "$env:USERPROFILE\Documents\Audit\$CSVName"
- if (!(Test-Path "$env:USERPROFILE\Documents\Audit")) {
- $null = New-Item -Path "$env:USERPROFILE\Documents\Audit\$CSVName" -ItemType Directory -Force
- }
- $CSV| ConvertFrom-Csv | Export-Csv -Path $CSVPath -NoTypeInformation
- }
- catch
- {
- if ($_.Exception.InnerException)
- {
- Write-Output $Error
- Write-Output $_.Exception.InnerException.Message
- }
- else
- {
- Write-Output $Error
- }
- }
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.