/build/static/layout/Breadcrumb_cap_w.png
08/31/2018 245 views
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

Comments


All Answers

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
}
Answered 09/11/2018 by: isudothings
Senior Yellow Belt