Share via


how to load credentials from txt file?

Question

Tuesday, November 29, 2011 2:24 PM

hi,

is there a way i can load credentials(servername\username, password) to a script so i can retrive wmi info from servers outside my domain?

you see i got this script from internet which gets all the hardware from all the computers in a domain.

http://www.powershellpro.com/wp-content/uploads/CompInv_v2.ps1

this works perfectly but here's the problem.

we have servers running in another location which do not reside in our domain. if i want the script to work now i have to type the servername, username and password manualy for every server.

if i could get the credentials from a file this would save me a lot of time so i want to modify the script to do this

how do i do this and where do i start?

All replies (5)

Tuesday, November 29, 2011 3:12 PM âś…Answered

Credentials to file:

$credential = get-credential
$pwd = convertfrom-securestring $credential.password
$user = $credential.username
$string = "$user `n$pwd
$string | out-file ".\creds.txt"

Credentials from file:

$array = @(get-content .\creds.txt)
$username = $array[0]
$password = convertto-securestring $array[1]
$creds = new-object System.Management.Automation.Pscredential -argumentlist $username, $password

Tuesday, November 29, 2011 2:31 PM | 1 vote

Working with Passwords, Secure Strings and Credentials in Windows PowerShell


Tuesday, November 29, 2011 2:55 PM

thanks for reply

but it does not mention anything to resolve my problem


Wednesday, November 30, 2011 2:58 AM

this seems significantly more secure than rot13 or screnc.exe. But does anyone know just how secure it is? or isn't?

But regardless how difficult it would be to determine the password from the content of the creds.txt file, it would be dead easy to use the code above to run any script under those credentials.

I suppose that the risk would be low enough in the context of the question. The thing to remember is to keep this file as well protected as if it contained the password in plain text.

 


Wednesday, November 30, 2011 3:04 AM

oops, finally read the `working with..." reference. It seems that, since "Only user that created this line can decrypt and use it", my suggestion that another user could effectively use the cred.txt file in a similar manner to use the credentials was incorrect.