remote rename - computer

by Symbiot at 2012-11-20 02:15:37

Hi

I am trying this:

New-PSSession -ComputerName vditemplate -credential $cred
start-sleep -seconds 5
enter-pssession -computername vditemplate -credential $cred


$a=get-itemproperty -path Registry::'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters' | Select-Object -ExpandProperty VirtualMachineName
$b=$a.TrimEnd("´contoso.com")

#test
$computerinfo = get-wmiobject -Class Win32_computersystem
$computerinfo
$computerinfo.rename($b)

But am getting returnValue: 5 (access denied)

$cred contains:

$password = ConvertTo-Securestring "P@ssw0rD" -asPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("VDITemplate\Administrator", $password )


What could I be doing to fix this?
by DonJ at 2012-11-20 07:50:19
So… couple of things. You’re spinning up two sessions: You create a session with New-PSSession, but you never use it. Because you’re giving Enter-PSSession a computer name, and not a session object, it’s spinning up its own session. Proper use would be:

$s = new-pssession -comp vditemplate -cred $cfred
enter-pssession -session $s


You don’t mention where you’re getting the Access Denied. Is it on the Rename?

Try using Rename-Computer instead.
by Symbiot at 2012-11-20 08:16:09
Yes, access denied is on the Rename…

Rename-Computer : When doing the remote session, I get an error saying that Pwershell doesn’t recognize ‘Rename-Computer’.

I’ve tried putting the commands in a ps1 file, and using ‘invoke-command’ to run the file, but that fails as well…
by DonJ at 2012-11-20 08:23:48
If PowerShell doesn’t recognize Rename-Computer, and you’re certain you’re using v2 or v3, then there’s a problem. Are you dialing into a constrained runspace, perhaps?

Also, can you run the command locally with success? If not, then you’ve got a permissions issue you need to resolve that isn’t related to PowerShell.
by Symbiot at 2012-11-20 08:29:05
hi

just a quick info… I am running the .rename($b) and getting:

[vditemplate]: PS C:\Users\admin\Documents> $computerinfo.rename($b)
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 5


via remote session…
by Symbiot at 2012-11-20 08:37:44
oh
The machine is on a domain… which might be the problem…
But if I remove it from domain, I get no internet connection, and thus cannot remote run anything…
by DonJ at 2012-11-20 08:38:34
Again, you need to try running this command locally, not via Remoting, to see what happens. That’s an important diagnostic step and it’d help me figure out of this is PowerShell or a permissions issue.
by DonJ at 2012-11-20 08:43:06
Removing it from the domain won’t change your "internet connection." Domain membership has nothing to do with Internet connectivity.

Not being in a domain makes it more difficult to connect via Remoting; you’ll likely have to utilize the TrustedHosts workaround. "Secrets of PowerShell Remoting" (www.PowerShellbooks.com - it’s free) covers how to connect to non-domain machines using TrustedHosts. "More difficult" doesn’t mean "impossible."
by JHofferle at 2012-12-04 05:35:33
I’ve found that when using the Rename method of Win32_ComputerSystem, I needed to include Authentication = 6 to avoid the access denied errors.

This is a function that was included in our internal PowerShell module for renaming computers remotely. These days we just use the Rename-Computer cmdlet included with PowerShell v3.

Function Rename-Computer
{
[CmdletBinding(SupportsShouldProcess =$True,
ConfirmImpact = 'High')]
Param
(
[Parameter(
Mandatory = $True,
Position = 0)]
[String]
$ComputerName,

[Parameter(
Mandatory = $True,
Position = 1)]
[String]
$NewName,

[Parameter()]
$Credential = (Get-Credential),

[Parameter()]
[Switch]
$Force
)

Process
{
if ( -NOT (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) )
{
Write-Warning "Unable to connect to $ComputerName"
return
}

$Credential = Get-Credential $Credential

if ( -NOT ($Credential -IS "System.Management.Automation.PsCredential") )
{
return
}

if ( $PSCmdlet.ShouldProcess($ComputerName, "Rename to $NewName") )
{
Write-Verbose "Old Name: $ComputerName"
Write-Verbose "New Name: $NewName"

$WMIParameters = @{Class = 'Win32_ComputerSystem'
ComputerName = $ComputerName
Authentication = 6}

$result = Get-WmiObject @WMIParameters | ForEach-Object {
$.Rename($NewName, <br> $Credential.GetNetworkCredential&#40;&#41;.Password,
$Credential.UserName)}

if ( $result.ReturnValue -eq 0 )
{
Write-Verbose "$ComputerName successfully renamed to $NewName"

if ( $Force )
{
Write-Verbose "Restarting $ComputerName"
try
{
Restart-Computer -ComputerName $ComputerName -Force -ErrorAction STOP
}
catch
{
Write-Warning "There was an error restarting $ComputerName"
Write-Debug ( $
| Out-String )
}
}
}
else
{
Write-Warning "There was an error renaming $ComputerName`: $($result.ReturnValue)"
}
}
}

<#

.Synopsis
Renames a domain-joined computer.

.Description
The Rename-Computer function changes the name of the specified
computer. By renaming the computer, the associated Active Directory
account is also renamed. A reboot is required before the name change
takes effect.

.parameter ComputerName
The current name of the computer to be renamed.

.parameter NewName
The new name for the specified computer.

.parameter Credential
Specifies a domain user account that has permission to rename the computer.

.parameter Force
Forces the computer to reboot after a successful rename. A reboot is
required before the name change takes effect.

.Example
Rename-Computer -ComputerName Computer01 -NewName Computer02

Description
-----------
This command renames the computer Computer01 to Computer02.

.Example
Rename-Computer -ComputerName Computer01 -NewName Computer02 -Verbose -Force

Description
-----------
This command renames the computer Computer01 to Computer02, displays
verbose output, and forces a reboot.

.Example
$MyCred = Get-Credential
Rename-Computer -ComputerName Computer01 -NewName Computer02 -Credential $MyCred

Description
-----------
The first part of this command stores the credential information from
Get-Credential in the $MyCred variable.

The second part of this command renames the computer Computer01 to
Computer02, and uses the credential information stored in $MyCred
instead of prompting for the information.

#>
}
by RichardSiddaway at 2012-12-05 01:52:48
Authentication = 6 means that you are using DCOM packet privacy to encrypt the WMI communication between your local and remote machines.

If you are usintg PS 3 an alternative would be to use Invoke-CimMethod which would negate the need for DCOM