1

I have a script that uses invoke-request to get a remote ID string. How can I retrieve the value of instanceID and set it to (SET instanceId) within .bat script

powershell $instanceId =(Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id).Content exit

TNKS

1
  • The variable isn't set in PoSh - but simply output.
  • The batch wraps the powershell command in a parsing for /f loop and sets a batch variable.

@Echo off
For /f "usebackqdelims=" %%A in (
  `Powershell.exe -nologo -NoProfile -command "(Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/instance-id).Content"`
) Do Set InstanceID=%%A
Echo InstanceID=%InstanceID%
Pause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.