VI7HAL_S
VI7HAL_S

Reputation: 1

How to Run some scripts into Virtual Machines(username and password) created in Hyper-V through scripts, without prompts

Problem statement: I want to run some scripts into a Virtual machine created in Hyper-V. The virtual machine has a Username and a password.

The problem is Whenever I use invoke-command or enter-pssession, it prompts for username and password. I need to do it without Entering details manually every time and should be able to do it through scripts.

Upvotes: 0

Views: 714

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

You will need to run the scripts as a Hyper-V admin (local admin on the host), but you can automate the process of providing the VM credentials by export a [pscredential] object to disk:

# Enter the credentials when prompted
$VMCredentials = Get-Credential

$VMCredentials | Export-CliXml path\to\credentials.xml

Export-CliXml will automatically use DPAPI to encrypt the password part using a key derived from the current security context - meaning only the same user on the same machine will be able to decrypt it again.

In order to use these stored credentials, simply call Import-CliXml:

$VMCredentials = Import-CliXml path\to\credentials.xml

Invoke-Command { ... } -VMName VM01 -Credential $VMCredentials

Upvotes: 1

Related Questions