Had to dig through email to find this, so obviously it needs to go into my aux brain - this blog.
I had a special account set up with WMI permissions. But that's not the same as the SQL Server account running the script..
- To
get credentialing working the first time:
- Log
onto the box as the service account for SQL Server
- Run this in Powershell: read-host -assecurestring | convertfrom-securestring |out-file c:\scripts\securestring_theADaccountName.txt # it will ask for the password – provide it.
- (that
will read the password then save it, encrypted and readable only by that
logged-in user, to the named file. Name it so that you know which
account you have to log in as in order for it to work.)
- To
use from then on:
$username =
"mydomain\myWMIacct"
$password =
cat c:\scripts\securestring_theADaccountName.txt
| convertto-securestring
$cred =
new-object -typename
System.Management.Automation.PSCredential
`
-argumentlist $username, $password
Get-WmiObject -ComputerName myservername -Class
Win32_operatingsystem -Namespace "root\cimv2"
-Credential $Cred
$credential = New-Object System.Management.Automation.PsCredential("yourdomain\youruser", (ConvertTo-SecureString "yourpassword" -AsPlainText -Force))
No comments:
Post a Comment