remote rename - computer

Welcome Forums General PowerShell Q&A remote rename - computer

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years, 2 months ago.

  • Author
  • #6098

    Points: 0
    Rank: Member

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


    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

    $computerinfo = get-wmiobject -Class Win32_computersystem

    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


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

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

    via remote session...

    by Symbiot at 2012-11-20 08:37:44

    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" ( – 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')]
    Mandatory = $True,
    Position = 0)]

    Mandatory = $True,
    Position = 1)]

    $Credential = (Get-Credential),


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

    $Credential = Get-Credential $Credential

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

    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, `
    $Credential.GetNetworkCredential().Password, `

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

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

    < # .Synopsis
    Renames a domain-joined computer.

    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.

    Rename-Computer -ComputerName Computer01 -NewName Computer02

    This command renames the computer Computer01 to Computer02.

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

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

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

    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

The topic ‘remote rename - computer’ is closed to new replies.

One thought on “remote rename - computer

  1. Pingback: Renaming a Computer | PowerShell Server Admin

Comments are closed.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort