Asked by:
Substitue variable??

Question
-
Hi guys I need some help ;)
I have written some lines and google couldn't help me, cause I couldn't explain google my problem.
Problem:
In the source you see the commented line with the problem...
I try to get from the value form $map_x the variable $map_G or $map_H or $map_I
$("$map_x") <-- This only works in my head
regards
Michael
$debug = 1 $map_G="\\UNC1" $map_H="\\UNC2" $map_I="\\UNC3" $Networkdrives = "G:\","H:\","I:\" foreach ($Networkdrive in $Networkdrives) { if (Test-Path -Path $Networkdrive) { if ($debug -eq 0 ) { } else { $Networkdrive + " <--- Networkdrive exist" } } else { if ($debug -eq 0 ) { } else { $Networkdrive + " <--- Networkdrive dosn´t exist... start mapping" } $map_x="map_" + $Networkdrive.Substring(0,$Networkdrive.Length-2) #Here I want have the value form $map_x as variable $map_G|H|I (the UNC path) New-PSDrive -Name $Networkdrive -PSProvider "FileSystem" -Root $map_x } }
Wednesday, June 28, 2017 4:09 PM
All replies
-
help join-path -full
Don't use double '\'
$map_G = '\UNC1' $map_H = '\UNC2' $map_I = '\UNC3' $Networkdrives = 'G', 'H', 'I' foreach ($Networkdrive in $Networkdrives) { if (Get-PSDrive $Networkdrive) { if($debug){ "$Networkdrive <--- Networkdrive exist"} } else { if($debug){ "$Networkdrive <--- Networkdrive dosn´t exist... start mapping" } $root = (Get-Variable "map_$Networkdrive").Value New-PSDrive -Name $Networkdrive -PSProvider FileSystem -Root $root } }
\_(ツ)_/
- Edited by jrv Wednesday, June 28, 2017 5:22 PM
Wednesday, June 28, 2017 5:19 PM -
Thanks jrv, ask a profi and get the answer so quick :-D
This is the correct source for me, with the function Get-Vatiable.
Best regards
Michael
$map_G = '\UNC1' $map_H = '\UNC2' $map_I = '\UNC3' $Networkdrives = 'G', 'H', 'I' foreach ($Networkdrive in $Networkdrives) { if (Get-PSDrive $Networkdrive) { if($debug){ "$Networkdrive <--- Networkdrive exist"} } else { if($debug){ "$Networkdrive <--- Networkdrive dosn´t exist... start mapping" } $root = (Get-Variable "$map_x").Value New-PSDrive -Name $Networkdrive -PSProvider FileSystem -Root $root } }
Thursday, June 29, 2017 6:48 AM -
That won't work. Get-Variable does not take a variable it takes a strin.
Try this:
$map_G = '\UNC1' $map_H = '\UNC2' $map_I = '\UNC3' Get-Variable map_G Get-Variable map_H Get-Variable map_I
You can only get a variable by its name. Adding the dollar sign just returns the contents of the variable to name and there is no variable named like that.
To make a name from a drive letter and the string use "map_$NetworkDrive"
This is wrong:
"\\UNC2"
It should be
'\UNC2'
We do not use double slashes in PowerShell. It can cause issues so drop the habit.
This is how we would do this in PowerShell using PS methods.
$VerbosePreference = 'Continue'
$drives = @{ G = '\UNC1' H = '\UNC2' I = '\UNC3' } foreach ($drive in $drives.Keys) { if (Get-PsDrive $drive -EA 0 ) { Write-Verbose "$drive <--- Networkdrive exist" } else { Write-Verbose "$drive <--- Networkdrive dosn´t exist... start mapping" New-PSDrive -Name $drive -PSProvider FileSystem -Root $drives[$drive] } }You seem to be trying to use VB and C methods in PowerShell. They will only get you into trouble.
\_(ツ)_/
- Edited by jrv Thursday, June 29, 2017 7:10 AM
Thursday, June 29, 2017 6:59 AM