$Using: variable preface not respected
tsax6010 opened this issue · comments
Cannot pass $variable from parent session with $Using:variable like the native invoke-command
Please provide an example of your code.
Hi Marc. Here is a sample of how I am trying to use your module.
# Need local admin creds to perform VDI gold image maintenance
# Use Microsoft LAPS Powershell module to retrieve local admin password for
computer from AD
# When running via remote Powershell, need to change to system context to
avoid double hop issues
Import-Module ADMPWD.ps
Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -force
Install-Module -Name Invoke-CommandAs -Confirm:$false -force
$LocationNum = $env:computername.Substring(1,4)
$GoldImage = ("S"+$LocationNum+"GOLD-1")
$UserContext =
([System.Security.Principal.Windowsidentity]::GetCurrent()).Name
If ($UserContext -ne "NT Authority\System")
{
Write-Host "Use Invoke-CommandAS to run as system"
$LAPS = (Invoke-CommandAS -Scriptblock {Get-AdmPwdPassword
-ComputerName $Using:GoldImage} -AsSystem)
}
Else
{
Write-Host "Already running as System. Continue"
$LAPS = (Get-AdmPwdPassword -ComputerName $GoldImage)
}
$LAPS
Dana Nelson
Lead Technical Engineer |
330.463.6875
…On Mon, Dec 2, 2019 at 5:20 PM Marc R Kellerman ***@***.***> wrote:
Please provide an example of your code.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#40?email_source=notifications&email_token=AMK4RSU5OTE4XL25YEYHEFTQWWC2BA5CNFSM4JUKJJC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFWDIMY#issuecomment-560739379>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMK4RSRK3CUC7ZCWMDR4KMDQWWC2BANCNFSM4JUKJJCQ>
.
--
CONFIDENTIALITY/EMAIL NOTICE: The material in this transmission contains
confidential and privileged information intended only for the addressee.
If
you are not the intended recipient, please be advised that you have
received this material in error and that any forwarding, copying, printing,
distribution, use or disclosure of the material is strictly prohibited.
If
you have received this material in error, please (i) do not read it,
(ii)
reply to the sender that you received the message in error, and
(iii) erase
or destroy the material. Emails are not secure and can be
intercepted,
amended, lost or destroyed, or contain viruses. You are deemed
to have
accepted these risks if you communicate with us by email. Thank you.
Seems like you’re not doing a remote session, so might not work. I’ll need to try and se whit the invoke-command works with $using when not against a remote computer.
Yeah, it's a hybrid of sorts. The LAPS cmdlet is natively a remote call to
AD, so the double hop still exists. I need to pass the variable from the
parent session containing the computer name through to the scheduled task
that you are creating.
…On Mon, Dec 2, 2019, 6:35 PM Marc R Kellerman ***@***.***> wrote:
Seems like you’re not doing a remote session, so might not work. I’ll need
to try and se whit the invoke-command works with $using when not against a
remote computer.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#40?email_source=notifications&email_token=AMK4RSQMO443NZ2JWTFA4DLQWWLSRA5CNFSM4JUKJJC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFXMCYQ#issuecomment-560906594>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMK4RSVUIJXZHD43YSMXWXDQWWLSRANCNFSM4JUKJJCQ>
.
--
CONFIDENTIALITY/EMAIL NOTICE: The material in this transmission contains
confidential and privileged information intended only for the addressee.
If
you are not the intended recipient, please be advised that you have
received this material in error and that any forwarding, copying, printing,
distribution, use or disclosure of the material is strictly prohibited.
If
you have received this material in error, please (i) do not read it,
(ii)
reply to the sender that you received the message in error, and
(iii) erase
or destroy the material. Emails are not secure and can be
intercepted,
amended, lost or destroyed, or contain viruses. You are deemed
to have
accepted these risks if you communicate with us by email. Thank you.
What I mean is if you ran this against a remote machine, or even did:
Invoke-CommandAs -ComputerName ‘localhost’ ...
This will allow you to use the $Using variable.
Thanks Marc. I was also able to work around it by referencing arguments:
$LAPS = (Invoke-CommandAS -Scriptblock {
Import-Module ADMPWD.ps
Get-AdmPwdPassword -ComputerName $args[0]} -AsSystem -Args $GoldImage)
}
Dana Nelson
Lead Technical Engineer |
330.463.6875
…On Tue, Dec 3, 2019 at 7:48 AM Marc R Kellerman ***@***.***> wrote:
What I mean is if you ran this against a remote machine, or even did:
Invoke-CommandAs -ComputerName ‘localhost’ ...
This will allow you to use the $Using variable.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#40?email_source=notifications&email_token=AMK4RSUIJ4U7KZI4DFTVMUTQWZISBA5CNFSM4JUKJJC2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFZIC4Y#issuecomment-561152371>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMK4RSVCIVZIE2CEZWLPXNLQWZISBANCNFSM4JUKJJCQ>
.
--
CONFIDENTIALITY/EMAIL NOTICE: The material in this transmission contains
confidential and privileged information intended only for the addressee.
If
you are not the intended recipient, please be advised that you have
received this material in error and that any forwarding, copying, printing,
distribution, use or disclosure of the material is strictly prohibited.
If
you have received this material in error, please (i) do not read it,
(ii)
reply to the sender that you received the message in error, and
(iii) erase
or destroy the material. Emails are not secure and can be
intercepted,
amended, lost or destroyed, or contain viruses. You are deemed
to have
accepted these risks if you communicate with us by email. Thank you.