/build/static/layout/Breadcrumb_cap_w.png

Powershell question

Does anyone use powershell and if so how can in fix this script to auto login and not prompt for credentials?

$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
$computer = Get-ADComputer -server MYSERVERNAME -searchbase "DC=Domain,DC=com" -Filter {passwordlastset -lt $time -and Name -like "*"}
$credential = New-Object System.Management.Automation.PsCredential ("OREILLY2\svc-account", (ConvertTo-SecureString "password" -AsPlainText -Force))
Invoke-Command -computername $computer.name -scriptblock { Reset-ComputermachinePassword -server server.domain.com -Credential $credential }

I still get prompted for creds even though they are in there perhaps i need to add enter-pssession?

0 Comments   [ + ] Show comments

Answers (1)

Posted by: isudothings 5 years ago
Senior Purple Belt
0
Invoke-Command won't lake locally scoped variables and apply them to a remote session. 

Try it this way instead:

$DaysInactive = 90
$time = (Get-Date).Adddays(-($DaysInactive))
$computer = Get-ADComputer -server MYSERVERNAME -searchbase "DC=Domain,DC=com" -Filter {passwordlastset -lt $time -and Name -like "*"}

Invoke-Command -computername $computer.name -scriptblock {
$credential = New-Object System.Management.Automation.PsCredential ("OREILLY2\svc-account", (ConvertTo-SecureString "password" -AsPlainText -Force))
Reset-ComputermachinePassword -server server.domain.com -Credential $credential
}

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ