powershellpowershell-4.0mdt

Entering a password for DomainCredential in Rename-Computer in Powershell


I am trying to use a Powershell script to rename computers based on their serial numbers.

So far I have - Set-ExecutionPolicy -Unrestricted $name = (Get-WmiObject win32_bios).SerialNumber.Trim() Rename-Computer -NewName $name -DomainCredential \

I don't want this to prompt because I have a few thousand system to image and I would like this to just do the rename silently and then the MDT 2012 Update 1 will do the reboot.

I am a n00b when it comes to powershell and to scripting and I have spent the better part of a week trying to figure this out. I can get the rename to work locally with no problems but I am hoping I can get some help doing it silently.

I guess my question is, how do I put a password in my powershell script so I don't have to enter it at the workstation?


Solution

  • You can use the Get-WmiObject command let for the same and pass a password stored in a variable like below

    (Get-WmiObject win32_computersystem).Rename( $NewName,$passwd,'domain\username')
    

    to store the password in variable, you can do something like

    $key = 1..32 | ForEach-Object { Get-Random -Maximum 256 }
    $passwd = Read-Host "Enter password" -AsSecureString
    $encpwd = ConvertFrom-SecureString $passwd -Key $key
    $encpwd
    

    This examples are taken from this post how to pass credentials to rename command?

    go through this post; it explains both secure and non-secure way of storing the password in great detail.