PSSession

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

PSSession

Post by obrienc »

I wrote a script in the ISE that works but it doesn't in PSStudio. This is the script. It takes the vm name on hyper-v and finds the guest windows name WIN-something after a sysprep and then renames it to the VMName.
  1. $n = "Web1"
  2. $x = Get-VMNetworkAdapter -ComputerName OWCVHOST -VMName $n | Select -ExpandProperty IPAddresses
  3. $y = ($x -Split '\n')[0]
  4. $z = [System.Net.Dns]::GetHostByAddress($y) | Select -ExpandProperty Hostname
  5. $s = New-PSSession -ComputerName $z -Credential administrator
  6. $a = Get-PSSession | where ComputerName -EQ $z | select -ExpandProperty Id
  7. Enter-PSSession -Id "$a"
  8. Rename-Computer -NewName Web1
  9. Exit-PSSession
  10. Remove-PSSession -Id $a
  11. Restart-VM -ComputerName OWCVHOST -Name Web1 -Force -Confirm:$false
Here is the code with variables in PSStudio. It fails on Enter-PSSession
with this error
Enter-PSSession : The method or operation is not implemented.
TemplateVHDX.psf (103, 3): ERROR: At Line: 103 char: 3
ERROR: + Enter-PSSession -Id $a
  1. $x = Get-VMNetworkAdapter -ComputerName $s1 -VMName $NewFolder | Select -ExpandProperty IPAddresses
  2. $y = ($x -Split '\n')[0]
  3. $z = [System.Net.Dns]::GetHostByAddress($y) | Select -ExpandProperty Hostname
  4. $s = New-PSSession -ComputerName $z -Credential administrator
  5. $a = Get-PSSession | where ComputerName -EQ $z | select -ExpandProperty Id
  6. Enter-PSSession -Id $a
  7. Rename-Computer -ComputerName $z -NewName $a$_
  8. Exit-PSSession
  9. Remove-PSSession -Id $a
  10. Restart-VM -ComputerName $s1 -Name $NewFolder -Force -Confirm:$false
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PSSession

Post by jvierra »

You cannot use Enter-PsSession in a script. It won't work. It only works at a prompt.

You do not need to remote to rename and restart a computer. Just use this command:

Rename-Computer -ComputerName $z -NewName Web1 -Restart -LocalCredential Administrator -Force

help Rename-Computer -ShowWindow
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: PSSession

Post by obrienc »

For anyone who gets this error.
ERROR: Rename-Computer : Cannot establish the WMI connection to the computer 'WIN-' with the following error message: The RPC server is unavailable.

WMI service is running
RPC Service is running

I ran this on the template and it works as intended. Thank you jvierra.
  1. netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked