Difference between revisions of "Power Shell"

Jump to navigation Jump to search
897 bytes added ,  07:36, 1 September 2010
→‎Credentials: Added "Store Password Securely"
(Added "CIM via PowerShell" link)
(→‎Credentials: Added "Store Password Securely")
Line 278: Line 278:
</source>
</source>


However, this doesn't really help much in a fully scripted situation where you need to supply user and pass in an unattended fashion, for that you also need the help of <code> ConvertTo-SecureString </code>
However, this doesn't really help much in a fully scripted situation where you need to supply user and pass in an unattended fashion, for that you also need the help of <code> ConvertTo-SecureString </code>, but if you want to be secure you need to use [[Power_Shell#Store Password Securely|Store Password Securely]]


=== ConvertTo-SecureString ===
=== ConvertTo-SecureString ===
Line 288: Line 288:
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $svr.user,$pass
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $svr.user,$pass
$wmiobj = Get-WMIObject -query "SELECT * FROM Win32_BIOS" -credential $cred -computer $svr.ip
$wmiobj = Get-WMIObject -query "SELECT * FROM Win32_BIOS" -credential $cred -computer $svr.ip
</source>
=== Store Password Securely ===
Adapted (a little) from http://bsonposh.com/archives/338 by Brandon
This is a two stage process, 1st you have to create a file with your (encrypted) password in (its encrypted by the currently logged in user - so if its going to be used in a scheduled task, make sure the user that will execute the script creates the password file).
<source lang="powershell">
$Credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content "Pass.fil"
</source>
Then you can use this in a script, the <code> $cred </code> is a standard credential object.
<source lang="powershell">
$pass = Get-Content "Cred.fil" | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential("MPADGLOBAL\simonstrutt",$pass)
</source>
</source>


Navigation menu