How to rename a domain computer in Powershell - powershell

I've been trying to rename a domain computer with the following script:
$username = "domain\username"
$password = "password"
$ip = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
$hostname = (nslookup $ip)[3]
$hostname = $hostname.replace(" ", "")
$hostname = $hostname.split(":")[1]
$hostname = $hostname.split(".")[0].ToLower()
Rename-Computer -NewName $hostname -DomainCredential $username -Restart -Force
It does everything I desire apart from inputting the password which at this point is a manual process. Can someone advise me on how to get it to input from $password into the prompt box so that I can completely automate the process?
Alternatively, if there's a better way to do it in Powershell, I'm open to going another direction.

You can use this code:
$Username = "DomainUserName"
$Password = "PlainPassword" | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential($Username ,$Password)
Rename-Computer -NewName $newComputerName -ComputerName $OldName -Restart -DomainCredential $Creds

Related

Powershell: Using specific credentials to execute a command

Dears,
In my Powershell script, I would like to use a specific credential on a remote computer. This login exists on the remote computer and is local.
Hence, my code looks like this:
[Windows.Security.Credentials.PasswordVault,Windows.Security.Credentials,ContentType=WindowsRuntime]
$Vault = New-Object Windows.Security.Credentials.PasswordVault
$RESOURCE = "Resource"
$USERNAME = "MyLogin"
try {
$credentials = $Vault.Retrieve($RESOURCE,$USERNAME)
$pwd = $credentials.Password | ConvertTo-SecureString -Key (1..16)
}
catch {
$pwd = Read-Host "please enter your password:" -AsSecureString
$Encrypted = ConvertFrom-SecureString -SecureString $pwd -Key (1..16)
$credentials = new-object -Type Windows.Security.Credentials.PasswordCredential -ArgumentList $RESOURCE,"REMOTE_COMPUTER\"+$USERNAME,$Encrypted
$Vault.Add($credentials)
}
$cred = New-Object System.Management.Automation.PsCredential($USERNAME,$pwd)
$ComputerMemory = Get-WmiObject -ComputerName "REMOTE_COMPUTER" -Class win32_operatingsystem -ErrorAction Stop -Credential $cred
$Memory = ([math]::round(((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize), 2))
echo $Memory
Problem of course is when executing GetWmiObject, I got an Access denied error...
Is there something wrong with this code?
Thanks in advance for your feedback,
Kind regards,

Check File size powershell ssh

I use this script for check file size. How can I make to verify each computers ip's in ip.txt and the result print in results.txt ?
Thank you!
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\Users\user\Desktop\Check_File.txt -append
$Computers = Get-Content -Path C:\Users\user\Desktop\ip.txt
foreach($Computer in $Computers){
$User = "admin"
$Password = "paSSw"
$Command = "dir C:\Proset\SRM\abc.ddc && hostname"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
Get-SSHTrustedHost | Remove-SSHTrustedHost
$SessionID = New-SSHSession -ComputerName $Computer -Credential $Credentials -AcceptKey:$true
Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command | Select -Expand Output > result.txt
}

Rename computer and add join domain powershell script

I am trying to rename a machine and add it to the domain with 1 restart from WORKGROUP (after the machine gets renamed and joined domain). I tried the code below but it's giving me an error:
$bios = (Get-WmiObject Win32_Bios).SerialNumber
$name = $bios
Rename-Computer -NewName "$name"
$domain = "DOMAINNAME"
$username = "USERNAME"
$password = "PASSWORD" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential -NewName $name
Read-Host "The computer will restart in 5..."
shutdown /r /t 5
The error I'm receiving is the account already exist. It does join the domain but not the "Renamed" name that I want.
This is how it has to be done.
Rename-Computer -NewName newserver -Force
Add-Computer -DomainName example.ne -Credential $credential -NewName newserver -Options JoinWithNewName
You could better read the full documentation of Add-Computer cmdelt. Get-Help Add-Computer -Online

Authenticating with PowerShell to Kill Process

I would like to be able to run this from different workstations which mean we need to authenticate. I have this below but it doesn't seem to work. Can you see anything wrong.
get-content .\killprocess.PS1
$username = 'je\leigh'
$password = cat 'U:\Accounts\Synergy Tools\securestring.txt' | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential `-argumentlist $username, $password
(Get-Content 'U:\Accounts\Synergy Tools\Computers.txt') | ForEach-Object {Get-WmiObject -computer $_ -class win32_process -filter "name='synergy.exe' or name='booking9.exe' or name='acl_apps.exe' or name='pos.exe' or name='booking.exe'" -credential $cred| %{$_.terminate()} | out-null}
If securestring.txt contains a readable password E.g. MyP#ssWord!123. Try using ConvertTo-SecureString's -AsPlainText & -Force parameters:
$password = Get-Content 'U:\...\securestring.txt' | ConvertTo-SecureString -AsPlainText -Force
This will convert the text in securestring.txt into a readable SecureString, which can be viewed with $Password | ConvertFrom-SecureString:
01000000d08c9ddf0115d1118c7a00c04fc297eb010000000a65b1b20fe4e0418aece793d7242358
0000000002000000000003660000c00000001000000082e9a55adb7c90d9bed5946aed69e5380000
000004800000a000000010000000932bccf24879272d36536f6ef98e1be4180000006fb092d673a8
1f84b5954f9cb35daee2addb2d325ec25112140000005c8982b4cd15503000796213c3e0d3869780
Then just give the $password variable straight to -argumentlist $username, $password
If you wanted to you could store the output of ConvertFrom-SecureString into the securestring.txt then use the following in your script:
$password = Get-Content 'U:\...\securestring.txt' | ConvertTo-SecureString
Note: You may get errors if there are more than one line in securestring.txt as it will turn $password an array of strings.

Invoke-Command Elevation Issue

I'm trying to run Invoke-Command with an answer file to the user credentials but I can't seem to get it to finish running. I'm using a local administrator account so nothing on the domain. Here is what I have and the error:
$Username = "$Env:Computername\admin"
$Pass = ConvertTo-SecureString "12345" -AsPlainText -Force
$User = New-Object Management.Automation.PSCredential($UserName, $Pass)
Invoke-Command -ComputerName $Env:Computername -Credential $User -ScriptBlock {
$Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$Name = 'DontDisplayLastUserName'
Set-ItemProperty -path $Path -name $Name -value 0
}
And the error:
[computer] Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerberos authentication: There are curren
tly no logon servers available to service the logon request.
Any help is greatly appreciated.
Working Solution:
$Username = "$Env:Computername\admin"
$Pass = ConvertTo-SecureString "12345" -AsPlainText -Force
$User = New-Object Management.Automation.PSCredential($UserName, $Pass)
Invoke-Command -ComputerName localhost -Credential $User -ScriptBlock {
$Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System'
$Name = 'DontDisplayLastUserName'
Set-ItemProperty -path $Path -name $Name -value 0
}
I finally figured out the problem, hit me kind of randomly. The following line needed to be changed.
Before:
Invoke-Command -ComputerName $Env:Computername
After:
Invoke-Command -ComputerName localhost
Without localhost it was looking at it like a remote computer and not allowing it access.