Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to submit credential objects as arguments #49

Open
skille opened this issue May 28, 2020 · 6 comments
Open

Unable to submit credential objects as arguments #49

skille opened this issue May 28, 2020 · 6 comments

Comments

@skille
Copy link

skille commented May 28, 2020

Cannot serialize the credential. If this command is starting a workflow, the credentials cannot be persisted, because the process in which the workflow is started does not have permission to seri
alize credentials.
-- If the workflow was started in a PSSession to the local computer, add the EnableNetworkAccess parameter to the command that created the session.
-- If the workflow was started in a PSSession to a remote computer, add the Authentication parameter with a value of CredSSP to the command that created the session. Or, connect to a session conf
iguration that has a RunAsUser property value.
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-ScheduledTask
+ PSComputerName :

@mkellerman
Copy link
Owner

Please provide an example of the code you are executing.

@skille
Copy link
Author

skille commented May 29, 2020

I tested now again, unable to provoke the error, but still, the code does not run.
Import-Module -Name Invoke-CommandAs
$Credential = Get-Credential
$Server = 'ServerName'
Invoke-CommandAs -ComputerName $Server -AsSystem -ArgumentList $Credential -ScriptBlock {
param ($Credential)
$Credential.UserName
}
The username is not returned

This also returns nothing;
Invoke-CommandAs -ComputerName $Server -AsSystem -ArgumentList $Credential -ScriptBlock {
param ($Credential)
Test-Path -Path $env:TEMP
}

Removing the argument returns True as expected.
Invoke-CommandAs -ComputerName $Server -AsSystem -ScriptBlock {
Test-Path -Path $env:TEMP
}

Looking besides this issue, I must say you have done an impressive job with this module!

@mkellerman
Copy link
Owner

You can pass credential parameter inside this scriptblock.. too much inceptions.. lol rename it to $cred or something

@skille
Copy link
Author

skille commented May 29, 2020

ffs! Thanks a lot ! ;)
`Invoke-CommandAs -ComputerName $server -ArgumentList $Credential -ScriptBlock {

param($Cred)
$Cred.username
test-path $env:temp
}
`
Now returning both the username and True :)

Have a nice day :)

@skille skille closed this as completed May 29, 2020
@skille
Copy link
Author

skille commented May 29, 2020

Actually, I tested wrong when I closed the case.
I forgot to add the -AsSystem switch. Adding it returns nothing.
Invoke-CommandAs -ComputerName $ServerName -ArgumentList $Credential -AsSystem -ScriptBlock {
param($Cred)
$Cred.username
test-path $env:temp
}

@skille skille reopened this May 29, 2020
@skille
Copy link
Author

skille commented Jun 3, 2020

I worked around this problem by converting the credential objects to an base64 encoded string and back.
`
$Base64Credential = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((@{
UserName = $Credential.UserName
Password = $Credential.GetNetworkCredential().Password
} | ConvertTo-Json)))

Invoke-CommandAs -ComputerName $ServerName -AsSystem -ArgumentList $Base64Credential -ScriptBlock {
param(
$Base64Credential
)
$Credential = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($Base64Credential)) | ConvertFrom-Json
$Credential = New-Object -TypeName PScredential -ArgumentList $Credential.UserName,$(ConvertTo-SecureString -String $Credential.Password -AsPlainText -Force)

$Credential

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants