Well, I got this script to work, so there is no emergency here. But I don’t understand why I needed to go to extraordinary lengths to make it work. It is a script that we put on our image. The first time the freshly imaged machine boots and a user logs on, the script changes the time zone, renames the machine, and joins the domain. Then the script deletes any traces that it existed on that machine. (We run it through the runonce registry key.) The script you are seeing has been trimmed and fixed for public viewing.
The first issue I had was trying to break out of the foreach loop. I ended up fixing it by using a label for that loop and breaking on the label name. Before I started using a label, the break command would exit the entire script.
Second issue is that the automatic variables that are produced when the loop reads my csv file, are empty. This happened after I labeled the foreach loop. Before labeling the automatic variables were working fine. So I ended up typing in the full name of the variables. I.e., $machine.MacAddr instead of $_.MacAddr. That made it work again. Is that a bug in the software?
Third issue is one that has bothered me for a 2-3 years. Every time I create automatic variables by reading a file with a foreach loop, I have to put those variables into a regular variable before I can use them. I can’t run AD cmdlets against them, for instance. Ran into this problem way back in the beginning of my powershell career (3 years ago) and once I figured out how to make it work, I just forgot about it. I’d really like to know why I have trouble using automatic variables without plugging them into a real variable.
Thanks for your help. I really want to understand powershell. Since I started learning it, my job changed so that I code 90% of the time.
Makes me AND my boss very happy.
$MachInfo = "c:\PostImg\MachInfotest.csv"
Function Join-Domain ($compname, $room, $seat)
{
write-host "`ncompname = " $compname
write-host "room = " $room
write-host "seat = " $seat
} # End Function Join-Domain
#######################
# Main Driver Program #
#######################
$today = (get-date -format g)
#########################
# Get local MAC address #
#########################
$localnetwork = get-ciminstance `
-class "Win32_NetworkAdapterConfiguration" `
-filter "DHCPEnabled = 'true' AND Ipenabled='true'"
[string]$localMac = $localnetwork.MACAddress
#######################################################################
# Below looks for the local MAC addresses on the machinelist.csv file #
# so the script will know how to rename the machine. #
#######################################################################
$MachList = Import-Csv $MachInfo
:MacFound foreach ($machine in $MachList) {
$Name = $_.MachName
[string]$Mac = $_.MacAddr
[string]$Room = $_.Room
[string]$Seat = $_.Seat
# $Name = $machine.MachName
# [string]$Mac = $machine.MacAddr
# [string]$Room = $machine.Room
# [string]$Seat = $machine.Seat
write-host "automatic variable _.machname = " $_.machname
if ($Mac -eq $localMac)
{
break MacFound # Jump out of the foreach loop. No need to keep looking if you found it.
} # End if mac = localmac
} # End foreach statement
if ($Mac -eq $localMac)
{
# "Sending $Name machine to join-domain function"
Join-Domain -compname $Name `
-room $room `
-seat $seat
} # End if mac = localmac
else
{
"Local PC Mac address could not be found in machinfo.csv list: $localmac"
}
#Remove-Item "c:\PostImg\*" -force # Removes all files in the tjtest folder