Pass password into -credential

narue1992 picture narue1992 · Jan 13, 2016 · Viewed 74.5k times · Source

I am trying to login into a computer. I have been playing with various versions and determined that my past questions were when I didn't know what I was really trying to do.

I discovered that I was on the incorrect PC when running the script.

When I now run the script on the correct PC, the following code requires me to enter the password.

gwmi win32_service –credential domain\username –computer PC#

Is there a way with my current script above, to enforce the username and password without user entry? I have to do this for 100s of PCs so I want to loop through all of them without the user having to input the password 100s of times.


I tried doing the following:

$Username = 'domain\username'
$Password = 'password'

$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$SecureString = $pass
# Users you password securly
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString –computer PC#

However, I get an error of A parameter cannot be found that matches parameter name 'computer'.

also tried:

$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString 
# Sets yous credentials to be used
#$RemoteConn = New-PSSession -ComputerName "PC#" -Credential $MySecureCreds -Authentication default

but the RemoteConn didn't work

Answer

narue1992 picture narue1992 · Jan 13, 2016

WOW I figured it out thanks to https://social.technet.microsoft.com/forums/windowsserver/en-US/440ab7ed-7727-4ff7-a34a-6e69e2dff251/getwmiobject-prompting-for-password-issues

So I didn't realize I can use the $MySecureCreds as the -credential

ANSWER:

$Username = 'domain\username'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force

$SecureString = $pass
# Users you password securly
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$SecureString 

gwmi win32_service –credential $MySecureCreds –computer PC#