I am performing a slightly unusual task in that i'm running a PowerShell script which performs various tasks. One step of the task requires me to map network drives using NET USE
(on old XP devices). I have a list of Device, IP in a CSV but, am struggling to pull out the Device Name from the CSV when using Foreach as, i'm embedding the variable from the ForEach
loop in the path of the net use command and, PowerShell is not interpreting it how i would expect.
CSV: Column 1 = Device, Column 2 = IP
Code:
$Site = "ABC"
$mapdevs = Import-Csv "C:\DEVS\$Site.csv"
ForEach ($dev in $mapdevs) {
& Net Use * \\$dev.Device\c$ /user:admin P@ssw0rd }
Does anyone know how i can retrive the column value from the CSV and embed in the net use line?
Otherwise i thought this might work:
$Site = "ABC"
$mapdevs = Import-Csv "C:\DEVS\$Site.csv"
ForEach ($dev.Device in $mapdevs) {
& Net Use * \\$dev.Device\c$ /user:admin P@ssw0rd }
Definitely not right as a second attempt. I can't use New-PSDrive because, the end point systems don't support WMF.
Thanks in Advance
When expanding a $Variable.Property
in a string you have to use an enclosing $()
to force evaluation otherwise only $Variable
is expanded and .Property
treated as text.
You might need to escape literal $
with a backtick.
So use
& Net Use * ("\\$($dev.Device)\c`$") /user:admin P@ssw0rd
Alternativly use the -f
format operator
& Net Use * ("\\{0}\c`$" -f $dev.Device) /user:admin P@ssw0rd
Concatenating also should work:
& Net Use * ("\\"+$dev.Device+"\c`$") /user:admin P@ssw0rd