1

In the script attached I am trying to rename a PC if the PC has a certain hostname. However, the script is proceeding anyway and bypasses the if/else statement.

What am I doing wrong? I am kind of new with Windows Powershell.

Thanks!

# get current computername
$hostname = hostname.exe
#$env:computername
If ( $hostname = "CLNT3100" ) 
{

#Get all the computers with CLNT3* and sort them with the 'highest client' on top. Then put them in newlist.txt
Get-ADComputer -Filter 'SamAccountName -like "CLNT3*"' | Select -Exp Name | Sort-Object -Descending >> C:\newlist.txt

#Put the text file in a variable and show the top line
$Text = Get-Content -Path C:\newlist.txt
#$Text[0]

#Trim CLNT for numbering
$Text1 = $Text[0].TrimStart("CLNT")

#Add 1 number to the previous CLNT
$Text2 = 1 + $Text1

#Add CLNT again to the new variable
$Text3 = "CLNT" + $Text2

#Rename the computer
Rename-Computer –computername minint –newname $Text3
}
Else
{
Write-Host "Computernaam is niet minint!!!"
}

1 Answer 1

1

To compare if two values are equal in Powershell you have to use the -eqoperator. Check the Powershell equality operators to see the others like -gt, -lt etc, or type man about_Comparison_Operators in the PS shell.

Also, to learn Powershell I found this free ebook to be very good.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, so it should be If ( $hostname -eq "CLNT3100" ) then?
Yes. It would be -eq / -like / -match . Depending on what you are after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.