Powershell $Credential object to prevent the need for user interaction












1















Hello StackOverflow!,



I am trying to create a new object that I can pass the un/pwd too so that i don't require user interaction when speaking to the rest API i am working with.



Microsoft have a handy example (Example 7) of this found at: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6



However when using this method I receive the following:



    New-Object : Cannot find an overload for "PSCredential" and the argument 
count:
"2".
At line:17 char:15
+ ... redential = New-Object -TypeName
System.Management.Automation.PSCrede ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object],
MethodException
+ FullyQualifiedErrorId :
ConstructorInvokedThrowException,Microsoft.PowerShel
l.Commands.NewObjectCommand

Invoke-RestMethod : A positional parameter cannot be found that accepts
argument
'$null'.
At line:21 char:1
+ Invoke-RestMethod -Method Get -Uri $uri $Credential #-Credential $cre
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod],
ParameterB
indingException
+ FullyQualifiedErrorId :
PositionalParameterNotFound,Microsoft.PowerShell.Com
mands.InvokeRestMethodCommand


Just so that you can verify my code, its basically the same as the Microsoft example:



    $Uri = 'A url to a rest api'
$User = "A Username"
$PWD = "A Password"
$Credential = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList $User, $PWD


#Invokes rest get request to rabbitmq uri
Invoke-RestMethod -Method Get -Uri $uri $Credential


With the error received i imagine i'm not passing the New-Object command something it is expecting ?



Any help is appreciated.



Thanks,



J










share|improve this question























  • As the error message says. There is no "PSCredential" constructor that accepts two arguments of type String (docs). The only constructor that accepts 2 arguments expects String and SecureString. So your underlying question is: How do I convert a String to a SecureString.

    – Tomalak
    Nov 20 '18 at 13:52











  • Thanks for this, @TobyU mentioned this as well and can see he included the same conversion that Microsoft used on their page (doh!), I am now converting the string to a SecureString using this method but am still hitting a brick wall when passing this to the Invoke-RestMethod command.

    – JIreland
    Nov 20 '18 at 14:20











  • What happens when you explicitly state the parameter name, i.e. -Credential $Credential?

    – Tomalak
    Nov 20 '18 at 14:38
















1















Hello StackOverflow!,



I am trying to create a new object that I can pass the un/pwd too so that i don't require user interaction when speaking to the rest API i am working with.



Microsoft have a handy example (Example 7) of this found at: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6



However when using this method I receive the following:



    New-Object : Cannot find an overload for "PSCredential" and the argument 
count:
"2".
At line:17 char:15
+ ... redential = New-Object -TypeName
System.Management.Automation.PSCrede ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object],
MethodException
+ FullyQualifiedErrorId :
ConstructorInvokedThrowException,Microsoft.PowerShel
l.Commands.NewObjectCommand

Invoke-RestMethod : A positional parameter cannot be found that accepts
argument
'$null'.
At line:21 char:1
+ Invoke-RestMethod -Method Get -Uri $uri $Credential #-Credential $cre
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod],
ParameterB
indingException
+ FullyQualifiedErrorId :
PositionalParameterNotFound,Microsoft.PowerShell.Com
mands.InvokeRestMethodCommand


Just so that you can verify my code, its basically the same as the Microsoft example:



    $Uri = 'A url to a rest api'
$User = "A Username"
$PWD = "A Password"
$Credential = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList $User, $PWD


#Invokes rest get request to rabbitmq uri
Invoke-RestMethod -Method Get -Uri $uri $Credential


With the error received i imagine i'm not passing the New-Object command something it is expecting ?



Any help is appreciated.



Thanks,



J










share|improve this question























  • As the error message says. There is no "PSCredential" constructor that accepts two arguments of type String (docs). The only constructor that accepts 2 arguments expects String and SecureString. So your underlying question is: How do I convert a String to a SecureString.

    – Tomalak
    Nov 20 '18 at 13:52











  • Thanks for this, @TobyU mentioned this as well and can see he included the same conversion that Microsoft used on their page (doh!), I am now converting the string to a SecureString using this method but am still hitting a brick wall when passing this to the Invoke-RestMethod command.

    – JIreland
    Nov 20 '18 at 14:20











  • What happens when you explicitly state the parameter name, i.e. -Credential $Credential?

    – Tomalak
    Nov 20 '18 at 14:38














1












1








1








Hello StackOverflow!,



I am trying to create a new object that I can pass the un/pwd too so that i don't require user interaction when speaking to the rest API i am working with.



Microsoft have a handy example (Example 7) of this found at: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6



However when using this method I receive the following:



    New-Object : Cannot find an overload for "PSCredential" and the argument 
count:
"2".
At line:17 char:15
+ ... redential = New-Object -TypeName
System.Management.Automation.PSCrede ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object],
MethodException
+ FullyQualifiedErrorId :
ConstructorInvokedThrowException,Microsoft.PowerShel
l.Commands.NewObjectCommand

Invoke-RestMethod : A positional parameter cannot be found that accepts
argument
'$null'.
At line:21 char:1
+ Invoke-RestMethod -Method Get -Uri $uri $Credential #-Credential $cre
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod],
ParameterB
indingException
+ FullyQualifiedErrorId :
PositionalParameterNotFound,Microsoft.PowerShell.Com
mands.InvokeRestMethodCommand


Just so that you can verify my code, its basically the same as the Microsoft example:



    $Uri = 'A url to a rest api'
$User = "A Username"
$PWD = "A Password"
$Credential = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList $User, $PWD


#Invokes rest get request to rabbitmq uri
Invoke-RestMethod -Method Get -Uri $uri $Credential


With the error received i imagine i'm not passing the New-Object command something it is expecting ?



Any help is appreciated.



Thanks,



J










share|improve this question














Hello StackOverflow!,



I am trying to create a new object that I can pass the un/pwd too so that i don't require user interaction when speaking to the rest API i am working with.



Microsoft have a handy example (Example 7) of this found at: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6



However when using this method I receive the following:



    New-Object : Cannot find an overload for "PSCredential" and the argument 
count:
"2".
At line:17 char:15
+ ... redential = New-Object -TypeName
System.Management.Automation.PSCrede ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object],
MethodException
+ FullyQualifiedErrorId :
ConstructorInvokedThrowException,Microsoft.PowerShel
l.Commands.NewObjectCommand

Invoke-RestMethod : A positional parameter cannot be found that accepts
argument
'$null'.
At line:21 char:1
+ Invoke-RestMethod -Method Get -Uri $uri $Credential #-Credential $cre
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod],
ParameterB
indingException
+ FullyQualifiedErrorId :
PositionalParameterNotFound,Microsoft.PowerShell.Com
mands.InvokeRestMethodCommand


Just so that you can verify my code, its basically the same as the Microsoft example:



    $Uri = 'A url to a rest api'
$User = "A Username"
$PWD = "A Password"
$Credential = New-Object -TypeName
System.Management.Automation.PSCredential -ArgumentList $User, $PWD


#Invokes rest get request to rabbitmq uri
Invoke-RestMethod -Method Get -Uri $uri $Credential


With the error received i imagine i'm not passing the New-Object command something it is expecting ?



Any help is appreciated.



Thanks,



J







powershell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 13:46









JIrelandJIreland

61




61













  • As the error message says. There is no "PSCredential" constructor that accepts two arguments of type String (docs). The only constructor that accepts 2 arguments expects String and SecureString. So your underlying question is: How do I convert a String to a SecureString.

    – Tomalak
    Nov 20 '18 at 13:52











  • Thanks for this, @TobyU mentioned this as well and can see he included the same conversion that Microsoft used on their page (doh!), I am now converting the string to a SecureString using this method but am still hitting a brick wall when passing this to the Invoke-RestMethod command.

    – JIreland
    Nov 20 '18 at 14:20











  • What happens when you explicitly state the parameter name, i.e. -Credential $Credential?

    – Tomalak
    Nov 20 '18 at 14:38



















  • As the error message says. There is no "PSCredential" constructor that accepts two arguments of type String (docs). The only constructor that accepts 2 arguments expects String and SecureString. So your underlying question is: How do I convert a String to a SecureString.

    – Tomalak
    Nov 20 '18 at 13:52











  • Thanks for this, @TobyU mentioned this as well and can see he included the same conversion that Microsoft used on their page (doh!), I am now converting the string to a SecureString using this method but am still hitting a brick wall when passing this to the Invoke-RestMethod command.

    – JIreland
    Nov 20 '18 at 14:20











  • What happens when you explicitly state the parameter name, i.e. -Credential $Credential?

    – Tomalak
    Nov 20 '18 at 14:38

















As the error message says. There is no "PSCredential" constructor that accepts two arguments of type String (docs). The only constructor that accepts 2 arguments expects String and SecureString. So your underlying question is: How do I convert a String to a SecureString.

– Tomalak
Nov 20 '18 at 13:52





As the error message says. There is no "PSCredential" constructor that accepts two arguments of type String (docs). The only constructor that accepts 2 arguments expects String and SecureString. So your underlying question is: How do I convert a String to a SecureString.

– Tomalak
Nov 20 '18 at 13:52













Thanks for this, @TobyU mentioned this as well and can see he included the same conversion that Microsoft used on their page (doh!), I am now converting the string to a SecureString using this method but am still hitting a brick wall when passing this to the Invoke-RestMethod command.

– JIreland
Nov 20 '18 at 14:20





Thanks for this, @TobyU mentioned this as well and can see he included the same conversion that Microsoft used on their page (doh!), I am now converting the string to a SecureString using this method but am still hitting a brick wall when passing this to the Invoke-RestMethod command.

– JIreland
Nov 20 '18 at 14:20













What happens when you explicitly state the parameter name, i.e. -Credential $Credential?

– Tomalak
Nov 20 '18 at 14:38





What happens when you explicitly state the parameter name, i.e. -Credential $Credential?

– Tomalak
Nov 20 '18 at 14:38












1 Answer
1






active

oldest

votes


















1














You need to make a SecureString out of the password like:



$User = "A Username"
$PWD = ConvertTo-SecureString -String "A Password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($User,$PWD)

Invoke-RestMethod -Method Get -Uri $uri -Credential $Credential





share|improve this answer


























  • Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

    – JIreland
    Nov 20 '18 at 13:58













  • My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

    – JIreland
    Nov 20 '18 at 14:00













  • I've extended my answer. Does it work now?

    – TobyU
    Nov 20 '18 at 14:06











  • Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

    – JIreland
    Nov 20 '18 at 14:15






  • 1





    I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

    – TobyU
    Nov 20 '18 at 14:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394438%2fpowershell-credential-object-to-prevent-the-need-for-user-interaction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to make a SecureString out of the password like:



$User = "A Username"
$PWD = ConvertTo-SecureString -String "A Password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($User,$PWD)

Invoke-RestMethod -Method Get -Uri $uri -Credential $Credential





share|improve this answer


























  • Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

    – JIreland
    Nov 20 '18 at 13:58













  • My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

    – JIreland
    Nov 20 '18 at 14:00













  • I've extended my answer. Does it work now?

    – TobyU
    Nov 20 '18 at 14:06











  • Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

    – JIreland
    Nov 20 '18 at 14:15






  • 1





    I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

    – TobyU
    Nov 20 '18 at 14:17
















1














You need to make a SecureString out of the password like:



$User = "A Username"
$PWD = ConvertTo-SecureString -String "A Password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($User,$PWD)

Invoke-RestMethod -Method Get -Uri $uri -Credential $Credential





share|improve this answer


























  • Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

    – JIreland
    Nov 20 '18 at 13:58













  • My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

    – JIreland
    Nov 20 '18 at 14:00













  • I've extended my answer. Does it work now?

    – TobyU
    Nov 20 '18 at 14:06











  • Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

    – JIreland
    Nov 20 '18 at 14:15






  • 1





    I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

    – TobyU
    Nov 20 '18 at 14:17














1












1








1







You need to make a SecureString out of the password like:



$User = "A Username"
$PWD = ConvertTo-SecureString -String "A Password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($User,$PWD)

Invoke-RestMethod -Method Get -Uri $uri -Credential $Credential





share|improve this answer















You need to make a SecureString out of the password like:



$User = "A Username"
$PWD = ConvertTo-SecureString -String "A Password" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($User,$PWD)

Invoke-RestMethod -Method Get -Uri $uri -Credential $Credential






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 14:16

























answered Nov 20 '18 at 13:49









TobyUTobyU

2,208921




2,208921













  • Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

    – JIreland
    Nov 20 '18 at 13:58













  • My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

    – JIreland
    Nov 20 '18 at 14:00













  • I've extended my answer. Does it work now?

    – TobyU
    Nov 20 '18 at 14:06











  • Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

    – JIreland
    Nov 20 '18 at 14:15






  • 1





    I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

    – TobyU
    Nov 20 '18 at 14:17



















  • Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

    – JIreland
    Nov 20 '18 at 13:58













  • My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

    – JIreland
    Nov 20 '18 at 14:00













  • I've extended my answer. Does it work now?

    – TobyU
    Nov 20 '18 at 14:06











  • Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

    – JIreland
    Nov 20 '18 at 14:15






  • 1





    I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

    – TobyU
    Nov 20 '18 at 14:17

















Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

– JIreland
Nov 20 '18 at 13:58







Thanks for the suggestion, however I am now getting the following: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterB indingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Com mands.InvokeRestMethodCommand

– JIreland
Nov 20 '18 at 13:58















My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

– JIreland
Nov 20 '18 at 14:00







My code now looks like: $uri = 'a url to rest api' $User = "a user" $PWD = ConvertTo-SecureString -String "a password" -AsPlainText -Force $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWD #Invokes rest get request to rabbitmq uri Invoke-RestMethod -Method Get -Uri $uri $Credential

– JIreland
Nov 20 '18 at 14:00















I've extended my answer. Does it work now?

– TobyU
Nov 20 '18 at 14:06





I've extended my answer. Does it work now?

– TobyU
Nov 20 '18 at 14:06













Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

– JIreland
Nov 20 '18 at 14:15





Again thank you, but looks like im still getting the same error: Invoke-RestMethod : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'. At line:21 char:1 + Invoke-RestMethod -Method Get -Uri $uri $Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], Parame terBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell .Commands.InvokeRestMethodCommand My Credential obj now looks like yours with the ()

– JIreland
Nov 20 '18 at 14:15




1




1





I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

– TobyU
Nov 20 '18 at 14:17





I've updated my answer once more. You need to use the -Credential parameter with Invoke-RestMethod to provide your credentials.

– TobyU
Nov 20 '18 at 14:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394438%2fpowershell-credential-object-to-prevent-the-need-for-user-interaction%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith