Reputation: 33
I am a powershell newbie and I'm trying to develop a small script in powershell to rename computers in a text file and join them in a domain. I know the main steps but I have some difficulties with the details.
First, I'm using powershell 3.0 and I launch the script from a remote server. Since my computers are freshly dematerialised, I only have the ip adress to help me. Fortunatly, we are using static IP so I created a text file with all ip adress in order I want them to be.
The first problem is our name convention is department-local-00 , 01 , 02 and on and on... I'm unable to create a loop with 2 digits... only 1 for the first 9 entries (0, 1, 2, 3 ....). I tried many thing but it just seems cumbersome and doesn't work.
Here's my code so far:
$Computers = Get-Content -Path "C:\blablabla\computer\test.txt"
$room = "b4554"
$b = 0
foreach ($Computer in $Computers)
{
Write-Host ("department-" + $room + "-" + $b)
Invoke-Command -ComputerName $computer -credential ladmin -ScriptBlock {Rename-Computer -NewName ("logti-" + $classe + "-" + $b) }
add-computer -DomainName axiom.ad.ca -Credential AXIOM\dadmin
Restart-Computer -computername $computer
$b = $b + 1
$computer
}
So far... the only things that's been bugging me is I can't add 00 to 09 in my loop and that it seems by using the -credential switch I am sending my domain admin username and password in clear... which is far from ideal.
Anyone have an idea how to clean this up? I'm open to suggestion if anyone have a better idea.
Upvotes: 0
Views: 1346
Reputation: 200573
Build your strings using the format operator (-f
):
foreach ($Computer in $Computers)
{
Write-Host ("department-{0}-{1:d2}" -f $room, $b)
Invoke-Command -ComputerName $computer -credential ladmin -ScriptBlock {
Rename-Computer -NewName ("logti-{0}-{1:d2}" -f $args[0], $args[1])
} -ArgumentList $classe, $b
add-computer -DomainName axiom.ad.ca -Credential AXIOM\dadmin
Restart-Computer -computername $computer
$b++
$computer
}
Upvotes: 2