The Multifunctioning DBA

Nov 6 2008   11:17PM GMT

Powershell Alternate Credentials



Posted by: Colin Smith
Tags:
Microsoft Windows
Powershell

I received a question from Bryan about my Post on how to use Powershell to Monitor Disk Space. He was curious how he could pass alternate credentials using Powershell in order to connect to some servers that are sitting out in a DMZ. First I want to thank Bryan for this awesome question and thank him again for heading over to http://sysadminsmith.com and submitting the question. You can also leave comments here and I will do my best to get to them all. Anyway, I knew that Powershell has a commandlet of get-credential and that this will open a windows authentication box for you to enter username and password. We do not want to have to manually enter that credential every time though. So here is what I did.:


PS C:\Documents and Settings\smithc> Read-Host ”Enter password to secure:” -AsSecureString | ConvertFrom-SecureString | Out-File c:\securepass.txt
Enter password to secure:: ********
PS C:\Documents and Settings\smithc>

So that was not to bad and here is what is in my file now:


You can see that this is not what I put in for my password. This is awesome!! And check this out. Lets say I get access to this machine and I want to know what that password is. Not only can I not read it in a text editor but even if I get Powershell to get it for me I can still not see the password. Check this out:

I love that. Anyway, now we have a password that is a secure string and we want to use that for out Credential. Here is how we do it.
$pass = get-content c:\securepass.txt | convertto-securestring
$creds = new-object -typename System.Managemnet.Automation.PSCredential -argumtents “Username”, $pass

Is that cool or what? I love it stuff….

So now here is the code with the new pieces as well as the changes in red.

##########################################

###        Gather Disk Information     ###

##########################################

Clear-Content “D:\Scripts\Powershell\PAC\lowdisk.txt”

$i = 0

$pass = get-content c:\securepass.txt | convertto-securestring

$creds = new-object -typename System.Managemnet.Automation.PSCredential -argumtents “Username”, $pass

$users = “email@email.com“, ” email@email.com

$computers = Get-Content “D:\Scripts\Powershell\PAC\computerlistall.txt”

echo “ServerName        Drive Letter    Drive Size    Free Space    Percent Free” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

echo “———-        ————    ———-    ———-    ————” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

foreach ($computer in $computers)

{

$drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk -credentials $creds | Where-Object {$_.DriveType -eq 3}

foreach($drive in $drives)

{

$size1 = $drive.size / 1GB

$size = “{0:N2}” -f $size1

$free1 = $drive.freespace / 1GB

$free = “{0:N2}” -f $free1

$ID = $drive.DeviceID

$a = $free1 / $size1 * 100

$b = “{0:N2}” -f $a

##############################################

##    Determine if any disks low    ##

##############################################

if (($ID -eq “D:”) -or ($ID -eq “S:”) -or ($ID -eq “T:”) -or ($ID -eq “C:”) -and ($free1 -lt 1))

{

echo “$computer        $ID            $size        $free        $b” >> “D:\Scripts\Powershell\PAC\lowdisk.txt”

$i++

}

}

}

####################################################

##    Send Notification if alert $i is greater then 0         ##

####################################################

if ($i -gt 0)

{

foreach ($user in $users)

{

echo “Sending Email notification ro $user”

$smtpServer = “smtp server”

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)

$emailFrom = “fromuser@domain.com

$subject = “Email Subject”

foreach ($line in Get-Content “D:\Scripts\Powershell\PAC\lowdisk.txt”)

{

$body += “$line `n”

}

$smtp.Send($EmailFrom,$user,$subject,$body)

$body = “”

}

}

So I hope that this answers Bryan’s question and I hope it will help out some others. Let me know if you have question or comments about this. Leave them here of check out my site at http://sysadminsmith.com.

1  Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Ipxfer
    Have you tried: $creds.GetNetworkCredential() this actually dumps your username and password in clear text.
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: