BeckoningEagle
New Member
I am trying to run this powershell script at the end of my installation, what it does is replace the computer name with a prefix and some of the octets of the IP address. This is for a very specific need. Assume that the user configured for this task has been secured in a way that can only be used for this specific task. I am aware that storing the password in a script in plain text is not ideal. The script runs fine when running interactively, but it doesn't seem to run at all when placing it as a file in the Before Logon section of the install. I also tried after logon. Any help will be appreciated:
#----BEGINNING OF SCRIPT----------------------------------
# Unsecure Credentials
$Password = ConvertTo-SecureString 'ThePa$$word' -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential ('DOMAIN\TheUser', $Password)
#Generate the new computername, appending the middle 2 octets of the IP Address
$currentIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike '*Loopback*' -and $_.PrefixOrigin -ne 'WellKnown' }).IPAddress
$computerName = "TPR-"
if ($currentIP -ne $null)
{
$modifiedIP = $currentIP.Substring(3)
$modifiedIP = $modifiedIP.replace('.', '-')
$computerName = $computerName + $modifiedIP
}
# Start a new PowerShell session with the provided credentials
Start-Process powershell -Credential $UserCredential -ArgumentList '-NoProfile -Command "Rename-Computer -NewName $computerName"'
Restart-Computer -Force
#-------END OF SCRIPT---------------------------
#----BEGINNING OF SCRIPT----------------------------------
# Unsecure Credentials
$Password = ConvertTo-SecureString 'ThePa$$word' -AsPlainText -Force
$UserCredential = New-Object System.Management.Automation.PSCredential ('DOMAIN\TheUser', $Password)
#Generate the new computername, appending the middle 2 octets of the IP Address
$currentIP = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -notlike '*Loopback*' -and $_.PrefixOrigin -ne 'WellKnown' }).IPAddress
$computerName = "TPR-"
if ($currentIP -ne $null)
{
$modifiedIP = $currentIP.Substring(3)
$modifiedIP = $modifiedIP.replace('.', '-')
$computerName = $computerName + $modifiedIP
}
# Start a new PowerShell session with the provided credentials
Start-Process powershell -Credential $UserCredential -ArgumentList '-NoProfile -Command "Rename-Computer -NewName $computerName"'
Restart-Computer -Force
#-------END OF SCRIPT---------------------------
Last edited: