Hello,
\nI have a script that gets the local group memberships on some servers remotely. No problem running it in the context of an admin account. The problem is when I am trying to automate it and run it as a service account. Here is the part of the code that gives me trouble:<\/p>\n
$password = 'someStringHere'\n$serverName = 'computer001.domain.local'\n$computer = [ADSI]\"WinNT://$serverName\"\n$computer.psbase.Password = $password\n<\/code><\/pre>\n
Advertisement
And then when I type “$computer.psbase.Password” it shows as empty, without any value.
\nI have tried with:<\/p>\n
$computer.psbase.Password = $credential.GetNetworkCredential().Password\n<\/code><\/pre>\n
Advertisement
still no difference.
\nThis here doesn’t work too:<\/p>\n
$computer = New-Object System.DirectoryServices.DirectoryEntry(\"DistinguishedName\", \"domain\\username\", \"PasswordString\")\n<\/code><\/pre>\nIn all cases the ‘$computer.psbase.username’ part populates successfully but the ‘$computer.psbase.Password’ part is always empty. Powershell doesn’t give any errors while executing the commands.
\nWhat am I doing wrong?<\/p>","upvoteCount":5,"answerCount":7,"datePublished":"2025-07-08T10:20:24.641Z","author":{"@type":"Person","name":"badsector82","url":"https://community.spiceworks.com/u/badsector82"},"suggestedAnswer":[{"@type":"Answer","text":"
Hello,
\nI have a script that gets the local group memberships on some servers remotely. No problem running it in the context of an admin account. The problem is when I am trying to automate it and run it as a service account. Here is the part of the code that gives me trouble:<\/p>\n
$password = 'someStringHere'\n$serverName = 'computer001.domain.local'\n$computer = [ADSI]\"WinNT://$serverName\"\n$computer.psbase.Password = $password\n<\/code><\/pre>\nAnd then when I type “$computer.psbase.Password” it shows as empty, without any value.
\nI have tried with:<\/p>\n
$computer.psbase.Password = $credential.GetNetworkCredential().Password\n<\/code><\/pre>\nstill no difference.
\nThis here doesn’t work too:<\/p>\n
$computer = New-Object System.DirectoryServices.DirectoryEntry(\"DistinguishedName\", \"domain\\username\", \"PasswordString\")\n<\/code><\/pre>\nIn all cases the ‘$computer.psbase.username’ part populates successfully but the ‘$computer.psbase.Password’ part is always empty. Powershell doesn’t give any errors while executing the commands.
\nWhat am I doing wrong?<\/p>","upvoteCount":5,"datePublished":"2025-07-08T10:20:24.709Z","url":"https://community.spiceworks.com/t/pass-credential-with-powershell-using-adsi/1221907/1","author":{"@type":"Person","name":"badsector82","url":"https://community.spiceworks.com/u/badsector82"}},{"@type":"Answer","text":"
what is the password used for? If you are running as a service account locally you shouldn’t need to enter any credentials to get the local admin groups.<\/p>\n
Or are you running this as a service account on a central machine which is then trying to use powershell remoting to query other machines and get their local group details?<\/p>","upvoteCount":3,"datePublished":"2025-07-08T16:42:17.085Z","url":"https://community.spiceworks.com/t/pass-credential-with-powershell-using-adsi/1221907/2","author":{"@type":"Person","name":"molan","url":"https://community.spiceworks.com/u/molan"}},{"@type":"Answer","text":"
Yes, I’m using it on a separate machine from which I query all the servers.
\nI’m using these lines to get the local group members:<\/p>\n
$allGroups = $computer.Children | where { $_.SchemaClassName -eq 'group' }\n foreach ($group in $allGroups) {\n $groupName = $group.Name\n $members = @($group.psbase.invoke('members') | % { ([adsi]$_).Path })\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2025-07-09T10:50:10.218Z","url":"https://community.spiceworks.com/t/pass-credential-with-powershell-using-adsi/1221907/3","author":{"@type":"Person","name":"badsector82","url":"https://community.spiceworks.com/u/badsector82"}},{"@type":"Answer","text":"storing a credential in your script poses a security risk. You can store an encrypted credential, but the credential has to be encrypted by the same user \\ process that is going to use it in order to be able to decrypt it.<\/p>\n
for example to encrypt and store a credential<\/p>\n
$cred = Get-Credential\n$cred | Export-Clixml -Path \"C:\\Secure\\serviceAccountCred.xml\"\n<\/code><\/pre>\nthen to read it using the same process \\ account<\/p>\n
# Load credentials\n$cred = Import-Clixml -Path \"C:\\Secure\\serviceAccountCred.xml\"\n<\/code><\/pre>\n