The easiest way is to refer to the variable containing the local account in the scriptblock is to prefix the variable with "$Using:".
Like this:
Get-LocalUser $Using:User }).PasswordLastSet
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am a powershell toddler (baby) who is learning by example.
I have a powershell script which inspects each computer currently communicating on my network and lists all local accounts with account information.
The last piece of information needed is password last set. Testing out code to grab this information has me stymied.
$TestComp = "remotecomputer"
$User = "localaccount"
$PasswordLastSet = (Invoke-Command -ComputerName $TestComp -ScriptBlock {
Get-LocalUser localaccount }).PasswordLastSet
write-host $User $PasswordLastSet
outputs the following and is verified correct information
localaccount 2/13/2023 11:09:19 AM
Any fancy powershell magic or mojo to use a variable like in the code below?
$TestComp = "remotecomputer"
$User = "localaccount"
$PasswordLastSet = (Invoke-Command -ComputerName $TestComp -ScriptBlock {
Get-LocalUser $User }).PasswordLastSet
write-host $User $PasswordLastSet
outputs
localaccount
I really do want to get the password last set for a variable list of local accounts on remote computers.
The easiest way is to refer to the variable containing the local account in the scriptblock is to prefix the variable with "$Using:".
Like this:
Get-LocalUser $Using:User }).PasswordLastSet