# Invoke-VMScript passing credentials to Add-Computer cmdlet

**URL:** <https://forums.powershell.org/t/invoke-vmscript-passing-credentials-to-add-computer-cmdlet/6381>\
**Category:** PowerShell Help\
**Created:** [May 5, 2016, 7:45am UTC](https://forums.powershell.org/t/invoke-vmscript-passing-credentials-to-add-computer-cmdlet/6381 "2016-05-05T07:45:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan-iverson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-iverson/32/11_2.png) [@dan-iverson](https://forums.powershell.org/u/dan-iverson)\
**Post date:** [May 5, 2016, 7:45am UTC](https://forums.powershell.org/t/invoke-vmscript-passing-credentials-to-add-computer-cmdlet/6381/1 "2016-05-05T07:45:13Z")

</div>

We have this really cool process within our storage where we can move over a list of VM’s to a test environment and then remove the form the domain and add them to the test domain.

The problem I am running into is when I use Invoke-VMScript to pass the commands to the servers to remove them from the existing domain and then to add them to the new domain, I need credentials to joint them to the new domain. The reason this is a problem is because I can’t figure out how to construct the credential on the host side so they can be used by PowerShell to use in the Add-Computer cmdlet.

I am working on getting it working with a netdom command, but I would prefer figuring it out in PowerShell.

```
netdom join %computername% /Domain:a.local /UserD:me /PasswordD:pw /REBoot
```

---

<div class="post-metadata">

**Author:** ![dan-iverson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-iverson/32/11_2.png) [@dan-iverson](https://forums.powershell.org/u/dan-iverson)\
**Post date:** [May 5, 2016, 9:20am UTC](https://forums.powershell.org/t/invoke-vmscript-passing-credentials-to-add-computer-cmdlet/6381/2 "2016-05-05T09:20:17Z")

</div>

Making progress here, but I wonder if there is a way to do this without having to pass a user name and password in the script?

```
$cmd4 = @'
$password = 'pw' | ConvertTo-SecureString -asPlainText -Force
$username = 'dom\me' 
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -ComputerName $(hostname) -Credential $credential -DomainName 'dom.com' -Restart 
'@
$paramInvoke = @{
    VM = $namefull
    GuestCredential = $CredLocal
    ScriptType = 'Powershell'
    ScriptText = $cmd4
    Server = $viServerTst
}

    Invoke-VMScript @paramInvoke
```

---

<div class="post-metadata">

**Author:** ![dan-iverson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-iverson/32/11_2.png) [@dan-iverson](https://forums.powershell.org/u/dan-iverson)\
**Post date:** [May 5, 2016, 9:45am UTC](https://forums.powershell.org/t/invoke-vmscript-passing-credentials-to-add-computer-cmdlet/6381/3 "2016-05-05T09:45:56Z")

</div>

One more thing that was of concern was the fact that the severs would not have access to their previous domain, would I need credentials and connectivity to change them to work group. If you don’t use the UnjoinDomainCredential parameter, you CAN remove it from the domain with this code.

```
$cmd5 = 'Remove-Computer -WorkgroupName `"workgroup`" -Force -Restart -Confirm:$false'
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/invoke-vmscript-passing-credentials-to-add-computer-cmdlet/6381/4 "2024-05-16T20:41:29Z")

</div>


